Android

Quickstart Guide

Overview

We'll show you how to install the Mixpanel Android library using Gradle.

Check out our Advanced Android Guide for additional configurations and use cases, like setting up your project with European Union data storage.

Skip to a complete code example.

1. Install Mixpanel

You will need your project token for initializing your library. You can get your project token from project settings.

Step 1 - Add the mixpanel-android library as a gradle dependency:

We publish builds of our library to the Maven central repository as an .aar file. This file contains all of the classes, resources, and configurations that you'll need to use the library. To install the library inside Android Studio, you can simply declare it as dependency in your build.gradle file.

dependencies {
    implementation 'com.mixpanel.android:mixpanel-android:5.+'
}

On Jan 1, 2022, we’ll remove the Messages & Experiments feature from Mixpanel. For now, you can choose to opt in to our beta version without the Messages & Experiments feature support.

implementation "com.mixpanel.android:mixpanel-android:6.0.0-beta+"

Once you've updated your build.gradle file, 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.

Sync Android With GradleSync Android With Gradle

This should download the .aar dependency at which point you'll have access to the Mixpanel library API calls. If it cannot find the dependency, you should make sure you've specified mavenCentral() as a repository in your build.gradle.

Step 2 - Add permissions to your AndroidManifest.xml:

In order for the library to work you'll need to ensure that you're requesting the following permissions in your AndroidManifest.xml:

<!--
This permission is required to allow the application to send
events and properties to Mixpanel.
-->
<uses-permission
  android:name="android.permission.INTERNET" />

<!--
  This permission is optional but recommended so we can be smart
  about when to send data.
 -->
<uses-permission
  android:name="android.permission.ACCESS_NETWORK_STATE" />

<!--
  This permission is optional but recommended so events will
  contain information about bluetooth state
-->
<uses-permission
  android:name="android.permission.BLUETOOTH" />

At this point, you're ready to use the Mixpanel Android library inside Android Studio.

2. Initialize Mixpanel

Once you've set up your build system or IDE to use the Mixpanel library, you can initialize it in your code by calling MixpanelAPI.getInstance with your application context and your Mixpanel project token. You can find your token in project settings.

import com.mixpanel.android.mpmetrics.MixpanelAPI;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, "YOUR_TOKEN");
    }
}

See all configuration options

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 collect common mobile events. You can enable/disable automatic collection through your project settings. Also, Mixpanel automatically tracks some properties by default. learn more

JSONObject props = new JSONObject();
props.put("source", "Pat's affiliate site");
props.put("Opted out of email", true);

mixpanel.track("Sign Up", props);

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 Live View 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

Here's a runnable code example that covers everything in this quickstart guide.

import com.mixpanel.android.mpmetrics.MixpanelAPI;


public class MainActivity extends ActionBarActivity {

        private MixpanelAPI mMixpanel;

    @Override
       protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mMixpanel = MixpanelAPI.getInstance(this, "YOUR_TOKEN");
       }

...
        
        private void sendToMixpanel() throws JSONException {
            JSONObject props = new JSONObject();
            props.put("source", "Pat's affiliate site");
            props.put("Opted out of email", true);
            mixpanel.track("Sign Up", props);
      }

...
}

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?
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 for example before your application is completely shutdown.

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 Android library. To enable it, you will want to add the following permission within your AndroidManifest.xml inside the <application> tag:

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

Did this page help you?