Quick Start Guide

Contents: [ Next Steps ]
[NOTE ]

Visit the Dev Docs

A better version of this page exists at https://developer.mixpanel.com/docs/quick-start-guide.

To use Mixpanel in your android app you need to add Mixpanel dependencies, update your application permissions, and initialize the Mixpanel library.

  1. Add dependencies to app/build.gradle.
apply plugin: 'com.android.application'

dependencies {
	implementation 'com.android.support:appcompat-v7:22.1.1'

	implementation "com.mixpanel.android:mixpanel-android:5.+"
}

android {
	compileSdkVersion 22
	buildToolsVersion "22.0.1"

defaultConfig {
	applicationId "com.mixpanel.example.myapplciation"
	minSdkVersion 15
	targetSdkVerision 22
	versionCode 1
	versionName "1.0"
	}
}
  1. Add permissions to app/src/main/AndroidManifest.xml.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.mixpanel.example.myapplication">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.BLUETOOTH" />

<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > 
	<activity android:name=".MainActivity" android:label="@string/app_name">
		<intent-filter>
			<action android:name="android.intent.action.MAIN" />
		</intent-filter>
	</activity>
</application>
</manifest>

  1. Add Mixpanel to your main activity app/src/main/java/com/mixpanel/example/myapplication/MainActivity.java
package com.mixpanel.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;

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);

		String projectToken = YOUR_PROJECT_TOKEN; // Insert project token here
		MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, projectToken);
	}
}

Make sure you've replaced "YOUR_PROJECT_TOKEN" with your project token, which you can find by clicking the settings gear in the upper righthand corner of your Mixpanel project and selecting Project settings from the dropdown. After installing the library into your Android app, Mixpanel will automatically collect common mobile events. You can enable/ disable automatic collection through your project settings.

  1. Add code to track additional events.
package com.mixpanel.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.mixpanel.android.mpmetrics.MixpanelAPI;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends ActionBarActivity {

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

		String projectToken = YOUR_PROJECT_TOKEN; // Insert project token here
		MixpanelAPI mixpanel = MixpanelAPI.getInstance(this, projectToken);
  
  	try {
  		JSONObject props = new JSONObject();
  		props.put("Gender", "Female");
  		props.put("Logged in", false);
  		mixpanel.track("MainActivity - onCreate called", props);
	 	} catch (JSONException e) {
  	Log.e("MYAPP", "Unable to add properties to JSONObject", e)
		}
	}
}
  1. Mixpanel is now integrated with your app.

Make sure you that have replaced "YOUR_PROJECT_TOKEN" with the project token you want to send data to and run your application. You should see the "MainActivity - onCreate called" event show up on your Mixpanel reports just seconds after your app runs.

NOTE

In order to ensure our library doesn't have a large impact on performance or battery life, events aren't sent to Mixpanel immediately. Instead we queue them on the device and then send them in batches every 60 seconds by default.

Next Steps

Now that you've integrated Mixpanel, there are a number of powerful ways to track and engage with your users.

  • Track important actions in your app - To get the most value out of Mixpanel, you should identify the actions that are important to you and track them. To track events, you can add code to your app or use our codeless event tracking tool.

  • Setup and run your first A/B test - Once you have the Mixpanel SDK initialized you can build and run an A/B test without writing any additional code. This is a great way to quickly test and measure changes to your application.

  • Send your users a push notification or message - Mixpanel allows you to send your users targeted push notifications and in-app messages. To get started, you need to send people profiles to Mixpanel. This will allow you to send messages to particular users within your app.

Is this article helpful?
0 out of 0 found this helpful

Comments

0 comments

Please sign in to leave a comment.