Overview
Installing Mixpanel is easy. This guide will show you how to do it with our SDKs. Already collect product data? Connect your Data Warehouse or via 3rd Party Integrations.Code
Choose from the methods below. Not sure how to choose? Read our guide. Mixpanel supports EU and India data residency, which you choose per project. If your project uses either, point the SDK at that region’s host. Each tab below shows how. Events sent to the wrong region aren’t ingested.- Javascript
- Python
- PHP
- Node
- Go
- Ruby
- Java
- React Native
- Flutter
- iOS (Objective-C)
- iOS (Swift)
- Android
- Unity
- HTTP API
Install the SDK
- snippet
- npm
- yarn
<!-- Paste this right before your closing </head> tag -->
<script type="text/javascript">
(function (f, b) { if (!b.__SV) { var e, g, i, h; window.mixpanel = b; b._i = []; b.init = function (e, f, c) { function g(a, d) { var b = d.split("."); 2 == b.length && ((a = a[b[0]]), (d = b[1])); a[d] = function () { a.push([d].concat(Array.prototype.slice.call(arguments, 0))); }; } var a = b; "undefined" !== typeof c ? (a = b[c] = []) : (c = "mixpanel"); a.people = a.people || []; a.toString = function (a) { var d = "mixpanel"; "mixpanel" !== c && (d += "." + c); a || (d += " (stub)"); return d; }; a.people.toString = function () { return a.toString(1) + ".people (stub)"; }; i = "disable time_event track track_pageview track_links track_forms track_with_groups add_group set_group remove_group register register_once alias unregister identify name_tag set_config reset opt_in_tracking opt_out_tracking has_opted_in_tracking has_opted_out_tracking clear_opt_in_out_tracking start_batch_senders people.set people.set_once people.unset people.increment people.append people.union people.track_charge people.clear_charges people.delete_user people.remove".split( " "); for (h = 0; h < i.length; h++) g(a, i[h]); var j = "set set_once union unset remove delete".split(" "); a.get_group = function () { function b(c) { d[c] = function () { call2_args = arguments; call2 = [c].concat(Array.prototype.slice.call(call2_args, 0)); a.push([e, call2]); }; } for ( var d = {}, e = ["get_group"].concat( Array.prototype.slice.call(arguments, 0)), c = 0; c < j.length; c++) b(j[c]); return d; }; b._i.push([e, f, c]); }; b.__SV = 1.2; e = f.createElement("script"); e.type = "text/javascript"; e.async = !0; e.src = "undefined" !== typeof MIXPANEL_CUSTOM_LIB_URL ? MIXPANEL_CUSTOM_LIB_URL : "file:" === f.location.protocol && "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js".match(/^\/\//) ? "https://cdn.mxpnl.com/libs/mixpanel-2-latest.min.js" : "//cdn.mxpnl.com/libs/mixpanel-2-latest.min.js"; g = f.getElementsByTagName("script")[0]; g.parentNode.insertBefore(e, g); } })(document, window.mixpanel || []);
</script>
npm install --save mixpanel-browser
yarn add mixpanel-browser
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.You can learn more about configuring Session Replay and Heatmaps here.Javascript
//Import Mixpanel SDK (only required for npm and yarn)
import mixpanel from "mixpanel-browser";
// Near entry of your product, init Mixpanel
mixpanel.init("YOUR_TOKEN", {
debug: true,
track_pageview: true,
persistence: "localStorage",
record_sessions_percent: 100, //records 100% of all sessions
record_heatmap_data: true,
});
api_host:mixpanel.init("YOUR_TOKEN", {
api_host: "https://api-eu.mixpanel.com",
debug: true,
track_pageview: true,
persistence: "localStorage",
record_sessions_percent: 100, //records 100% of all sessions
record_heatmap_data: true,
});
mixpanel.init("YOUR_TOKEN", {
api_host: "https://api-in.mixpanel.com",
debug: true,
track_pageview: true,
persistence: "localStorage",
record_sessions_percent: 100, //records 100% of all sessions
record_heatmap_data: true,
});
Install the SDK
Python
pip install mixpanel
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.Python
from mixpanel import Mixpanel
mp = Mixpanel("YOUR_TOKEN")
Consumer with api_host set:from mixpanel import Mixpanel, Consumer
mp = Mixpanel("YOUR_TOKEN", consumer=Consumer(api_host="api-eu.mixpanel.com"))
from mixpanel import Mixpanel, Consumer
mp = Mixpanel("YOUR_TOKEN", consumer=Consumer(api_host="api-in.mixpanel.com"))
Install the SDK
Install with Composer- Add mixpanel/mixpanel-php as a dependency and run composer update:
php
"require": {
...
"mixpanel/mixpanel-php" : "2.*"
...
}
- Download the Mixpanel PHP Library
- Extract the zip file to a directory called “mixpanel-php” in your project root
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.php
<?php
// import Mixpanel
require 'mixpanel-php/lib/Mixpanel.php';
// get the Mixpanel class instance, replace with your project token
$mp = Mixpanel::getInstance("YOUR_TOKEN");
?>
host option:<?php
$mp = Mixpanel::getInstance("YOUR_TOKEN", array(
"host" => "api-eu.mixpanel.com"
));
?>
<?php
$mp = Mixpanel::getInstance("YOUR_TOKEN", array(
"host" => "api-in.mixpanel.com"
));
?>
Install the SDK
Node.js
npm install mixpanel
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.Node.js
// Grab the Mixpanel factory
var Mixpanel = require("mixpanel");
// Create an instance of the mixpanel client
var mixpanel = Mixpanel.init("YOUR_TOKEN");
host:var mixpanel = Mixpanel.init("YOUR_TOKEN", {
host: "api-eu.mixpanel.com",
});
var mixpanel = Mixpanel.init("YOUR_TOKEN", {
host: "api-in.mixpanel.com",
});
Install the SDK
Go
go get github.com/mixpanel/mixpanel-go
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.Go
package main
import (
"context"
"github.com/mixpanel/mixpanel-go"
)
func main() {
ctx := context.Background()
mp := mixpanel.NewApiClient("YOUR_TOKEN")
}
mp := mixpanel.NewApiClient("YOUR_TOKEN", mixpanel.EuResidency())
mp := mixpanel.NewApiClient("YOUR_TOKEN", mixpanel.InResidency())
Install the SDK
Ruby
gem install mixpanel-ruby
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.Ruby
require 'mixpanel-ruby'
mp = Mixpanel::Tracker.new(YOUR_TOKEN)
consumer = Mixpanel::Consumer.new(
'https://api-eu.mixpanel.com/track',
'https://api-eu.mixpanel.com/engage',
'https://api-eu.mixpanel.com/groups',
)
mp = Mixpanel::Tracker.new(YOUR_TOKEN) do |type, message|
consumer.send!(type, message)
end
consumer = Mixpanel::Consumer.new(
'https://api-in.mixpanel.com/track',
'https://api-in.mixpanel.com/engage',
'https://api-in.mixpanel.com/groups',
)
mp = Mixpanel::Tracker.new(YOUR_TOKEN) do |type, message|
consumer.send!(type, message)
end
Install the SDK
Java
<!--Include the following in your project's pom.xml-->
<dependency>
<groupId>com.mixpanel</groupId>
<artifactId>mixpanel-java</artifactId>
<version>1.4.4</version>
</dependency>
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.Java
//Import Mixpanel API
import com.mixpanel.mixpanelapi.MessageBuilder;
import com.mixpanel.mixpanelapi.MixpanelAPI;
// Set up an instance of MixpanelAPI
MixpanelAPI mixpanel = new MixpanelAPI();
MessageBuilder messageBuilder = new MessageBuilder('YOUR_TOKEN');
MixpanelAPI:MixpanelAPI mixpanel = new MixpanelAPI("https://api-eu.mixpanel.com/track",
"https://api-eu.mixpanel.com/engage",
"https://api-eu.mixpanel.com/groups");
MixpanelAPI mixpanel = new MixpanelAPI("https://api-in.mixpanel.com/track",
"https://api-in.mixpanel.com/engage",
"https://api-in.mixpanel.com/groups");
Install the SDK
Under your app’s root directory, run:npm install mixpanel-react-native
pod install
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.//Import Mixpanel API
import { Mixpanel } from "mixpanel-react-native";
// Set up an instance of Mixpanel
const trackAutomaticEvents = false;
const mixpanel = new Mixpanel("YOUR_TOKEN", trackAutomaticEvents);
mixpanel.init();
init():mixpanel.init(false, {}, "https://api-eu.mixpanel.com");
mixpanel.init(false, {}, "https://api-in.mixpanel.com");
Install the SDK
Addmixpanel_flutter: ^2.0.0 to your package’s pubspec.yaml file and run:flutter pub get
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.import 'package:mixpanel_flutter/mixpanel_flutter.dart';
class _YourClassState extends State<YourClass> {
Mixpanel mixpanel;
@override
void initState() {
super.initState();
initMixpanel();
}
Future<void> initMixpanel() async {
// Once you've called this method once, you can access `mixpanel` throughout the rest of your application.
mixpanel = await Mixpanel.init("YOUR_TOKEN", trackAutomaticEvents: false);
}
}
serverURL:mixpanel = await Mixpanel.init(
"YOUR_TOKEN",
trackAutomaticEvents: false,
serverURL: "https://api-eu.mixpanel.com",
);
mixpanel = await Mixpanel.init(
"YOUR_TOKEN",
trackAutomaticEvents: false,
serverURL: "https://api-in.mixpanel.com",
);
Install the SDK
- Swift Package Manager
- Cocoapods
- Carthage
Note: Swift Package Manager requires Xcode 12+ 1. In Xcode, select File > Swift Packages > Add Package Dependency. 2. Follow the prompts using the URL for this repository and must select a version greater than or equal to v4.0.0
- Create a Podfile in your Xcode project directory by running
pod initin your terminal, edit the Podfile generated, and add the following line:pod 'Mixpanel'. 2. Runpod installin your Xcode project directory. CocoaPods should download and install the Mixpanel library, and create a new Xcode workspace. Open up this workspace in Xcode or typingopen *.xcworkspacein your terminal.
Add
github "mixpanel/mixpanel-iphone" to your Cartfile.Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.#import "Mixpanel/Mixpanel.h"
- (BOOL)application:(UIApplication _)application
didFinishLaunchingWithOptions:(NSDictionary _)launchOptions {
...
Mixpanel \*mixpanel = [Mixpanel sharedInstanceWithToken:@"YOUR_TOKEN"
trackAutomaticEvents: NO];
...
}
serverURL after initialization:mixpanel.serverURL = @"https://api-eu.mixpanel.com";
mixpanel.serverURL = @"https://api-in.mixpanel.com";
Install the SDK
- Swift Package Manager
- Cocoapods
- Carthage
Note: Swift Package Manager requires Xcode 12+
- In Xcode, select File > Swift Packages > Add Package Dependency.
- Paste the URL
https://github.com/mixpanel/mixpanel-swiftand a minimum semantic version of v2.8.0.
- Create a Podfile in your Xcode project directory by running
pod initin your terminal, edit the Podfile generated, and add the following line:pod 'Mixpanel-swift'. - Run
pod installin your Xcode project directory. CocoaPods should download and install the Mixpanel library, and create a new Xcode workspace. Open up this workspace in Xcode or typingopen *.xcworkspacein your terminal.
Add
github "mixpanel/mixpanel-swift" to your Cartfile.Configure the SDK
- Import Mixpanel into
AppDelegate.swift - Initialize Mixpanel within
application:didFinishLaunchingWithOptionsas seen below
YOUR_TOKEN with your project token. You can find your token here.import Mixpanel
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
...
// Replace with your Project Token
Mixpanel.initialize(token: "YOUR_TOKEN", trackAutomaticEvents: false)
...
}
serverURL in MixpanelOptions:let options = MixpanelOptions(
token: "YOUR_TOKEN",
serverURL: "https://api-eu.mixpanel.com"
)
Mixpanel.initialize(options: options)
let options = MixpanelOptions(
token: "YOUR_TOKEN",
serverURL: "https://api-in.mixpanel.com"
)
Mixpanel.initialize(options: options)
Install the SDK
Addimplementation 'com.mixpanel.android:mixpanel-android:7.+' as a dependency to your build.gradle file.Once you’ve updated build.gradle, you can force Android Studio to sync with your new configuration by clicking the Sync Project with Gradle Files icon at the top of the window:
mavenCentral() as a repository in build.gradle.Next, add the following permissions in your AndroidManifest.xml:<!--Required to allow the application to send events to Mixpanel.-->
<uses-permission android:name="android.permission.INTERNET" />
<!--Optional, but recommended so we can send data intelligently based on network conditions -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Optional, but recommended so events will contain information about bluetooth state-->
<uses-permission android:name="android.permission.BLUETOOTH" />
Configure the SDK
ReplaceYOUR_TOKEN with your project token. You can find your token here.import com.mixpanel.android.mpmetrics.MixpanelAPI;
public class MainActivity extends ActionBarActivity {
private MixpanelAPI mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
trackAutomaticEvents = false;
// Replace with your Project Token
mp = MixpanelAPI.getInstance(this, "YOUR_TOKEN", trackAutomaticEvents);
}
}
serverURL with MixpanelOptions:MixpanelOptions options = new MixpanelOptions.Builder()
.serverURL("https://api-eu.mixpanel.com")
.build();
mp = MixpanelAPI.getInstance(this, "YOUR_TOKEN", trackAutomaticEvents, options);
MixpanelOptions options = new MixpanelOptions.Builder()
.serverURL("https://api-in.mixpanel.com")
.build();
mp = MixpanelAPI.getInstance(this, "YOUR_TOKEN", trackAutomaticEvents, options);
Install the SDK
Addhttps://github.com/mixpanel/mixpanel-unity.git#master to the dependencies section of com.mixpanel.unity.Alternatively, you can download and install the .unitypackage file from our releases page.Configure the SDK
To initialize the library, first open the unity project settings menu for Mixpanel. (Edit -> Project Settings -> Mixpanel) Then, enter your project token into the Token and Debug Token input fields within the inspector. You can find your token here.
https://api-eu.mixpanel.com/ or https://api-in.mixpanel.com/.Note: If you prefer to initialize Mixpanel manually, you can select the Manual Initialization in the settings and call Mixpanel.Init() to initialize.To use mixpanel, add the following sample code to a part of your application:using mixpanel;
Install the SDK
No install required
api-eu.mixpanel.com or api-in.mixpanel.com instead of api.mixpanel.com.Integration / Framework Guides
Segment
Google Tag Manager
Ad Spend
Amazon S3
Amazon Kafka
CMS & E-Commerce
Customer.io
Freshpaint
Google Cloud Storage
Google Pubsub
Google Sheets
Langfuse
LaunchDarkly
mParticle
Next.js
Mobile Attribution Tracking
Rudderstack
Shopify
Vendo for Shopify
Snowplow
Stripe
Tealium
Next: Identify Your Users
With Mixpanel installed, you’re now ready to identify the users who use your product.
FAQ
Does Mixpanel automatically track page views?
Does Mixpanel automatically track page views?
Yes, if you pass
track_pageview: true in the mixpanel.init() call, Mixpanel will automatically track a “Page View” event every time a new page is loaded. Learn more here.What are the recommended web configuration options?
What are the recommended web configuration options?
When tracking on web, we recommend using localStorage, as this is more reliable:Please note that cross-subdomain tracking is not possible when using local storage. If your implementation requires cross-subdomain tracking, remove the persistence flag and use the default “cookie” persistence option.
mixpanel.set_config({ persistence: "localStorage" });
Does Mixpanel use third-party cookies?
Does Mixpanel use third-party cookies?
No, our Mixpanel JavaScript SDK does not set or use any third-party cookies. If you wish to disable cookies entirely, you can set the disable_persistence option to true when initializing your Mixpanel JS instance. Note that disabling persistence will disable the use of super properties and anonymous -> identified user tracking.
How can I track in a privacy compliant way?
How can I track in a privacy compliant way?
If a user opts out of tracking, you can call the
.optOutTracking() method on any of our client-side SDKs; this prevents any subsequent data being tracked from that user’s device. Learn more here.For iOS specifically: Mixpanel does not use IDFA, so it does not require user permission through the AppTrackingTransparency(ATT) framework. For more details, refer to our Apple App Developer Privacy Guidance.