Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

Amplitude.native.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { NativeModules } from 'react-native';
  2. const { Amplitude: AmplitudeNative } = NativeModules;
  3. /**
  4. * Wrapper for the Amplitude native module.
  5. */
  6. class Amplitude {
  7. /**
  8. * Create new Amplitude instance.
  9. *
  10. * @param {string} instanceName - The name of the Amplitude instance. Should
  11. * be used only for multi-project logging.
  12. */
  13. constructor(instanceName) {
  14. // It might not have been included in the build.
  15. if (!AmplitudeNative) {
  16. throw new Error('Amplitude analytics is not supported');
  17. }
  18. this._instanceName = instanceName;
  19. }
  20. /**
  21. * Initializes the Amplitude SDK.
  22. *
  23. * @param {string} apiKey - The API_KEY of the Amplitude project.
  24. * @returns {void}
  25. */
  26. init(apiKey) {
  27. AmplitudeNative.init(this._instanceName, apiKey);
  28. }
  29. /**
  30. * Sets an identifier for the current user.
  31. *
  32. * @param {string} userId - The new user id.
  33. * @returns {void}
  34. */
  35. setUserId(userId) {
  36. AmplitudeNative.setUserId(this._instanceName, userId);
  37. }
  38. /**
  39. * Sets user properties for the current user.
  40. *
  41. * @param {Object} userProperties - The user properties to be set.
  42. * @returns {void}
  43. */
  44. setUserProperties(userProperties) {
  45. AmplitudeNative.setUserProperties(this._instanceName, userProperties);
  46. }
  47. /**
  48. * Log an event with eventType and eventProperties.
  49. *
  50. * @param {string} eventType - The type of the event.
  51. * @param {Object} eventProperties - The properties of the event.
  52. * @returns {void}
  53. */
  54. logEvent(eventType, eventProperties) {
  55. // The event properties are converted to JSON string because of known
  56. // performance issue when passing objects trough the RN bridge too
  57. // often (a few times a second).
  58. AmplitudeNative.logEvent(
  59. this._instanceName, eventType, JSON.stringify(eventProperties));
  60. }
  61. }
  62. /**
  63. * Cache of <tt>Amplitude</tt> instances by instanceName.
  64. */
  65. const instances = {};
  66. /**
  67. * The default (with instanceName - undefined) <tt>Amplitude</tt> instance.
  68. */
  69. let defaultInstance;
  70. export default {
  71. /**
  72. * Returns a <tt>Amplitude</tt> instance.
  73. *
  74. * @param {Object} options - Optional parameters.
  75. * @param {string} options.host - The host property from the current URL.
  76. * @param {string|undefined} options.instanceName - The name of the
  77. * amplitude instance. Should be used only for multi-project logging.
  78. * @returns {Amplitude}
  79. */
  80. getInstance(options = {}) {
  81. let instance;
  82. const { host = '', instanceName = '' } = options;
  83. let internalInstanceName = host;
  84. if (instanceName !== '') {
  85. internalInstanceName += `-${instanceName}`;
  86. }
  87. if (internalInstanceName === '') {
  88. instance = defaultInstance = defaultInstance || new Amplitude();
  89. } else {
  90. instance = instances[internalInstanceName]
  91. = instances[internalInstanceName]
  92. || new Amplitude(internalInstanceName);
  93. }
  94. return instance;
  95. }
  96. };