Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mixpanel-react-native

mixpanel281.5kApache-2.03.0.9TypeScript support: included

Official React Native Tracking Library for Mixpanel Analytics

mixpanel, react, native, ios, android, analytics, tracking, sdk

readme

Mixpanel React Native Library

Table of Contents

Introduction

Welcome to the official Mixpanel React Native library. The Mixpanel React Native library is an open-source project, and we'd love to see your contributions! We'd also love for you to come and work with us! Check out Jobs for details.

Quick Start Guide

Mixpanel's React Native SDK is a wrapper around Mixpanel’s native iOS and Android SDKs and it supports offline tracking. Check out our official documentation for more in depth information on installing and using Mixpanel on React Native.

1. Install Mixpanel

Prerequisites

  • React Native v0.6+
  • Setup development environment for React Native

    Steps

  • Under your app's root directory, install Mixpanel React Native SDK.
    npm install mixpanel-react-native
  • Under your application's ios folder, run

    pod install

    Please note: You do not need to update your Podfile to add Mixpanel.

  • Since Xcode 12.5, there is a known swift compile issue, please refer to this workaround. However the compile issue has been resolved in Xcode 13.2.1+, there is no extra step required as long as you upgrade to Xcode 13.2.1+.

2. Initialize Mixpanel

To start tracking with the library you must first initialize with your project token. You can get your project token from project settings.

import { Mixpanel } from 'mixpanel-react-native';

const trackAutomaticEvents = false;
const mixpanel = new Mixpanel("Your Project Token", trackAutomaticEvents);
mixpanel.init();

Once you've called this method once, you can access mixpanel throughout the rest of your application.

3. Send Data

Let's get started by sending event data. You can send an event from anywhere in your application. Better understand user behavior by storing details that are specific to the event (properties). After initializing the library, Mixpanel will automatically track some properties by default. learn more

// Track with event-name
mixpanel.track('Sent Message');
// Track with event-name and property
mixpanel.track('Plan Selected', {'Plan': 'Premium'});

In addition to event data, you can also send user profile data. We recommend this after completing the quickstart guide.

4. Check for Success

Open up Events in Mixpanel to view incoming events. Once data hits our API, it generally takes ~60 seconds for it to be processed, stored, and queryable in your project.

Complete Code Example


import React from 'react';
import { Button, SafeAreaView } from "react-native";
import { Mixpanel } from 'mixpanel-react-native';

const trackAutomaticEvents = false;
const mixpanel = new Mixpanel("Your Project Token", trackAutomaticEvents);
mixpanel.init();

const SampleApp = () => {
  return (
    <SafeAreaView>
      <Button
        title="Select Premium Plan"
        onPress={() => mixpanel.track("Plan Selected", {"Plan": "Premium"})}
      />
    </SafeAreaView>
  );
}

export default SampleApp;

Expo and React Native for Web support (3.0.2 and above)

Starting from version 3.0.2, we have introduced support for Expo, React Native for Web, and other platforms utilizing React Native that do not support iOS and Android directly. To enable this feature,
Step 1:

npm install @react-native-async-storage/async-storage

When JavaScript mode is enabled, Mixpanel utilizes AsyncStorage to persist data. If you prefer not to use it, or if AsyncStorage is unavailable in your target environment, you can import or define a different storage class. However, it must follow a subset (see: MixpanelAsyncStorage) of the same interface as AsyncStorage The following example demonstrates how to use a custom storage solution:

// Optional: if you do not want to use the default AsyncStorage
const MyAsyncStorage = require("@my-org/<library-path>/AsyncStorage"); 
const trackAutomaticEvents = false;
const useNative = false;
const mixpanel = new Mixpanel('YOUR_TOKEN', trackAutomaticEvents, useNative, MyAsyncStorage);
mixpanel.init();


Step 2: Initialize Mixpanel with an additional parameter, useNative, set to false.

const trackAutomaticEvents = false;
const useNative = false;
const mixpanel = new Mixpanel(
    "YOUR_MIXPANEL_TOKEN",
    trackAutomaticEvents,
    useNative
  );

