Skip to content

mixpanel/mixpanel-android

Repository files navigation

Mixpanel Android Library

Latest Version

April 15, 2024 - v7.5.2

Table of Contents

Quick Start Guide

Check out our official documentation for more in depth information on installing and using Mixpanel on Android.

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.

Add the following lines to the dependencies section in app/build.gradle

implementation "com.mixpanel.android:mixpanel-android:7.+"

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 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, your Mixpanel project token and automatic events setting. 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);

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

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 the Events report (formerly 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.

👋 👋 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 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>
...

Other common issues

People.set throwing UnsupportedOperationException

I want to know more!

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

Have any questions? Reach out to support@mixpanel.com to speak to someone smart, quickly.

Want to Contribute?

The Mixpanel library for Android 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 our opening positions for details.

Changelog

See changelog.

License

See LICENSE File for details. The Base64Coder,
ConfigurationChecker, and StackBlurManager classes, and the entirety of the
 com.mixpanel.android.java_websocket package used by this
software have been licensed from non-Mixpanel sources and modified
for use in the library. Please see the relevant source files, and the
LICENSE file in the com.mixpanel.android.java_websocket package for details.

The StackBlurManager class uses an algorithm by Mario Klingemann <mario@quansimondo.com>
You can learn more about the algorithm at
http://www.quasimondo.com/StackBlurForCanvas/StackBlurDemo.html.