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.ts 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import logger from '../logger';
  2. import AbstractHandler, { IEvent } from './AbstractHandler';
  3. import { fixDeviceID } from './amplitude/fixDeviceID';
  4. import amplitude from './amplitude/lib';
  5. /**
  6. * Analytics handler for Amplitude.
  7. */
  8. export default class AmplitudeHandler extends AbstractHandler {
  9. _deviceId: string;
  10. _userId: Object;
  11. /**
  12. * Creates new instance of the Amplitude analytics handler.
  13. *
  14. * @param {Object} options - The amplitude options.
  15. * @param {string} options.amplitudeAPPKey - The Amplitude app key required by the Amplitude API.
  16. * @param {boolean} options.amplitudeIncludeUTM - Whether to include UTM parameters
  17. * in the Amplitude events.
  18. */
  19. constructor(options: any) {
  20. super(options);
  21. const {
  22. amplitudeAPPKey,
  23. amplitudeIncludeUTM: includeUtm = true,
  24. user
  25. } = options;
  26. this._enabled = true;
  27. const onError = (e: Error) => {
  28. logger.error('Error initializing Amplitude', e);
  29. this._enabled = false;
  30. };
  31. // Forces sending all events on exit (flushing) via sendBeacon
  32. const onExitPage = () => {
  33. // @ts-ignore
  34. amplitude.getInstance().sendEvents();
  35. };
  36. if (navigator.product === 'ReactNative') {
  37. amplitude.getInstance().init(amplitudeAPPKey);
  38. fixDeviceID(amplitude.getInstance()).then(() => {
  39. amplitude.getInstance().getDeviceId()
  40. // @ts-ignore
  41. .then((deviceId: string) => {
  42. this._deviceId = deviceId;
  43. });
  44. });
  45. } else {
  46. const amplitudeOptions: any = {
  47. includeReferrer: true,
  48. includeUtm,
  49. saveParamsReferrerOncePerSession: false,
  50. onError,
  51. onExitPage
  52. };
  53. // @ts-ignore
  54. amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
  55. fixDeviceID(amplitude.getInstance());
  56. }
  57. if (user) {
  58. this._userId = user;
  59. amplitude.getInstance().setUserId(user);
  60. }
  61. }
  62. /**
  63. * Sets the Amplitude user properties.
  64. *
  65. * @param {Object} userProps - The user portperties.
  66. * @returns {void}
  67. */
  68. setUserProperties(userProps: any) {
  69. if (this._enabled) {
  70. amplitude.getInstance().setUserProperties(userProps);
  71. }
  72. }
  73. /**
  74. * Sends an event to Amplitude. The format of the event is described
  75. * in AnalyticsAdapter in lib-jitsi-meet.
  76. *
  77. * @param {Object} event - The event in the format specified by
  78. * lib-jitsi-meet.
  79. * @returns {void}
  80. */
  81. sendEvent(event: IEvent) {
  82. if (this._shouldIgnore(event)) {
  83. return;
  84. }
  85. // @ts-ignore
  86. amplitude.getInstance().logEvent(this._extractName(event) ?? '', event);
  87. }
  88. /**
  89. * Return amplitude identity information.
  90. *
  91. * @returns {Object}
  92. */
  93. getIdentityProps() {
  94. if (navigator.product === 'ReactNative') {
  95. return {
  96. deviceId: this._deviceId,
  97. userId: this._userId
  98. };
  99. }
  100. return {
  101. sessionId: amplitude.getInstance().getSessionId(),
  102. // @ts-ignore
  103. deviceId: amplitude.getInstance().options.deviceId,
  104. // @ts-ignore
  105. userId: amplitude.getInstance().options.userId
  106. };
  107. }
  108. }