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.

Amplitude.native.js 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 user properties for the current user.
  31. *
  32. * @param {Object} userProperties - The user properties to be set.
  33. * @returns {void}
  34. */
  35. setUserProperties(userProperties) {
  36. AmplitudeNative.setUserProperties(this._instanceName, userProperties);
  37. }
  38. /**
  39. * Log an event with eventType and eventProperties.
  40. *
  41. * @param {string} eventType - The type of the event.
  42. * @param {Object} eventProperties - The properties of the event.
  43. * @returns {void}
  44. */
  45. logEvent(eventType, eventProperties) {
  46. // The event properties are converted to JSON string because of known
  47. // performance issue when passing objects trough the RN bridge too
  48. // often (a few times a second).
  49. AmplitudeNative.logEvent(
  50. this._instanceName, eventType, JSON.stringify(eventProperties));
  51. }
  52. }
  53. /**
  54. * Cache of <tt>Amplitude</tt> instances by instanceName.
  55. */
  56. const instances = {};
  57. /**
  58. * The default (with instanceName - undefined) <tt>Amplitude</tt> instance.
  59. */
  60. let defaultInstance;
  61. export default {
  62. /**
  63. * Returns a <tt>Amplitude</tt> instance.
  64. *
  65. * @param {Object} options - Optional parameters.
  66. * @param {string} options.host - The host property from the current URL.
  67. * @param {string|undefined} options.instanceName - The name of the
  68. * amplitude instance. Should be used only for multi-project logging.
  69. * @returns {Amplitude}
  70. */
  71. getInstance(options = {}) {
  72. let instance;
  73. const { host = '', instanceName = '' } = options;
  74. let internalInstanceName = host;
  75. if (instanceName !== '') {
  76. internalInstanceName += `-${instanceName}`;
  77. }
  78. if (internalInstanceName === '') {
  79. instance = defaultInstance = defaultInstance || new Amplitude();
  80. } else {
  81. instance = instances[internalInstanceName]
  82. = instances[internalInstanceName]
  83. || new Amplitude(internalInstanceName);
  84. }
  85. return instance;
  86. }
  87. };