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

functions.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // @flow
  2. import JitsiMeetJS, {
  3. analytics,
  4. browser,
  5. isAnalyticsEnabled
  6. } from '../base/lib-jitsi-meet';
  7. import { getJitsiMeetGlobalNS, loadScript } from '../base/util';
  8. import {
  9. checkChromeExtensionsInstalled,
  10. isMobileBrowser
  11. } from '../base/environment/utils';
  12. import { AmplitudeHandler } from './handlers';
  13. import logger from './logger';
  14. /**
  15. * Sends an event through the lib-jitsi-meet AnalyticsAdapter interface.
  16. *
  17. * @param {Object} event - The event to send. It should be formatted as
  18. * described in AnalyticsAdapter.js in lib-jitsi-meet.
  19. * @returns {void}
  20. */
  21. export function sendAnalytics(event: Object) {
  22. try {
  23. analytics.sendEvent(event);
  24. } catch (e) {
  25. logger.warn(`Error sending analytics event: ${e}`);
  26. }
  27. }
  28. /**
  29. * Resets the analytics adapter to its initial state - removes handlers, cache,
  30. * disabled state, etc.
  31. *
  32. * @returns {void}
  33. */
  34. export function resetAnalytics() {
  35. analytics.reset();
  36. }
  37. /**
  38. * Creates the analytics handlers.
  39. *
  40. * @param {Store} store - The redux store in which the specified {@code action} is being dispatched.
  41. * @returns {Promise} Resolves with the handlers that have been successfully loaded.
  42. */
  43. export function createHandlers({ getState }: { getState: Function }) {
  44. getJitsiMeetGlobalNS().analyticsHandlers = [];
  45. window.analyticsHandlers = []; // Legacy support.
  46. if (!isAnalyticsEnabled(getState)) {
  47. return Promise.resolve([]);
  48. }
  49. const state = getState();
  50. const config = state['features/base/config'];
  51. const { locationURL } = state['features/base/connection'];
  52. const host = locationURL ? locationURL.host : '';
  53. const {
  54. analytics: analyticsConfig = {},
  55. deploymentInfo
  56. } = config;
  57. const {
  58. amplitudeAPPKey,
  59. blackListedEvents,
  60. scriptURLs,
  61. googleAnalyticsTrackingId,
  62. whiteListedEvents
  63. } = analyticsConfig;
  64. const { group, user } = state['features/base/jwt'];
  65. const handlerConstructorOptions = {
  66. amplitudeAPPKey,
  67. blackListedEvents,
  68. envType: (deploymentInfo && deploymentInfo.envType) || 'dev',
  69. googleAnalyticsTrackingId,
  70. group,
  71. host,
  72. product: deploymentInfo && deploymentInfo.product,
  73. subproduct: deploymentInfo && deploymentInfo.environment,
  74. user: user && user.id,
  75. version: JitsiMeetJS.version,
  76. whiteListedEvents
  77. };
  78. const handlers = [];
  79. try {
  80. const amplitude = new AmplitudeHandler(handlerConstructorOptions);
  81. handlers.push(amplitude);
  82. // eslint-disable-next-line no-empty
  83. } catch (e) {}
  84. return (
  85. _loadHandlers(scriptURLs, handlerConstructorOptions)
  86. .then(externalHandlers => {
  87. handlers.push(...externalHandlers);
  88. if (handlers.length === 0) {
  89. // Throwing an error in order to dispose the analytics in the catch clause due to the lack of any
  90. // analytics handlers.
  91. throw new Error('No analytics handlers created!');
  92. }
  93. return handlers;
  94. })
  95. .catch(e => {
  96. analytics.dispose();
  97. logger.error(e);
  98. return [];
  99. }));
  100. }
  101. /**
  102. * Inits JitsiMeetJS.analytics by setting permanent properties and setting the handlers from the loaded scripts.
  103. * NOTE: Has to be used after JitsiMeetJS.init. Otherwise analytics will be null.
  104. *
  105. * @param {Store} store - The redux store in which the specified {@code action} is being dispatched.
  106. * @param {Array<Object>} handlers - The analytics handlers.
  107. * @returns {void}
  108. */
  109. export function initAnalytics({ getState }: { getState: Function }, handlers: Array<Object>) {
  110. if (!isAnalyticsEnabled(getState) || handlers.length === 0) {
  111. return;
  112. }
  113. const state = getState();
  114. const config = state['features/base/config'];
  115. const {
  116. deploymentInfo
  117. } = config;
  118. const { group, server } = state['features/base/jwt'];
  119. const roomName = state['features/base/conference'].room;
  120. const permanentProperties = {};
  121. if (server) {
  122. permanentProperties.server = server;
  123. }
  124. if (group) {
  125. permanentProperties.group = group;
  126. }
  127. // Report if user is using websocket
  128. permanentProperties.websocket = navigator.product !== 'ReactNative' && typeof config.websocket === 'string';
  129. // Optionally, include local deployment information based on the
  130. // contents of window.config.deploymentInfo.
  131. if (deploymentInfo) {
  132. for (const key in deploymentInfo) {
  133. if (deploymentInfo.hasOwnProperty(key)) {
  134. permanentProperties[key] = deploymentInfo[key];
  135. }
  136. }
  137. }
  138. analytics.addPermanentProperties(permanentProperties);
  139. analytics.setConferenceName(roomName);
  140. // Set the handlers last, since this triggers emptying of the cache
  141. analytics.setAnalyticsHandlers(handlers);
  142. if (!isMobileBrowser() && browser.isChrome()) {
  143. const bannerCfg = state['features/base/config'].chromeExtensionBanner;
  144. checkChromeExtensionsInstalled(bannerCfg).then(extensionsInstalled => {
  145. if (extensionsInstalled?.length) {
  146. analytics.addPermanentProperties({
  147. hasChromeExtension: extensionsInstalled.some(ext => ext)
  148. });
  149. }
  150. });
  151. }
  152. }
  153. /**
  154. * Tries to load the scripts for the analytics handlers and creates them.
  155. *
  156. * @param {Array} scriptURLs - The array of script urls to load.
  157. * @param {Object} handlerConstructorOptions - The default options to pass when creating handlers.
  158. * @private
  159. * @returns {Promise} Resolves with the handlers that have been successfully loaded and rejects if there are no handlers
  160. * loaded or the analytics is disabled.
  161. */
  162. function _loadHandlers(scriptURLs = [], handlerConstructorOptions) {
  163. const promises = [];
  164. for (const url of scriptURLs) {
  165. promises.push(
  166. loadScript(url).then(
  167. () => {
  168. return { type: 'success' };
  169. },
  170. error => {
  171. return {
  172. type: 'error',
  173. error,
  174. url
  175. };
  176. }));
  177. }
  178. return Promise.all(promises).then(values => {
  179. for (const el of values) {
  180. if (el.type === 'error') {
  181. logger.warn(`Failed to load ${el.url}: ${el.error}`);
  182. }
  183. }
  184. // analyticsHandlers is the handlers we want to use
  185. // we search for them in the JitsiMeetGlobalNS, but also
  186. // check the old location to provide legacy support
  187. const analyticsHandlers = [
  188. ...getJitsiMeetGlobalNS().analyticsHandlers,
  189. ...window.analyticsHandlers
  190. ];
  191. const handlers = [];
  192. for (const Handler of analyticsHandlers) {
  193. // Catch any error while loading to avoid skipping analytics in case
  194. // of multiple scripts.
  195. try {
  196. handlers.push(new Handler(handlerConstructorOptions));
  197. } catch (error) {
  198. logger.warn(`Error creating analytics handler: ${error}`);
  199. }
  200. }
  201. logger.debug(`Loaded ${handlers.length} analytics handlers`);
  202. return handlers;
  203. });
  204. }