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

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