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

functions.js 4.5KB

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