Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

functions.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. const logger = require('jitsi-meet-logger').getLogger(__filename);
  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. scriptURLs,
  59. googleAnalyticsTrackingId
  60. } = analyticsConfig;
  61. const { group, server, user } = state['features/base/jwt'];
  62. const handlerConstructorOptions = {
  63. amplitudeAPPKey,
  64. envType: (deploymentInfo && deploymentInfo.envType) || 'dev',
  65. googleAnalyticsTrackingId,
  66. group,
  67. host,
  68. product: deploymentInfo && deploymentInfo.product,
  69. subproduct: deploymentInfo && deploymentInfo.environment,
  70. user: user && user.id,
  71. version: JitsiMeetJS.version
  72. };
  73. _loadHandlers(scriptURLs, handlerConstructorOptions)
  74. .then(handlers => {
  75. const roomName = state['features/base/conference'].room;
  76. const permanentProperties = {};
  77. if (server) {
  78. permanentProperties.server = server;
  79. }
  80. if (group) {
  81. permanentProperties.group = group;
  82. }
  83. // Optionally, include local deployment information based on the
  84. // contents of window.config.deploymentInfo.
  85. if (deploymentInfo) {
  86. for (const key in deploymentInfo) {
  87. if (deploymentInfo.hasOwnProperty(key)) {
  88. permanentProperties[key] = deploymentInfo[key];
  89. }
  90. }
  91. }
  92. analytics.addPermanentProperties(permanentProperties);
  93. analytics.setConferenceName(roomName);
  94. // Set the handlers last, since this triggers emptying of the cache
  95. analytics.setAnalyticsHandlers(handlers);
  96. })
  97. .catch(error => {
  98. analytics.dispose();
  99. logger.error(error);
  100. });
  101. }
  102. /**
  103. * Tries to load the scripts for the analytics handlers.
  104. *
  105. * @param {Array} scriptURLs - The array of script urls to load.
  106. * @param {Object} handlerConstructorOptions - The default options to pass when
  107. * creating handlers.
  108. * @private
  109. * @returns {Promise} Resolves with the handlers that have been
  110. * successfully loaded and rejects if there are no handlers loaded or the
  111. * analytics is disabled.
  112. */
  113. function _loadHandlers(scriptURLs = [], handlerConstructorOptions) {
  114. const promises = [];
  115. for (const url of scriptURLs) {
  116. promises.push(
  117. loadScript(url).then(
  118. () => {
  119. return { type: 'success' };
  120. },
  121. error => {
  122. return {
  123. type: 'error',
  124. error,
  125. url
  126. };
  127. }));
  128. }
  129. return Promise.all(promises).then(values => {
  130. for (const el of values) {
  131. if (el.type === 'error') {
  132. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  133. }
  134. }
  135. // analyticsHandlers is the handlers we want to use
  136. // we search for them in the JitsiMeetGlobalNS, but also
  137. // check the old location to provide legacy support
  138. const analyticsHandlers = [
  139. ...getJitsiMeetGlobalNS().analyticsHandlers,
  140. ...window.analyticsHandlers,
  141. // NOTE: when we add second handler it will be good to put all
  142. // build-in handlers in an array and destruct it here.
  143. AmplitudeHandler
  144. ];
  145. const handlers = [];
  146. for (const Handler of analyticsHandlers) {
  147. // Catch any error while loading to avoid skipping analytics in case
  148. // of multiple scripts.
  149. try {
  150. handlers.push(new Handler(handlerConstructorOptions));
  151. } catch (error) {
  152. logger.warn(`Error creating analytics handler: ${error}`);
  153. }
  154. }
  155. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  156. return handlers;
  157. });
  158. }