diff --git a/index.js b/index.js index 902f19f4..4fc0dd6f 100644 --- a/index.js +++ b/index.js @@ -82,7 +82,7 @@ export class Mixpanel { superProperties = {}, serverURL = "https://api.mixpanel.com" ) { - await MixpanelReactNative.initialize( + await this.mixpanelImpl.initialize( this.token, this.trackAutomaticEvents, optOutTrackingDefault, diff --git a/javascript/mixpanel-storage.js b/javascript/mixpanel-storage.js index 9061923a..52dddb99 100644 --- a/javascript/mixpanel-storage.js +++ b/javascript/mixpanel-storage.js @@ -3,7 +3,15 @@ import {MixpanelLogger} from "mixpanel-react-native/javascript/mixpanel-logger"; export class AsyncStorageAdapter { constructor(storage) { if (!storage) { - this.storage = require("@react-native-async-storage/async-storage"); + try { + this.storage = require("@react-native-async-storage/async-storage"); + } catch { + console.error( + "[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null. Please run 'npm install @react-native-async-storage/async-storage' or follow the Mixpanel guide to set up your own Storage class." + ); + console.error("[Mixpanel] Falling back to in-memory storage"); + this.storage = new InMemoryStorage(); + } } else { this.storage = storage; } @@ -34,3 +42,21 @@ export class AsyncStorageAdapter { } } } + +class InMemoryStorage { + constructor() { + this.store = {}; + } + + async getItem(key) { + return this.store.hasOwnProperty(key) ? this.store[key] : null; + } + + async setItem(key, value) { + this.store[key] = value; + } + + async removeItem(key) { + delete this.store[key]; + } +}