You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // @flow
  2. import JitsiMeetJS, {
  3. analytics,
  4. isAnalyticsEnabled
  5. } from '../base/lib-jitsi-meet';
  6. import { getJitsiMeetGlobalNS, loadScript } from '../base/util';
  7. import { AmplitudeHandler } from './handlers';
  8. import logger from './logger';
  9. /**
  10. * Sends an event through the lib-jitsi-meet AnalyticsAdapter interface.
  11. *
  12. * @param {Object} event - The event to send. It should be formatted as
  13. * described in AnalyticsAdapter.js in lib-jitsi-meet.
  14. * @returns {void}
  15. */
  16. export function sendAnalytics(event: Object) {
  17. try {
  18. analytics.sendEvent(event);
  19. } catch (e) {
  20. logger.warn(`Error sending analytics event: ${e}`);
  21. }
  22. }
  23. /**
  24. * Resets the analytics adapter to its initial state - removes handlers, cache,
  25. * disabled state, etc.
  26. *
  27. * @returns {void}
  28. */
  29. export function resetAnalytics() {
  30. analytics.reset();
  31. }
  32. /**
  33. * Loads the analytics scripts and inits JitsiMeetJS.analytics by setting
  34. * permanent properties and setting the handlers from the loaded scripts.
  35. * NOTE: Has to be used after JitsiMeetJS.init. Otherwise analytics will be
  36. * null.
  37. *
  38. * @param {Store} store - The redux store in which the specified {@code action}
  39. * is being dispatched.
  40. * @returns {void}
  41. */
  42. export function initAnalytics({ getState }: { getState: Function }) {
  43. getJitsiMeetGlobalNS().analyticsHandlers = [];
  44. window.analyticsHandlers = []; // Legacy support.
  45. if (!analytics || !isAnalyticsEnabled(getState)) {
  46. return;
  47. }
  48. const state = getState();
  49. const config = state['features/base/config'];
  50. const { locationURL } = state['features/base/connection'];
  51. const host = locationURL ? locationURL.host : '';
  52. const {
  53. analytics: analyticsConfig = {},
  54. deploymentInfo
  55. } = config;
  56. const {
  57. amplitudeAPPKey,
  58. blackListedEvents,
  59. scriptURLs,
  60. googleAnalyticsTrackingId,
  61. whiteListedEvents
  62. } = analyticsConfig;
  63. const { group, server, user } = state['features/base/jwt'];
  64. const handlerConstructorOptions = {
  65. amplitudeAPPKey,
  66. blackListedEvents,
  67. envType: (deploymentInfo && deploymentInfo.envType) || 'dev',
  68. googleAnalyticsTrackingId,
  69. group,
  70. host,
  71. product: deploymentInfo && deploymentInfo.product,
  72. subproduct: deploymentInfo && deploymentInfo.environment,
  73. user: user && user.id,
  74. version: JitsiMeetJS.version,
  75. whiteListedEvents
  76. };
  77. _loadHandlers(scriptURLs, handlerConstructorOptions)
  78. .then(handlers => {
  79. const roomName = state['features/base/conference'].room;
  80. const permanentProperties = {};
  81. if (server) {
  82. permanentProperties.server = server;
  83. }
  84. if (group) {
  85. permanentProperties.group = group;
  86. }
  87. // Optionally, include local deployment information based on the
  88. // contents of window.config.deploymentInfo.
  89. if (deploymentInfo) {
  90. for (const key in deploymentInfo) {
  91. if (deploymentInfo.hasOwnProperty(key)) {
  92. permanentProperties[key] = deploymentInfo[key];
  93. }
  94. }
  95. }
  96. analytics.addPermanentProperties(permanentProperties);
  97. analytics.setConferenceName(roomName);
  98. // Set the handlers last, since this triggers emptying of the cache
  99. analytics.setAnalyticsHandlers(handlers);
  100. })
  101. .catch(error => {
  102. analytics.dispose();
  103. logger.error(error);
  104. });
  105. }
  106. /**
  107. * Tries to load the scripts for the analytics handlers.
  108. *
  109. * @param {Array} scriptURLs - The array of script urls to load.
  110. * @param {Object} handlerConstructorOptions - The default options to pass when
  111. * creating handlers.
  112. * @private
  113. * @returns {Promise} Resolves with the handlers that have been
  114. * successfully loaded and rejects if there are no handlers loaded or the
  115. * analytics is disabled.
  116. */
  117. function _loadHandlers(scriptURLs = [], handlerConstructorOptions) {
  118. const promises = [];
  119. for (const url of scriptURLs) {
  120. promises.push(
  121. loadScript(url).then(
  122. () => {
  123. return { type: 'success' };
  124. },
  125. error => {
  126. return {
  127. type: 'error',
  128. error,
  129. url
  130. };
  131. }));
  132. }
  133. return Promise.all(promises).then(values => {
  134. for (const el of values) {
  135. if (el.type === 'error') {
  136. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  137. }
  138. }
  139. // analyticsHandlers is the handlers we want to use
  140. // we search for them in the JitsiMeetGlobalNS, but also
  141. // check the old location to provide legacy support
  142. const analyticsHandlers = [
  143. ...getJitsiMeetGlobalNS().analyticsHandlers,
  144. ...window.analyticsHandlers,
  145. // NOTE: when we add second handler it will be good to put all
  146. // build-in handlers in an array and destruct it here.
  147. AmplitudeHandler
  148. ];
  149. const handlers = [];
  150. for (const Handler of analyticsHandlers) {
  151. // Catch any error while loading to avoid skipping analytics in case
  152. // of multiple scripts.
  153. try {
  154. handlers.push(new Handler(handlerConstructorOptions));
  155. } catch (error) {
  156. logger.warn(`Error creating analytics handler: ${error}`);
  157. }
  158. }
  159. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  160. return handlers;
  161. });
  162. }