您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.js 4.5KB

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