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

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