Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

functions.js 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. // Report if user is using websocket
  88. permanentProperties.websocket = navigator.product !== 'ReactNative' && typeof config.websocket === 'string';
  89. // Optionally, include local deployment information based on the
  90. // contents of window.config.deploymentInfo.
  91. if (deploymentInfo) {
  92. for (const key in deploymentInfo) {
  93. if (deploymentInfo.hasOwnProperty(key)) {
  94. permanentProperties[key] = deploymentInfo[key];
  95. }
  96. }
  97. }
  98. analytics.addPermanentProperties(permanentProperties);
  99. analytics.setConferenceName(roomName);
  100. // Set the handlers last, since this triggers emptying of the cache
  101. analytics.setAnalyticsHandlers(handlers);
  102. })
  103. .catch(error => {
  104. analytics.dispose();
  105. logger.error(error);
  106. });
  107. }
  108. /**
  109. * Tries to load the scripts for the analytics handlers.
  110. *
  111. * @param {Array} scriptURLs - The array of script urls to load.
  112. * @param {Object} handlerConstructorOptions - The default options to pass when
  113. * creating handlers.
  114. * @private
  115. * @returns {Promise} Resolves with the handlers that have been
  116. * successfully loaded and rejects if there are no handlers loaded or the
  117. * analytics is disabled.
  118. */
  119. function _loadHandlers(scriptURLs = [], handlerConstructorOptions) {
  120. const promises = [];
  121. for (const url of scriptURLs) {
  122. promises.push(
  123. loadScript(url).then(
  124. () => {
  125. return { type: 'success' };
  126. },
  127. error => {
  128. return {
  129. type: 'error',
  130. error,
  131. url
  132. };
  133. }));
  134. }
  135. return Promise.all(promises).then(values => {
  136. for (const el of values) {
  137. if (el.type === 'error') {
  138. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  139. }
  140. }
  141. // analyticsHandlers is the handlers we want to use
  142. // we search for them in the JitsiMeetGlobalNS, but also
  143. // check the old location to provide legacy support
  144. const analyticsHandlers = [
  145. ...getJitsiMeetGlobalNS().analyticsHandlers,
  146. ...window.analyticsHandlers,
  147. // NOTE: when we add second handler it will be good to put all
  148. // build-in handlers in an array and destruct it here.
  149. AmplitudeHandler
  150. ];
  151. const handlers = [];
  152. for (const Handler of analyticsHandlers) {
  153. // Catch any error while loading to avoid skipping analytics in case
  154. // of multiple scripts.
  155. try {
  156. handlers.push(new Handler(handlerConstructorOptions));
  157. } catch (error) {
  158. logger.warn(`Error creating analytics handler: ${error}`);
  159. }
  160. }
  161. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  162. return handlers;
  163. });
  164. }