Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AmplitudeHandler.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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
  16. * by the Amplitude API.
  17. */
  18. constructor(options: any) {
  19. super(options);
  20. const { amplitudeAPPKey, user } = options;
  21. this._enabled = true;
  22. const onError = (e: Error) => {
  23. logger.error('Error initializing Amplitude', e);
  24. this._enabled = false;
  25. };
  26. if (navigator.product === 'ReactNative') {
  27. amplitude.getInstance().init(amplitudeAPPKey);
  28. fixDeviceID(amplitude.getInstance()).then(() => {
  29. amplitude.getInstance().getDeviceId()
  30. // @ts-ignore
  31. .then((deviceId: string) => {
  32. this._deviceId = deviceId;
  33. });
  34. });
  35. } else {
  36. const amplitudeOptions: any = {
  37. includeReferrer: true,
  38. onError
  39. };
  40. // @ts-ignore
  41. amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
  42. fixDeviceID(amplitude.getInstance());
  43. }
  44. if (user) {
  45. this._userId = user;
  46. amplitude.getInstance().setUserId(user);
  47. }
  48. }
  49. /**
  50. * Sets the Amplitude user properties.
  51. *
  52. * @param {Object} userProps - The user portperties.
  53. * @returns {void}
  54. */
  55. setUserProperties(userProps: any) {
  56. if (this._enabled) {
  57. amplitude.getInstance().setUserProperties(userProps);
  58. }
  59. }
  60. /**
  61. * Sends an event to Amplitude. The format of the event is described
  62. * in AnalyticsAdapter in lib-jitsi-meet.
  63. *
  64. * @param {Object} event - The event in the format specified by
  65. * lib-jitsi-meet.
  66. * @returns {void}
  67. */
  68. sendEvent(event: IEvent) {
  69. if (this._shouldIgnore(event)) {
  70. return;
  71. }
  72. // @ts-ignore
  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. // @ts-ignore
  90. deviceId: amplitude.getInstance().options.deviceId,
  91. // @ts-ignore
  92. userId: amplitude.getInstance().options.userId
  93. };
  94. }
  95. }