This will activate JavaScript mode.

👋 👋 Tell us about the Mixpanel developer experience! https://www.mixpanel.com/devnps 👍 👎

FAQ

I want to stop tracking an event/event property in Mixpanel. Is that possible?
Yes, in Lexicon, you can intercept and drop incoming events or properties. Mixpanel won’t store any new data for the event or property you select to drop. See this article for more information.

I have a test user I would like to opt out of tracking. How do I do that?
Mixpanel’s client-side tracking library contains the optOutTracking() method, which will set the user’s local opt-out state to “true” and will prevent data from being sent from a user’s device. More detailed instructions can be found in the section, Opting users out of tracking.

Why aren't my events showing up?
First, make sure your test device has internet access. To preserve battery life and customer bandwidth, the Mixpanel library doesn't send the events you record immediately. Instead, it sends batches to the Mixpanel servers every 60 seconds while your application is running, as well as when the application transitions to the background. You can call flush() manually if you want to force a flush at a particular moment.

mixpanel.flush();

If your events are still not showing up after 60 seconds, check if you have opted out of tracking. You can also enable Mixpanel debugging and logging, it allows you to see the debug output from the Mixpanel library. To enable it, call setLoggingEnabled with true, then run your iOS project with Xcode or android project with Android Studio. The logs should be available in the console.

mixpanel.setLoggingEnabled(true);

Starting with iOS 14.5, do I need to request the user’s permission through the AppTrackingTransparency framework to use Mixpanel?
No, Mixpanel does not use IDFA so it does not require user permission through the AppTrackingTransparency(ATT) framework.

If I use Mixpanel, how do I answer app privacy questions for the App Store?
Please refer to our Apple App Developer Privacy Guidance

I want to know more!

No worries, here are some links that you will find useful:

Have any questions? Reach out to Mixpanel Support to speak to someone smart, quickly.

changelog

#

v3.0.9 (2025-04-11)

Enhancements

  • Bump native SDKs, Swift 4.4.0 and Android 8.0.3 #285

Fixes

  • fix: custom storage #282

#

v3.0.8 (2024-11-21)

Fixes

  • encodeURIComponent body JSON data #277

#

v3.0.7 (2024-09-26)

Fixes

  • Revert the minSDK to 21 #271
  • revert android #267

#

v3.0.6 (2024-09-10)

Enhancements

  • bump iOS to 4.3.0 and Android minSDK to 23 #264
  • upgrade android sdk to 7.5.3 #263

Fixes

  • fix: opt in tracking issue during initialize #257

#

v3.0.5 (2024-05-14)

Fixes

  • fix: resolve import issue for MixpanelMain #253

#

v3.0.4 (2024-05-13)

Fixes

  • Improvement for supporting the custom storage #251
  • Avoid CORS errors #250

#

v3.0.3 (2024-05-10)

Enhancements

  • bump iOS to 4.2.7 #249
  • Require only the used subset of AsnycStorage as custom storage #246
  • Fix CI issues for both iOS and Android and update the sample app #241
  • Version bump Android SDK to 7.5.2 #237

Fixes

  • fix: fix adapter storage jest setup #244
  • fix storage adapter info being logged on every app launch #239
  • Added null check for empty constants object incase of web #238

#

v3.0.2 (2024-04-16)

Fixes

  • Avoid crash if AsyncStorage is null #235

#

v3.0.1 (2024-04-11)

Fixes

  • Replace the MixpanelReactNative with mixpanelImpl and bump native versions #231

Closed issues:

  • Expo managed workflow support #82
  • Feature request: Support react-native-web #24

#

v3.0.0 (2024-04-09)

This is the official release for Expo and React Native Web support. For more details, please refer to the release notes for versions 3.0.0-beta.2 and 3.0.0-beta.1.

Enhancements

  • bump ios version to 4.2.4 #228

#

v3.0.0-beta.2 (2024-03-06)

