Description
React Native wrapper for Mixpanel library, on top of the regular javascript sdk you can normally use, this provides you all the goodies of the native wrapper including notifications, analysis of the operating system, surveys etc..
If you'd like to support, you can donate some Ether to this address: 0x4cD5D72FFd950260e47F9e14F45811C5cCDD0283
Installation
- Run
npm install react-native-mixpanel --save
- Run
react-native link react-native-mixpanel
- (for RN 0.29.1+; otherwise use
rnpm link react-native-mixpanel
)
Additional Step for iOS
- Install Mixpanel iOS SDK via either Cocoapods or manually more info here
Additional info for Android (version >= 1.1.2)
From version 1.1.2 module uses Mixpanel SDK >= 5.6.0 that requires FCM
- Migration steps can be found here
- Allow sub-classes to override push notifications payload and Support when more than one push provider is used more info here
Autolinking and RN >= 0.60
Autolinking should work out of the box.
Remember to do: pod install.
Manual Installation
Installation iOS
npm install react-native-mixpanel --save
- Install Mixpanel iOS SDK via either Cocoapods or manually more info here
- In XCode, in the project navigator, right click
Libraries
➜ Add Files to [your project's name]
- Go to
node_modules
➜ react-native-mixpanel
and add RNMixpanel.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNMixpanel.a
to your project's Build Phases
➜ Link Binary With Libraries
- Click
RNMixpanel.xcodeproj
in the project navigator and go the Build Settings
tab. Make sure 'All' is toggled on (instead of 'Basic'). Look for Header Search Paths
and make sure it contains both $(SRCROOT)/../react-native/React
and $(SRCROOT)/../../React
- mark both as recursive
. - Run your project (
Cmd+R
)
Installation Android
- In
android/setting.gradle
...
include ':react-native-mixpanel', ':app'
project(':react-native-mixpanel').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-mixpanel/android')
- In
android/app/build.gradle
...
dependencies {
...
compile project(':react-native-mixpanel')
}
- register module (in MainActivity.java)
On newer versions of React Native register module (MainApplication.java):
import com.kevinejohn.RNMixpanel.*;
public class MainActivity extends ReactActivity {
......
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new RNMixpanel(),
new MainReactPackage());
}
}
Usage
var Mixpanel = require('react-native-mixpanel');
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN);
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false);
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true);
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true, true);
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true, true, null);
Mixpanel.optInTracking();
Mixpanel.optOutTracking();
Mixpanel.track("Event name");
Mixpanel.trackWithProperties('Click Button', {button_type: 'yellow button', button_text: 'magic button'});
Mixpanel.createAlias(UNIQUE_ID)
Mixpanel.createAlias('new_id', 'existing_id');
...
Mixpanel.createAlias('newer_id', 'new_id');
Mixpanel.identify(UNIQUE_ID)
Mixpanel.set({"$email": "elvis@email.com"});
Mixpanel.setOnce({"$email": "elvis@email.com", "Created": new Date().toISOString()});
Mixpanel.setGroup('company', 'mixpanel');
Mixpanel.setGroup('company', 'mixpanel');
Mixpanel.timeEvent("Image Upload");
Mixpanel.track("Image Upload");
Mixpanel.registerSuperProperties({"Account type": "Free", "User Type": "Vendor"});
Mixpanel.registerSuperPropertiesOnce({"Gender": "Female"});
Mixpanel.trackCharge(399);
Mixpanel.trackChargeWithProperties(399, {"product": "ACME Wearable tech"});
Mixpanel.increment("Login Count", 1);
Mixpanel.append("Lines", ["Simple", "Dashed"]);
Mixpanel.union("Lines", ["Dashed", "Custom"]);
Mixpanel.getPushRegistrationId();
Mixpanel.setPushRegistrationId("GCM/FCM push token");
Mixpanel.initPushHandling(YOUR_12_DIGIT_GOOGLE_SENDER_ID);
Mixpabel.clearSuperProperties();
Mixpanel.clearPushRegistrationId(token?: string);
Mixpanel.addPushDeviceToken("APNS push token");
Mixpanel.removePushDeviceToken(pushDeviceToken: string);
Mixpanel.removeAllPushDeviceTokens();
Mixpanel.setAppSessionPropertiesIOS({
minimumSessionDuration: 60,
maximumSessionDuration: 10,
});
Mixpanel.reset();
Mixpanel.getDistinctId(function(id){})
Displaying in-app messages
By default, in-app messages are shown to users when the app starts and a message is available to display
This behaviour can be disabled by default, and explicitally triggered at a later time (e.g. after your loading sequence)
For iOS, in your app delegate, add the following line:
// In application:didFinishLaunchingWithOptions:
Mixpanel *mixpanel = [Mixpanel sharedInstanceWithToken:YOUR_MIXPANEL_TOKEN];
// Turn this off so the message doesn't pop up automatically.
mixpanel.showNotificationOnActive = NO;
For Android, add the following to your app mainifest in the <application>
tag:
<meta-data android:name="com.mixpanel.android.MPConfig.AutoShowMixpanelUpdates" android:value="false" />
You can then call the following in your react native application:
Mixpanel.showInAppMessageIfAvailable();
More info: https://developer.mixpanel.com/docs/android-inapp-messages
Configure mixpanel urls
Add server url in .plist
files in iOS project.
<key>com.mixpanel.config.serverURL</key>
<string>https://api-eu.mixpanel.com</string>
Add endpoints to manifest
in your Android project.
<application ...>
<meta-data android:name="com.mixpanel.android.MPConfig.EventsEndpoint"
android:value="https://api-eu.mixpanel.com/track" />
<meta-data android:name="com.mixpanel.android.MPConfig.PeopleEndpoint"
android:value="https://api-eu.mixpanel.com/engage" />
<meta-data android:name="com.mixpanel.android.MPConfig.GroupsEndpoint"
android:value="https://api-eu.mixpanel.com/groups" />
</application>
Notes
For more info please have a look at the official Mixpanel reference for iOS