mixpanel / mixpanel-react-native

Official React Native Tracking Library for Mixpanel Analytics
https://mixpanel.com
Apache License 2.0
105 stars 44 forks source link

v3.0.1 Fails on Initialization #233

Closed kingdavidmartins closed 5 months ago

kingdavidmartins commented 5 months ago

Keep getting the following

[@RNC/AsyncStorage]: NativeModule: AsyncStorage is null.

To fix this issue try these steps:

  • Uninstall, rebuild and restart the app.

  • Run the packager with \`--reset-cache\` flag.

  • If you are using CocoaPods on iOS, run \`pod install\` in the \`ios\` directory, then rebuild and re-run the app.

  • Make sure your project's \`package.json\` depends on \`@react-native-async-storage/async-storage\`, even if you only depend on it indirectly through other dependencies. CLI only autolinks native modules found in your \`package.json\`.

  • If this happens while testing with Jest, check out how to integrate AsyncStorage here: https://react-native-async-storage.github.io/async-storage/docs/advanced/jest

If none of these fix the issue, please open an issue on the GitHub repository: https://github.com/react-native-async-storage/async-storage/issues

even when useNative is set to true.

When useNative is set to false and I pass custom storage class. It also registers as undefined/null leading it to fallback to async storage which I do not have installed causing the error to be thrown

I made sure to make class resemble mixpanel-storage.js https://github.com/mixpanel/mixpanel-react-native/blob/a68a0280f489bd8fb1160da4bb9fa938302ceb1d/javascript/mixpanel-storage.js#L3-L36

and passed it in like so using Expo Secure Storage. Which also failed

class MixpanelExpoSecureStorageAdapter {
  async getItem(key: string) {
    try {
      const token = await SecureStore.getItemAsync(key);
      return token;
    } catch (err) {
      return undefined;
    }
  }

  async setItem(key: string, value: string) {
    try {
      await SecureStore.setItemAsync(key, value);
      return;
    } catch (err) {
      return undefined;
    }
  }

  async removeItem(key: string) {
    try {
      await SecureStore.deleteItemAsync(key);
      return;
    } catch (err) {
      return undefined;
    }
  }
}

const trackAutomaticEvents = false;
const useNative = false;

const mixpanelInstance = new Mixpanel(
  `redacted`,
   trackAutomaticEvents,
   useNative,
   MixpanelExpoSecureStorageAdapter
);

Had to create a patch where I replaced https://github.com/mixpanel/mixpanel-react-native/blob/a68a0280f489bd8fb1160da4bb9fa938302ceb1d/javascript/mixpanel-storage.js#L6

with imports for expo secure storage as well as updating respective apis to custom class I created to begin with which worked.

Patch was needed to use custom storage or to set useNative to True. I couldn't use native sdk without 1st resolving async storage issue.

Assuming if I installed async storage I wouldn't have had this issue. But if we pass custom storage class shouldn't still be required. Shouldn't also be a blocker for useNative true

Please resolve

zihejia commented 5 months ago

hi @kingdavidmartins , sorry about the convenience, the issue has been fixed in 3.0.2. I'm closing this one now, feel free to reopen if the problem still exists.

alexrequelme commented 4 months ago

Why i have this error if i use useNative = true?

new Mixpanel(MIXPANEL_TOKEN, true, true)
image
zihejia commented 4 months ago

@alexrequelme , This might mean your native environment is not being properly configured for Mixpanel so it falls back to Javascript mode. Have you seen this warning? "MixpanelReactNative is not available; using JavaScript mode. If you prefer not to use the JavaScript mode, please follow the guide in the GitHub repository: https://github.com/mixpanel/mixpanel-react-native." If you share more information regarding which platform you are testing and steps to reproduce this, that would be great.

alexrequelme commented 4 months ago

Mmm, I'm using expo development build, but all works fine on version 2.4.1, when i upgraded i face this issue.

zihejia commented 4 months ago

@alexrequelme , if you run npm install @react-native-async-storage/async-storage will it make the error go away?

alexrequelme commented 4 months ago

I don't use AsyncStorage, I reverted the package back to version 2.4.1 and it's functioning perfectly, so I'll be sticking with this version for the time being.