Mixpanel released the offical RN library: https://github.com/mixpanel/mixpanel-react-native However I will try to keep this version updated as long as their library will not have all the possibilities available here.
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..
This repo is for of https://github.com/davodesign84/react-native-mixpanel
- 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
)
- (for RN 0.29.1+; otherwise use
- Install Mixpanel iOS SDK via either Cocoapods or manually more info here
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 should work out of the box.
Remember to do: pod install.
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 addRNMixpanel.xcodeproj
- In XCode, in the project navigator, select your project. Add
libRNMixpanel.a
to your project'sBuild Phases
➜Link Binary With Libraries
- Click
RNMixpanel.xcodeproj
in the project navigator and go theBuild Settings
tab. Make sure 'All' is toggled on (instead of 'Basic'). Look forHeader Search Paths
and make sure it contains both$(SRCROOT)/../react-native/React
and$(SRCROOT)/../../React
- mark both asrecursive
. - Run your project (
Cmd+R
)
- 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.*; // <--- import
public class MainActivity extends ReactActivity {
......
/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new RNMixpanel(), // <------ add here
new MainReactPackage());
}
}
// Require the module
var Mixpanel = require('react-native-mixpanel');
// Init Mixpanel SDK with your project token
// @param apiToken - your project token
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN);
// You can also opt out tracking by default (GDPR)
// @param apiToken - your project token
// @param optOutTrackingByDefault - whether or not to be opted out from tracking by default (default value: false)
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false);
// You can also disable trackCrashes
// @param apiToken - your project token
// @param optOutTrackingByDefault - whether or not to be opted out from tracking by default (default value: false)
// @param trackCrashes (iOS only!) - whether or not to track crashes in Mixpanel. may want to disable if you're seeing issues with your crash reporting for either signals or exceptions (default value: true)
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true);
// You can also disable automaticPushTracking
// @param apiToken - your project token
// @param optOutTrackingByDefault - whether or not to be opted out from tracking by default (default value: false)
// @param trackCrashes (iOS only!) - whether or not to track crashes in Mixpanel. may want to disable if you're seeing issues with your crash reporting for either signals or exceptions (default value: true)
// @param automaticPushTracking (iOS only!) - whether or not to automatically track pushes sent from Mixpanel (default value: true)
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true, true);
// You can also pass launchOptions
// @param apiToken - your project token
// @param optOutTrackingByDefault - whether or not to be opted out from tracking by default (default value: false)
// @param trackCrashes (iOS only!) - whether or not to track crashes in Mixpanel. may want to disable if you're seeing issues with your crash reporting for either signals or exceptions (default value: true)
// @param automaticPushTracking (iOS only!) - whether or not to automatically track pushes sent from Mixpanel (default value: true)
// @param launchOptions (iOS only!) - your application delegate's launchOptions (default value: null)
Mixpanel.sharedInstanceWithToken(YOUR_PROJECT_TOKEN, false, true, true, null);
// Opt in tracking.
// Use this method to opt in an already opted out user from tracking. People updates and track calls will be sent to Mixpanel after using this method.
Mixpanel.optInTracking();
// Opt out tracking.
// This method is used to opt out tracking. This causes all events and people request no longer to be sent back to the Mixpanel server.
Mixpanel.optOutTracking();
// Send and event name with no properties
Mixpanel.track("Event name");
// Track event with properties
Mixpanel.trackWithProperties('Click Button', {button_type: 'yellow button', button_text: 'magic button'});
// Create Alias from unique id, i.e. create a new mixpanel profile: to call when a user signs up, with a unique id that is not used by another mixpanel profile as param
Mixpanel.createAlias(UNIQUE_ID)
// OR:
// Create an alias, which Mixpanel will use to link two distinct_ids going forward (not retroactively). Multiple aliases can map to the same original ID, but not vice-versa.
// Aliases can also be chained - the following is a valid scenario:
Mixpanel.createAlias('new_id', 'existing_id');
...
Mixpanel.createAlias('newer_id', 'new_id');
// If the original ID is not passed in, we will use the current distinct_id - probably the auto-generated GUID.
// Notes:
// The best practice is to call createAlias() when a unique ID is first created for a user (e.g., when a user first registers for
// an account and provides an email address). createAlias() should never be called more than once for a given user, except to chain
// a newer ID to a previously new ID, as described above.
// More info about createAlias: https://developer.mixpanel.com/docs/javascript-full-api-reference#section-mixpanel-alias
// Identify, i.e. associate to an existing mixpanel profile: to call when a user logs in and is already registered in Mixpanel with this unique id
Mixpanel.identify(UNIQUE_ID)
// Set People properties (warning: if no mixpanel profile has been assigned to the current user when this method is called, it will automatically create a new mixpanel profile and the user will no longer be anonymous in Mixpanel)
Mixpanel.set({"$email": "elvis@email.com"});
// Set People Properties Once (warning: if no mixpanel profile has been assigned to the current user when this method is called, it will automatically create a new mixpanel profile and the user will no longer be anonymous in Mixpanel)
Mixpanel.setOnce({"$email": "elvis@email.com", "Created": new Date().toISOString()});
// Add a new Group for this user.
// @param Group key
// @param A valid Mixpanel property type
Mixpanel.addGroup('company', 'mixpanel');
// Register the current user into one Group. The Group must be added before setting
// @param Group key
// @param a singular group ID
Mixpanel.setGroup('company', 'mixpanel');
// Timing Events
// Sets the start time for an action, for example uploading an image
Mixpanel.timeEvent("Image Upload");
// to be followed by a tracking event to define the end time
Mixpanel.track("Image Upload");
// Register super properties
Mixpanel.registerSuperProperties({"Account type": "Free", "User Type": "Vendor"});
// Register super properties Once
Mixpanel.registerSuperPropertiesOnce({"Gender": "Female"});
// track Revenue
Mixpanel.trackCharge(399);
// track with properties
Mixpanel.trackChargeWithProperties(399, {"product": "ACME Wearable tech"});
// increment property
Mixpanel.increment("Login Count", 1);
// Append array to a list property
Mixpanel.append("Lines", ["Simple", "Dashed"]);
// Merge array to a list property, excluding duplicate values
Mixpanel.union("Lines", ["Dashed", "Custom"]);
// Android
// Retrieves current Firebase Cloud Messaging token.
// Should only be called after identify(String) has been called.
Mixpanel.getPushRegistrationId();
// send push notifications token to Mixpanel
// Android
Mixpanel.setPushRegistrationId("GCM/FCM push token");
// Android
// tell Mixpanel which user record in People Analytics should receive the messages when they are sent from the Mixpanel app,
// make sure you call this right after you call `identify`
// Deprecated.
// in 5.5.0. Google Cloud Messaging (GCM) is now deprecated by Google. To enable end-to-end Firebase Cloud Messaging (FCM) from Mixpanel you only need to add the following to your application manifest XML file:
// <service
// android:name="com.mixpanel.android.mpmetrics.MixpanelFCMMessagingService"
// android:enabled="true"
// android:exported="false">
// <intent-filter>
// <action android:name="com.google.firebase.MESSAGING_EVENT"/>
// </intent-filter>
// </service>
// Make sure you have a valid google-services.json file in your project and firebase messaging is included as a dependency. Example:
// buildscript {
// ...
// dependencies {
// classpath 'com.google.gms:google-services:4.1.0'
// ...
// }
// }
// dependencies {
// implementation 'com.google.firebase:firebase-messaging:17.3.4'
// implementation 'com.mixpanel.android:mixpanel-android:5.5.0'
// }
// apply plugin: 'com.google.gms.google-services'
// We recommend you update your Server Key on mixpanel.com from your Firebase console. Legacy server keys are still supported.
Mixpanel.initPushHandling(YOUR_12_DIGIT_GOOGLE_SENDER_ID);
// Allows to clear super properites
Mixpabel.clearSuperProperties();
// Android
// Unregister an android device for push notifications
// With token it will clear a single Firebase Cloud Messaging token from Mixpanel.
// Without token it will clear all current Firebase Cloud Messaging tokens from Mixpanel.
Mixpanel.clearPushRegistrationId(token?: string);
// iOS
Mixpanel.addPushDeviceToken("APNS push token");
// iOS - unregister iOS device, pushDeviceToken = APNS push token
Mixpanel.removePushDeviceToken(pushDeviceToken: string);
// iOS
// Unregister the given device to receive push notifications.
Mixpanel.removeAllPushDeviceTokens();
// Mixpanel reset method (warning: by default it will also generate a new unique id and call the identify method with it. Thus, the user will not be anonymous in Mixpanel.)
// @param flushOnReset - will flush data on reset (default value: true)
// @param autoGenerateNewUniqueId - determines if new unique id should be generated automaticaly or not (default value: true)
Mixpanel.reset();
Mixpanel.reset(false);
Mixpanel.reset(true, false);
// get the last distinct id set with identify or, if identify hasn't been
// called, the default mixpanel id for this device.
Mixpanel.getDistinctId(function(id){})
// iOS
// Allows control of the min/max sessions
Mixpanel.setAppSessionPropertiesIOS({
minimumSessionDuration: 60,
maximumSessionDuration: 10,
});
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 (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:
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
Mixpanel’s client-side libraries send user location data (city, region, country) as Properties by default. Mixpanel pulls the user’s IP address and runs it through a third-party IP geolocator (MaxMind). MaxMind then returns city, region, and country, and Mixpanel sets those as Properties. Then, the IP address is discarded - Mixpanel does not store users’ IPs.
In order to disable or anonymize geolocation data (i.e., not send city, region, and country by default when using a client-side library), you simply need to stop collecting the user’s IP, which will then not populate the city, region, and country properties. At this time, there is no way to selectively disable one or two of the three location default properties. However, you could disable the three default properties and then set your own custom property for the one or two that you’d still like to collect.
For iOS, in your app delegate, add the following line:
// In application:didFinishLaunchingWithOptions:
Mixpanel *mixpanel = [Mixpanel sharedInstanceWithToken:YOUR_MIXPANEL_TOKEN];
// To disable IP collection on iOS, make a small change in the Mixpanel library and set:
mixpanel.useIPAddressForGeoLocation = NO;
OR invoke after initialization (remember it's iOS only method that will thorw an error on Android):
Mixpanel.disableIpAddressGeolocalization();
For Android, add the following to your app mainifest in the <application>
tag:
<meta-data android:name="com.mixpanel.android.MPConfig.UseIpAddressForGeolocation" android:value="false" />
More info: https://help.mixpanel.com/hc/en-us/articles/115004494803-Disable-Geolocation-Collection
Before migrating or using EU api endpoints read this: https://help.mixpanel.com/hc/en-us/articles/360039135652-Data-Residency-in-EU
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>
For more info please have a look at the official Mixpanel reference for iOS