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

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