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 6.7KB

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