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

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