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.

AmplitudeHandler.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import amplitude from 'amplitude-js';
  2. import logger from '../logger';
  3. import AbstractHandler from './AbstractHandler';
  4. import { fixDeviceID } from './amplitude';
  5. /**
  6. * Analytics handler for Amplitude.
  7. */
  8. export default class AmplitudeHandler extends AbstractHandler {
  9. /**
  10. * Creates new instance of the Amplitude analytics handler.
  11. *
  12. * @param {Object} options -
  13. * @param {string} options.amplitudeAPPKey - The Amplitude app key required
  14. * by the Amplitude API.
  15. */
  16. constructor(options) {
  17. super(options);
  18. const { amplitudeAPPKey, host, user } = options;
  19. this._enabled = true;
  20. this._host = host; // Only used on React Native.
  21. const onError = e => {
  22. logger.error('Error initializing Amplitude', e);
  23. this._enabled = false;
  24. };
  25. const amplitudeOptions = {
  26. domain: navigator.product === 'ReactNative' ? host : undefined,
  27. includeReferrer: true,
  28. onError
  29. };
  30. this._getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
  31. fixDeviceID(this._getInstance());
  32. if (user) {
  33. this._getInstance().setUserId(user);
  34. }
  35. }
  36. /**
  37. * Returns the AmplitudeClient instance.
  38. *
  39. * @returns {AmplitudeClient}
  40. */
  41. _getInstance() {
  42. const name = navigator.product === 'ReactNative' ? this._host : undefined;
  43. return amplitude.getInstance(name);
  44. }
  45. /**
  46. * Sets the Amplitude user properties.
  47. *
  48. * @param {Object} userProps - The user portperties.
  49. * @returns {void}
  50. */
  51. setUserProperties(userProps) {
  52. if (this._enabled) {
  53. this._getInstance().setUserProperties(userProps);
  54. }
  55. }
  56. /**
  57. * Sends an event to Amplitude. The format of the event is described
  58. * in AnalyticsAdapter in lib-jitsi-meet.
  59. *
  60. * @param {Object} event - The event in the format specified by
  61. * lib-jitsi-meet.
  62. * @returns {void}
  63. */
  64. sendEvent(event) {
  65. if (this._shouldIgnore(event)) {
  66. return;
  67. }
  68. this._getInstance().logEvent(this._extractName(event), event);
  69. }
  70. /**
  71. * Return amplitude identity information.
  72. *
  73. * @returns {Object}
  74. */
  75. getIdentityProps() {
  76. return {
  77. sessionId: this._getInstance().getSessionId(),
  78. deviceId: this._getInstance().options.deviceId,
  79. userId: this._getInstance().options.userId
  80. };
  81. }
  82. }