Enhancements

  • Add support to use custom storage instead of @react-native-async-storage/async-storage.(https://github.com/mixpanel/mixpanel-react-native/pull/225) When JavaScript mode is enabled, Mixpanel utilizes AsyncStorage to persist data. If you prefer not to use it, or if AsyncStorage is unavailable in your target environment, you can import or define a different storage class. However, it must follow the same interface as AsyncStorage The following example demonstrates how to use a custom storage solution:
const MyAsyncStorage = require("@my-org/<library-path>/AsyncStorage"); // or your own storage class
const trackAutomaticEvents = false;
const useNative = false;
const mixpanel = new Mixpanel('YOUR_TOKEN', trackAutomaticEvents, useNative, MyAsyncStorage);
mixpanel.init();

Fixes

#

v3.0.0-beta.1 (2024-02-29)

Expo and React Native Web support

This version(PR #223) introduces support for Expo, React Native Web, and any platform using React Native that does not support iOS and Android. To activate this, initialize Mixpanel with an additional parameter useNative set to false, which will enable JavaScript mode. Currently in beta, we plan to iterate on this to address any issues and add more features. We welcome your feedback.

 const trackAutomaticEvents = false;
 const useNative = false;
 const mixpanel = new Mixpanel(
    "YOUR_MIXPANEL_TOKEN",
    trackAutomaticEvents,
    useNative
  );
 mixpanel.init();

To try the Expo sample app, navigate to Samples/MixpanelExpo, run npm install, and then execute npm run ios or npm run android

Known limitations and differences compared to the native mode (iOS/Android):

  • Automatic Events are currently not supported in this mode. Setting 'trackAutomaticEvents' to 'true' will have no effect.
  • Certain Mixpanel Properties are unavailable in Javascript mode, including detailed information about the device and screen.
  • The default flush interval is set to 10 seconds. The data will not flush automatically when the app moves to the background. We recommend flushing more frequently for key events.

#

v2.4.1 (2024-03-01)

Fixes

  • Fix mp_lib(Mixpanel Library) not being set as react-native

#

v2.4.0 (2023-12-02)

Enhancements

  • add api: setFlushBatchSize #219
  • RN 73 support for Android with AGP 8 required #215

#

v2.3.1 (2023-06-20)

Fixes

  • fix typo in the identify function resolve #205

#

v2.3.0 (2023-06-16)

Enhancements

  • Returning Promise for the identify() to avoid race condition #200

Fixes

  • Fix null reference used for synchronization (monitor-enter) #199
  • fix initialize was called with 4 arguments but expects 5 arguments #198
  • safer handling of super properties #197
  • Android resource linking failure fix #187

Closed issues:

  • Support new React Native new architecture (TurboModules/Fabric) #145
  • Feature Request: Built in getInstance() or similar instead of custom tooling #139
  • [Feature Request] Add support for Expo Config Plugins #69

#

v2.2.5 (2023-04-29)

Fixes

  • Remove semaphores, copy properties, use .merging, don't includeLibInfo in init #191

#

v2.2.4 (2023-04-25)

Fixes

  • use semaphore to prevent concurrent access to properties object #188

#

v2.2.3 (2023-04-17)

Fixes

  • Make 'groups' argument optional in trackWithGroups #169

#

v2.2.1 (2023-03-23)

Enhancements

  • bump iOS version to 4.1.0 #180

v2.2.0 (2023-03-06)

NOTE:

  • From this version we will prefix randomly generated device-specific distinct_ids with "$device:". The prefix is applied the next time a new random ID is generated, any IDs generated by previous SDK versions and persisted on the device will continue to be used as-is until reset is called to generate a new ID. This does not change the value sent for the $device_id property, which will continue to be the randomly-generated ID without a prefix. Mixpanel's $identify endpoint has been updated to accept UUIDs with this prefix to coordinate with this change.

Enhancements

  • bump ios to 4.0.5 and android to 7.3.0 #176

#

v2.1.0 (2022-09-14)

Enhancements

  • add serverURL to init params and fix Android module #160

#

v2.0.1 (2022-09-12)

Fixes

  • update typescript and iOS bridging header #158

#

v2.0.0 (2022-09-09)

BREAKING CHANGE:

This major release removes all remaining calls to Mixpanel's /decide API endpoint. The main effect of this is that the SDK no longer fetches the remote status of your project's "Automatically collect common mobile events" setting. From this version forward, automatic event tracking can only be controlled by the, now required, parameter trackAutomaticEvents. Upon upgrading, existing implementations will need to add this parameter to their Mixpanel initializer calls.

Enhancements

  • make trackAutomaticEvents required and bump versions to deprecate Decide #153

#

v1.5.0 (2022-06-24)

Enhancements

  • bump versions to get millisecond precision for event time property #146

#

v1.4.2 (2022-05-21)

Enhancements

  • bump versions to remove survey #140

#

v1.4.1 (2022-05-09)

Fixes

  • bump android version to 6.2.1 #138

#

v1.4.0 (2022-05-06)

Enhancements

  • Bump to latest ios and android sdk versions and remove android people identify(deprecated) #137
  • Allow disable flush on background #135
  • Adds a new API getDeviceId for React Native #134

#

v1.3.10 (2022-03-26)

Enhancements

  • upgrade android targetSdkVersion to 30 #131

#

v1.3.9 (2022-03-02)

Merged pull requests:

  • bump mixpanel-swift to 3.1.5 #129

#

v1.3.8 (2022-02-25)

Enhancements

  • add init super props & declare multi-prop interfaces #127

#

v1.3.7 (2022-01-26)

Fixes

  • Bump iOS SDK to v3.1.2 #125

#

v1.3.6 (2022-01-13)

Caution: Please DO NOT use this build! In this version, we have a bug in iOS that event names with & or % will be rejected by the server. We recommend you update to 1.3.7 or above.

Fixes

  • Fix common mobile events not showing 'react-native' as property value for 'Mixpanel Library' #122

#

v1.3.5 (2022-01-04)

Merged pull requests:

  • nested dictionary should be a valid type in iOS #119
  • fix setGroup array properties #118
  • bump SDk dependencies #116
  • Fix Expo SDK 44 build error #115

#

v1.3.4 (2021-09-25)

Merged pull requests:

  • Bump native SDK dependencies #102

#

v1.3.3 (2021-09-21)

Fixes

  • Enable automatic events in iOS #99
  • replace JCenter with Maven #95

Merged pull requests:

  • Bump tmpl from 1.0.4 to 1.0.5 in /Samples/ContextAPIMixpanel #100
  • Bump tmpl from 1.0.4 to 1.0.5 #98
  • Bump tmpl from 1.0.4 to 1.0.5 in /Samples/MixpanelDemo #97
  • Bump path-parse from 1.0.6 to 1.0.7 in /Samples/MixpanelDemo #87
  • Bump path-parse from 1.0.6 to 1.0.7 #84
  • Bump react-native from 0.64.0 to 0.64.1 in /Samples/MixpanelDemo #80

#

v1.3.2 (2021-08-23)

Fixes

  • Add constructor method type definition #85

#

v1.3.1 (2021-07-29)

Fixes

  • Fix the inconsistency typescript definition of init #78

Merged pull requests:

  • Add more sample apps for integrating Mixpanel including using Context API #79

#

v1.3.0 (2021-07-28)

Merged pull requests:

  • Deprecate the class method init() and improve the example #76

#

v1.2.4 (2021-06-24)

Merged pull requests:

  • Polish README #67
  • Update README: Improve the quick start guide #66
  • Fix Android crash caused by invalid number being set as property value #65
  • Bump glob-parent from 5.1.1 to 5.1.2 #63
  • Add a CHANGELOG placeholder #62
  • Add github workflow for auto release #61

#

v1.2.3 (2021-05-20)

#

v1.2.2 (2021-05-08)

#

v1.2.0 (2021-05-03)

#

v1.1.1 (2021-03-17)

#

v1.1.0 (2021-03-03)

#

v1.0.2 (2021-01-27)

#

v1.0.0 (2020-12-08)