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

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