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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import JitsiMeetJS, { isAnalyticsEnabled } from '../base/lib-jitsi-meet';
  2. import { getJitsiMeetGlobalNS, loadScript } from '../base/util';
  3. const logger = require('jitsi-meet-logger').getLogger(__filename);
  4. /**
  5. * Loads the analytics scripts and inits JitsiMeetJS.analytics by setting
  6. * permanent properties and setting the handlers from the loaded scripts.
  7. * NOTE: Has to be used after JitsiMeetJS.init. Otherwise analytics will be
  8. * null.
  9. *
  10. * @param {Store} store - The redux store in which the specified {@code action}
  11. * is being dispatched.
  12. * @returns {void}
  13. */
  14. export function initAnalytics({ getState }) {
  15. getJitsiMeetGlobalNS().analyticsHandlers = [];
  16. // legacy support for old analytics location
  17. window.analyticsHandlers = [];
  18. const { analytics } = JitsiMeetJS;
  19. if (!isAnalyticsEnabled({ getState }) || !analytics) {
  20. return;
  21. }
  22. const config = getState()['features/base/config'];
  23. const { analyticsScriptUrls } = config;
  24. const machineId = JitsiMeetJS.getMachineId();
  25. const handlerConstructorOptions = {
  26. product: 'lib-jitsi-meet',
  27. version: JitsiMeetJS.version,
  28. session: machineId,
  29. user: `uid-${machineId}`,
  30. server: getState()['features/base/connection'].locationURL.host
  31. };
  32. _loadHandlers(analyticsScriptUrls, handlerConstructorOptions)
  33. .then(handlers => {
  34. const permanentProperties = {
  35. roomName: getState()['features/base/conference'].room,
  36. userAgent: navigator.userAgent
  37. };
  38. const { group, server } = getState()['features/jwt'];
  39. if (server) {
  40. permanentProperties.server = server;
  41. }
  42. if (group) {
  43. permanentProperties.group = group;
  44. }
  45. // optionally include local deployment information based on
  46. // the contents of window.config.deploymentInfo
  47. if (config.deploymentInfo) {
  48. for (const key in config.deploymentInfo) {
  49. if (config.deploymentInfo.hasOwnProperty(key)) {
  50. permanentProperties[key]
  51. = config.deploymentInfo[key];
  52. }
  53. }
  54. }
  55. analytics.addPermanentProperties(permanentProperties);
  56. analytics.setAnalyticsHandlers(handlers);
  57. },
  58. error => analytics.dispose() && logger.error(error));
  59. }
  60. /**
  61. * Tries to load the scripts for the analytics handlers.
  62. *
  63. * @param {Array} scriptURLs - The array of script urls to load.
  64. * @param {Object} handlerConstructorOptions - The default options to pass when
  65. * creating handlers.
  66. * @private
  67. * @returns {Promise} Resolves with the handlers that have been
  68. * successfully loaded and rejects if there are no handlers loaded or the
  69. * analytics is disabled.
  70. */
  71. function _loadHandlers(scriptURLs, handlerConstructorOptions) {
  72. const promises = [];
  73. for (const url of scriptURLs) {
  74. promises.push(
  75. loadScript(url).then(
  76. () => {
  77. return { type: 'success' };
  78. },
  79. error => {
  80. return {
  81. type: 'error',
  82. error,
  83. url
  84. };
  85. }));
  86. }
  87. return Promise.all(promises).then(values => {
  88. for (const el of values) {
  89. if (el.type === 'error') {
  90. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  91. }
  92. }
  93. // analyticsHandlers is the handlers we want to use
  94. // we search for them in the JitsiMeetGlobalNS, but also
  95. // check the old location to provide legacy support
  96. let analyticsHandlers = [];
  97. analyticsHandlers = analyticsHandlers.concat(
  98. getJitsiMeetGlobalNS().analyticsHandlers);
  99. // legacy support for old analytics location
  100. analyticsHandlers = analyticsHandlers.concat(window.analyticsHandlers);
  101. if (analyticsHandlers.length === 0) {
  102. throw new Error('No analytics handlers available');
  103. } else {
  104. const handlers = [];
  105. for (const Handler of analyticsHandlers) {
  106. // catch any error while loading to avoid
  107. // skipping analytics in case of multiple scripts
  108. try {
  109. handlers.push(new Handler(handlerConstructorOptions));
  110. } catch (error) {
  111. logger.error('error instantiating analytics impl', error);
  112. }
  113. }
  114. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  115. return handlers;
  116. }
  117. });
  118. }