Mixpanel React Native SDK

3.3.0 · active · verified Sun Apr 19

Mixpanel React Native SDK is the official open-source library for integrating Mixpanel analytics into React Native applications, supporting both iOS and Android platforms. It acts as a wrapper around Mixpanel's native SDKs and enables robust offline tracking capabilities. The current stable version is 3.3.0, with frequent minor and patch releases to include enhancements, security updates, and bug fixes, indicating an active development cadence. A key differentiator is its direct wrapping of native SDKs, ensuring consistency with native Mixpanel features, and its explicit support for data persistence via `async-storage` to handle offline scenarios reliably. It's designed for React Native v0.6+ and ships with TypeScript types.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates basic Mixpanel integration in a React Native app, including initialization, tracking custom events with properties, identifying a user, setting user profile properties, and resetting the Mixpanel instance.

import React, { useEffect } from 'react';
import { Button, SafeAreaView, Text, View } from 'react-native';
import { Mixpanel } from 'mixpanel-react-native';

const projectToken = process.env.MIXPANEL_PROJECT_TOKEN ?? 'YOUR_MIXPANEL_PROJECT_TOKEN'; // Replace with your Mixpanel project token
const trackAutomaticEvents = false;

const mixpanel = new Mixpanel(projectToken, trackAutomaticEvents);

export default function App() {
  useEffect(() => {
    mixpanel.init();
  }, []);

  const trackEvent = (eventName) => {
    mixpanel.track(eventName, { source: 'React Native App', time: new Date().toISOString() });
    console.log(`Tracked event: ${eventName}`);
  };

  const identifyUser = () => {
    const userId = 'user123'; // Replace with actual user ID
    mixpanel.identify(userId);
    mixpanel.people.set({ '$first_name': 'Test', '$last_name': 'User', 'plan': 'Free' });
    console.log(`Identified user: ${userId}`);
  };

  return (
    <SafeAreaView style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text style={{ fontSize: 24, marginBottom: 20 }}>Mixpanel React Native Demo</Text>
      <View style={{ gap: 10 }}>
        <Button title="Track 'App Loaded'" onPress={() => trackEvent('App Loaded')} />
        <Button title="Track 'Button Clicked'" onPress={() => trackEvent('Button Clicked')} />
        <Button title="Identify User" onPress={identifyUser} />
        <Button title="Reset Mixpanel" onPress={() => mixpanel.reset()} />
      </View>
    </SafeAreaView>
  );
}

view raw JSON →