Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

AmplitudeHandler.ts 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 -
  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. if (navigator.product === 'ReactNative') {
  32. amplitude.getInstance().init(amplitudeAPPKey);
  33. fixDeviceID(amplitude.getInstance()).then(() => {
  34. amplitude.getInstance().getDeviceId()
  35. // @ts-ignore
  36. .then((deviceId: string) => {
  37. this._deviceId = deviceId;
  38. });
  39. });
  40. } else {
  41. const amplitudeOptions: any = {
  42. includeReferrer: true,
  43. includeUtm,
  44. saveParamsReferrerOncePerSession: false,
  45. onError
  46. };
  47. // @ts-ignore
  48. amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
  49. fixDeviceID(amplitude.getInstance());
  50. }
  51. if (user) {
  52. this._userId = user;
  53. amplitude.getInstance().setUserId(user);
  54. }
  55. }
  56. /**
  57. * Sets the Amplitude user properties.
  58. *
  59. * @param {Object} userProps - The user portperties.
  60. * @returns {void}
  61. */
  62. setUserProperties(userProps: any) {
  63. if (this._enabled) {
  64. amplitude.getInstance().setUserProperties(userProps);
  65. }
  66. }
  67. /**
  68. * Sends an event to Amplitude. The format of the event is described
  69. * in AnalyticsAdapter in lib-jitsi-meet.
  70. *
  71. * @param {Object} event - The event in the format specified by
  72. * lib-jitsi-meet.
  73. * @returns {void}
  74. */
  75. sendEvent(event: IEvent) {
  76. if (this._shouldIgnore(event)) {
  77. return;
  78. }
  79. // @ts-ignore
  80. amplitude.getInstance().logEvent(this._extractName(event) ?? '', event);
  81. }
  82. /**
  83. * Return amplitude identity information.
  84. *
  85. * @returns {Object}
  86. */
  87. getIdentityProps() {
  88. if (navigator.product === 'ReactNative') {
  89. return {
  90. deviceId: this._deviceId,
  91. userId: this._userId
  92. };
  93. }
  94. return {
  95. sessionId: amplitude.getInstance().getSessionId(),
  96. // @ts-ignore
  97. deviceId: amplitude.getInstance().options.deviceId,
  98. // @ts-ignore
  99. userId: amplitude.getInstance().options.userId
  100. };
  101. }
  102. }