How to Set Up Mixpanel in a Mobile App (iOS, Android, React Native)

Mobile analytics fails in quieter ways than web analytics. There’s no browser console to glance at, events sit in a queue on the device before they’re sent, and a bug you ship today stays in users’ hands until they update the app, which might be months.

That’s why I treat mobile Mixpanel setups with more caution than web ones. Get the event design and identity right before release, because you can’t hotfix a tracking call that’s already on a million phones.

This guide walks through setting up Mixpanel in iOS (Swift), Android and React Native apps, covering installation, initialization, identity, queueing and testing.

Before you start: decide shared rules

If you have web and mobile, write one tracking plan for both (post 4). The same action must have the same event name and properties on every platform. Add a platform super property (ios, android, web) so you can compare them.

Also create a separate dev project and use its token in debug builds.

iOS (Swift)

Install

Mixpanel’s Swift library can be installed with CocoaPods, Carthage or Swift Package Manager. For most modern projects I use Swift Package Manager: in Xcode, add the Mixpanel Swift package from Mixpanel’s GitHub repository.

Initialize

Initialize once, early in your app’s launch (for example in your App or AppDelegate):

import Mixpanel

Mixpanel.initialize(token: "YOUR_PROJECT_TOKEN", trackAutomaticEvents: false)

The trackAutomaticEvents flag controls Mixpanel’s legacy automatic mobile events. Mixpanel’s own quickstart examples set it to false. I usually do the same and track app lifecycle events deliberately.

Track an event

Mixpanel.mainInstance().track(event: "sign_up_completed", properties: [
  "signup_method": "apple",
  "plan": "free"
])

Identify and reset

// After login or signup
Mixpanel.mainInstance().identify(distinctId: user.id)

// On logout
Mixpanel.mainInstance().reset()

Mixpanel recommends against calling identify() for anonymous visitors. Call it only once you know who the user is.

Queueing and flushing

The Swift SDK batches events and flushes them on an interval, by default every 60 seconds. You can change it with flushInterval or flush manually with flush().

Android

Install

Add the Mixpanel Android library as a Gradle dependency (check Mixpanel’s docs for the current coordinates and version), and make sure your app has internet permission.

Initialize

boolean trackAutomaticEvents = false;
MixpanelAPI mixpanel = MixpanelAPI.getInstance(context, "YOUR_PROJECT_TOKEN", trackAutomaticEvents);

Track, identify, reset

JSONObject props = new JSONObject();
props.put("signup_method", "google");
mixpanel.track("sign_up_completed", props);

mixpanel.identify(user.getId());

// On logout
mixpanel.reset();

Configuration through the manifest

The Android SDK reads several settings from meta-data entries in your AndroidManifest.xml. Mixpanel’s docs show options such as enabling debug logging and controlling whether events flush when the app goes to the background.

<meta-data android:name="com.mixpanel.android.MPConfig.EnableDebugLogging"
           android:value="true" />

Only enable debug logging in debug builds.

Batching

You can adjust the flush batch size with setFlushBatchSize(), and flush manually with flush(). Android also offers a DeviceIdProvider interface if you need custom control over how device IDs are generated and whether they survive reset().

React Native

Install

npm install mixpanel-react-native
cd ios && pod install

The React Native SDK is a wrapper around Mixpanel’s native iOS and Android SDKs, and Mixpanel notes it supports offline tracking.

Initialize

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

const trackAutomaticEvents = false;
const mixpanel = new Mixpanel('YOUR_PROJECT_TOKEN', trackAutomaticEvents);
mixpanel.init();

Create this once and share it through your app, for example with a React context.

Track, identify, reset

mixpanel.track('sign_up_completed', { signup_method: 'email' });
mixpanel.identify(user.id);
mixpanel.reset();

Expo and React Native Web: JavaScript mode

From version 3.0.0, the SDK supports Expo, React Native for Web and other platforms without direct native access through JavaScript mode. Install AsyncStorage and pass useNative = false:

const trackAutomaticEvents = false;
const useNative = false;
const mixpanel = new Mixpanel('YOUR_PROJECT_TOKEN', trackAutomaticEvents, useNative);
mixpanel.init();

JavaScript mode is more portable but collects fewer automatic device properties than native mode. You can also supply a custom storage implementation if you don’t use AsyncStorage.

Batching

Mixpanel’s React Native docs say events are flushed immediately when a batch reaches 50 events by default; change it with setFlushBatchSize().

Identity on mobile

Mobile apps add a few identity wrinkles:

  • Reinstalls usually create a new anonymous ID. Calling identify() on login reconnects the user.
  • Call identify when the app reopens in a logged-in state, not just at login. Mixpanel recommends this so every session’s events carry the user ID.
  • Cross-platform users (web + iOS) merge correctly if every platform identifies with the same stable database ID (post 2).

Profiles on mobile

Each SDK has a People API for profile properties. On Swift, you call identify() before updating the profile, then set properties through the people interface. See post 27 for which properties belong on profiles.

Testing a mobile implementation

  1. Use a debug build pointed at your dev project, with debug logging on.
  2. Shorten the flush interval or call flush() after key actions, so events arrive quickly while testing.
  3. Watch the logs in Xcode or Android Studio for each request.
  4. In Mixpanel’s Events view, search for your test user and confirm events and properties. Mixpanel’s React Native readme notes events usually take about 60 seconds to become queryable after they reach the API.
  5. Test offline: put the device in airplane mode, trigger events, reconnect, and check they arrive.
  6. Test logout and login as two different users on the same device.

Mistakes to avoid

  • Different event names on iOS and Android.
  • Calling identify for anonymous users.
  • Not calling reset on logout, so shared devices merge users.
  • Shipping debug logging in production builds.
  • Assuming events arrive instantly. They’re queued.
  • Tracking business-critical events only on the device. Purchases and subscription renewals are more reliable from your backend or app store server notifications.

Final thoughts

Mobile tracking rewards care before release. Share one tracking plan across platforms, identify on login and app reopen, reset on logout, understand queueing, and test offline and multi-user cases before you ship.

Sources