From f39685c085a38d27f37cc01d0a68324c13dc94db Mon Sep 17 00:00:00 2001 From: Zihe Jia Date: Mon, 15 Apr 2024 12:13:35 -0700 Subject: [PATCH 1/3] should use the correct object to initialize Mixpanel --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 902f19f4..2c82464e 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, @@ -112,7 +112,7 @@ export class Mixpanel { trackAutomaticEvents, optOutTrackingDefault = DEFAULT_OPT_OUT ) { - await MixpanelReactNative.initialize( + await this.mixpanelImpl.initialize( token, trackAutomaticEvents, optOutTrackingDefault, From 91e4457f550e507cc094a3a83511d40506d0c437 Mon Sep 17 00:00:00 2001 From: Zihe Jia Date: Mon, 15 Apr 2024 18:00:36 -0700 Subject: [PATCH 2/3] avoid crash if AsyncStorage is null --- javascript/mixpanel-storage.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) 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]; + } +} From 53b39c2b18a118c9ff653db565234aac3951d0c4 Mon Sep 17 00:00:00 2001 From: Zihe Jia Date: Mon, 15 Apr 2024 20:59:54 -0700 Subject: [PATCH 3/3] fix tests --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 2c82464e..4fc0dd6f 100644 --- a/index.js +++ b/index.js @@ -112,7 +112,7 @@ export class Mixpanel { trackAutomaticEvents, optOutTrackingDefault = DEFAULT_OPT_OUT ) { - await this.mixpanelImpl.initialize( + await MixpanelReactNative.initialize( token, trackAutomaticEvents, optOutTrackingDefault,