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.1KB

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