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

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