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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import logger from '../logger';
  2. import AbstractHandler 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. /**
  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, user } = options;
  19. this._enabled = true;
  20. const onError = e => {
  21. logger.error('Error initializing Amplitude', e);
  22. this._enabled = false;
  23. };
  24. if (navigator.product === 'ReactNative') {
  25. amplitude.getInstance().init(amplitudeAPPKey);
  26. fixDeviceID(amplitude.getInstance()).then(() => {
  27. amplitude.getInstance().getDeviceId()
  28. .then(deviceId => {
  29. this._deviceId = deviceId;
  30. });
  31. });
  32. } else {
  33. const amplitudeOptions = {
  34. includeReferrer: true,
  35. onError
  36. };
  37. amplitude.getInstance().init(amplitudeAPPKey, undefined, amplitudeOptions);
  38. fixDeviceID(amplitude.getInstance());
  39. }
  40. if (user) {
  41. this._userId = user;
  42. amplitude.getInstance().setUserId(user);
  43. }
  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. amplitude.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. amplitude.getInstance().logEvent(this._extractName(event), event);
  69. }
  70. /**
  71. * Return amplitude identity information.
  72. *
  73. * @returns {Object}
  74. */
  75. getIdentityProps() {
  76. if (navigator.product === 'ReactNative') {
  77. return {
  78. deviceId: this._deviceId,
  79. userId: this._userId
  80. };
  81. }
  82. return {
  83. sessionId: amplitude.getInstance().getSessionId(),
  84. deviceId: amplitude.getInstance().options.deviceId,
  85. userId: amplitude.getInstance().options.userId
  86. };
  87. }
  88. }