Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

functions.js 4.6KB

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 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 config = getState()['features/base/config'];
  33. const { analyticsScriptUrls } = config;
  34. const machineId = JitsiMeetJS.getMachineId();
  35. const handlerConstructorOptions = {
  36. product: 'lib-jitsi-meet',
  37. version: JitsiMeetJS.version,
  38. session: machineId,
  39. user: `uid-${machineId}`,
  40. server: getState()['features/base/connection'].locationURL.host
  41. };
  42. _loadHandlers(analyticsScriptUrls, handlerConstructorOptions)
  43. .then(handlers => {
  44. const state = getState();
  45. const permanentProperties: Object = {
  46. roomName: state['features/base/conference'].room,
  47. userAgent: navigator.userAgent
  48. };
  49. const { group, server } = state['features/base/jwt'];
  50. if (server) {
  51. permanentProperties.server = server;
  52. }
  53. if (group) {
  54. permanentProperties.group = group;
  55. }
  56. // Optionally, include local deployment information based on the
  57. // contents of window.config.deploymentInfo.
  58. const { deploymentInfo } = config;
  59. if (deploymentInfo) {
  60. for (const key in deploymentInfo) {
  61. if (deploymentInfo.hasOwnProperty(key)) {
  62. permanentProperties[key] = deploymentInfo[key];
  63. }
  64. }
  65. }
  66. analytics.addPermanentProperties(permanentProperties);
  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. }