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

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