Android
Updated Dec. 14, 2011If you want to track user behavior on your Android application, the first thing to do is download the latest Android library or clone the git repository.
The repository contains:
- MPMetrics.jar - The Mixpanel Android library with source code
- Hello Mixpanel - And example Android application that integrates Mixpanel tracking
- Source code - Full source code to the whole Mixpanel library for Android
Contents
Setup
Add "MPMetrics.jar" to your classpath. In Eclipse follow the following steps:
- Click on "Properties" in the "Project" menu.
- Click on "Java Build Path" in the left pane.
- Click on the "Libraries" tab.
- Click on the "Add External JARs..." button and find "MPMetrics.jar."
Your properties window will look like the above.
Using the SDK tools directly, place "MPMetrics.jar" in your "libs" folder. The ant task will automatically add the jar to your classpath.
Edit your Android application's "AndroidManifest.xml. The "andoird.permission.INTERNET" permission is required. Add the following line after the </application> line of your "AndroidManifest.xml" file:
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
Initializing Mixpanel
The next thing you need to do in order to use Mixpanel is to instantiate an MPMetrics object. The MPMetrics constructor takes 2 arguments: your context and your Mixpanel API token. We recommend that you create this object in your Activity's onCreate method.
protected void onCreate(Bundle savedInstanceState) {
mMPMetrics = new MPMetrics(this, "YOUR_API_TOKEN");
}
Tracking events
After initializing the MPMetrics object, you are ready to track events. This can be done with the following code snippet:
mMPMetrics.track("Event here", null);
If you want to add properties to the event you can do the following:
JSONObject properties = new JSONObject();
properties.put("gender", "male");
properties.put("age", 20);
mMPMetrics.track("register", properties);
Storing data with super properties
Sometimes you know something about a user that you want to send with every event - perhaps the app version the user is using, or the gender of the user. Super Properties make this really easy - they are global, persistent properties that are attached to every event request. You can learn more about them here.
If you want to register a super property for your current user, you can do it as follows:
JSONObject superProperties = new JSONObject();
superProperties.put("age", 20);
mMPMetrics.registerSuperProperties(superProperties);
This call will register the "age" property with a value of 20 for the current user.
Technical overview
At this point you may be wondering how this thing works. The basic workflow is this:
- You track some events
- We queue them up, and send them as a batch after either 40 events or 60 seconds, whichever comes first
-
You call flush
before your application closes to ensure that all remaining data is sent. We recommend you do this in your Activity's onDestroy
method.
protected void onDestroy() { mMPMetrics.flush(); }
API Specification
So far we've just looked at the most important functions provided by the Android library. Here's some more comprehensive information: Mixpanel Android API.
