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 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 { analyticsScriptUrls, deploymentInfo } = config;
  41. const { group, server, user } = state['features/base/jwt'];
  42. const handlerConstructorOptions = {
  43. envType: (deploymentInfo && deploymentInfo.envType) || 'dev',
  44. group,
  45. subproduct: deploymentInfo && deploymentInfo.environment,
  46. user: user && user.id,
  47. version: JitsiMeetJS.version
  48. };
  49. _loadHandlers(analyticsScriptUrls, handlerConstructorOptions)
  50. .then(handlers => {
  51. const roomName = state['features/base/conference'].room;
  52. const permanentProperties = {};
  53. if (server) {
  54. permanentProperties.server = server;
  55. }
  56. if (group) {
  57. permanentProperties.group = group;
  58. }
  59. // Optionally, include local deployment information based on the
  60. // contents of window.config.deploymentInfo.
  61. if (deploymentInfo) {
  62. for (const key in deploymentInfo) {
  63. if (deploymentInfo.hasOwnProperty(key)) {
  64. permanentProperties[key] = deploymentInfo[key];
  65. }
  66. }
  67. }
  68. analytics.addPermanentProperties(permanentProperties);
  69. analytics.setConferenceName(roomName);
  70. // Set the handlers last, since this triggers emptying of the cache
  71. analytics.setAnalyticsHandlers(handlers);
  72. },
  73. error => analytics.dispose() && logger.error(error));
  74. }
  75. /**
  76. * Tries to load the scripts for the analytics handlers.
  77. *
  78. * @param {Array} scriptURLs - The array of script urls to load.
  79. * @param {Object} handlerConstructorOptions - The default options to pass when
  80. * creating handlers.
  81. * @private
  82. * @returns {Promise} Resolves with the handlers that have been
  83. * successfully loaded and rejects if there are no handlers loaded or the
  84. * analytics is disabled.
  85. */
  86. function _loadHandlers(scriptURLs, handlerConstructorOptions) {
  87. const promises = [];
  88. for (const url of scriptURLs) {
  89. promises.push(
  90. loadScript(url).then(
  91. () => {
  92. return { type: 'success' };
  93. },
  94. error => {
  95. return {
  96. type: 'error',
  97. error,
  98. url
  99. };
  100. }));
  101. }
  102. return Promise.all(promises).then(values => {
  103. for (const el of values) {
  104. if (el.type === 'error') {
  105. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  106. }
  107. }
  108. // analyticsHandlers is the handlers we want to use
  109. // we search for them in the JitsiMeetGlobalNS, but also
  110. // check the old location to provide legacy support
  111. const analyticsHandlers = [
  112. ...getJitsiMeetGlobalNS().analyticsHandlers,
  113. ...window.analyticsHandlers
  114. ];
  115. if (analyticsHandlers.length === 0) {
  116. throw new Error('No analytics handlers available');
  117. }
  118. const handlers = [];
  119. for (const Handler of analyticsHandlers) {
  120. // Catch any error while loading to avoid skipping analytics in case
  121. // of multiple scripts.
  122. try {
  123. handlers.push(new Handler(handlerConstructorOptions));
  124. } catch (error) {
  125. logger.warn(`Error creating analytics handler: ${error}`);
  126. }
  127. }
  128. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  129. return handlers;
  130. });
  131. }