3

I need to configure the mixpanel in my work and for that I created a test project to understand how to configure the mixpanel SDK.

I followed the basic examples in the documentation. https://github.com/davodesign84/react-native-mixpanel

But I'm having a problem: 'null is not an object (evaluating RNMixpnael.sharedInstanceWithToken)'.

this is my code:

import React from 'react';
import {
  SafeAreaView,
  Text,
  StatusBar,
} from 'react-native';

import Mixpanel from 'react-native-mixpanel';

Mixpanel.sharedInstanceWithToken("MY_PROJECT_TOKEN");

const App: () => React$Node = () => {
  return (
    <>
      <StatusBar barStyle="dark-content" />
      <SafeAreaView>
       <Text>Testing Mixpanel SDK</Text>
      </SafeAreaView>
    </>
  );
};

export default App;

And the error:

enter image description here

Could someone help me understand what I'm doing wrong?

  • Is this issue only for Android? Have you tried iOS? – Evanss Nov 06 '20 at 16:49
  • Have you run pod install https://github.com/davodesign84/react-native-mixpanel#autolinking-and-rn--060? What is the version of the react-native library in your project? – Roni Castro Nov 06 '20 at 23:38
  • You should make sure "MY_PROJECT_TOKEN" is not null and have an actual value – Nooruddin Lakhani Nov 07 '20 at 07:54
  • @NooruddinLakhani I tried hard coding the value but I still get the issue. It also works fine on iOS. – Evanss Nov 08 '20 at 17:08
  • whats the react native version? >=0.60 will have auto linking otherwise you need to link the library. The error says Mixpanel instance is null, not an issue with token – Wolverine Nov 09 '20 at 04:20
  • @Wolverine react-native is 0.63.3. – Evanss Nov 09 '20 at 09:16
  • @RoniCastro react-native-mixpanel is ^1.2.3. The issue is only with Android so I dont think pod install will fix this. – Evanss Nov 09 '20 at 09:27
  • Have you tried doing this Firebase step "Additional info for Android (version >= 1.1.2)" https://github.com/davodesign84/react-native-mixpanel#additional-info-for-android-version--112? Apparently it is necessary in your version. I have in my project push notification from mixpanel, but I am not 100% sure it is necessary everything if you don't use it, but I would give a try in adding at least the `implementation 'com.google.firebase:firebase-messaging:20.2.1'` and related code in build.gradle and the service. – Roni Castro Nov 09 '20 at 12:43

2 Answers2

0

What worked for me was adding this line to MainApplication.java

import com.kevinejohn.RNMixpanel.RNMixpanel;
Evanss
  • 17,152
  • 66
  • 217
  • 397
0

In react-native versions newer than 0.60 auto-linking should work. All you have to do is:

  1. install react-native-mixpanel using npm or yarn

  2. add pod 'Mixpanel' to your Podfile, initialize it with the token

    More info here: https://mixpanel.com/help/reference/ios

  3. run pod install inside ios folder <-- usually missing this or not restarting the JS server is the cause

  4. import it in your RN codd: import Mixpanel from 'react-native-mixpanel'; as you did

If the null error still shows up just make sure JS server is stopped, remove Xcode derived data, clean cache (npx watchman watch-del-all && yarn start --reset-cache) and rebuild. It should work.

Florin Dobre
  • 8,025
  • 2
  • 45
  • 68
  • Whats the difference between "import com.kevinejohn.RNMixpanel.*;" and "import com.kevinejohn.RNMixpanel.RNMixpanel" ? – Evanss Nov 13 '20 at 10:54
  • @Evanss the idea in my answer is that the pod needs to be installed. I updated my answer. The question is for iOS, not for android. Thank you for your comment. It made me aware that I added an Android import instead of an iOS import (step 2) and I corrected that line. – Florin Dobre Nov 13 '20 at 11:08