Implementation of Mixpanel in Android — Android Studio — Part 2(Final)

taimur tanveer
4 min readNov 2, 2020

Hi, this blog will be useful for the developers 💻 who are new to Mixpanel and want to know about Mixpanel and how to integrate Mixpanel into the android project. ❓❓

Pre-requisite —

  • Mixpanel Account ⌨ 📶
  • Android 📱
  • Java (Kotlin not supported by Mixpanel) 💻

🙏 🙏Recommend you to read the first article to know about Mixpanel and key features. 👇 👇 👇
Implementation Mixpanel in Android — Part 1

Create your 👉Mixpanel 👈 Account and setup Project in Mixpanel.

  1. Setup Project in MixPanel. In “ORGANIZATION SETTINGS”, click Projects.
  2. Click Create Project. The “Create New Project” box appears.
  3. Enter a name for the project and click Create Project. The new project appears in the list of projects. Congratulations 🎊🎉🍾🎈

Now, Let’s move on to the integration of Mixpanel with Android 📱Studio 😉 👨‍💻👨‍💻

Step 1️⃣— Add the MixPanel-android library as a Gradle dependency:
Add the below library in your app-level Gradle file and sync⭕ your project.

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

Step 2️⃣— Add 🙏permissions 🙏 to your AndroidManifest.xml.

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

Step 3️⃣— Initializing the Library◾️◾️◾️◾️

First of all, get the Mixpanel project token. You can get the project by following the below image. Click the picture 👇 👇🏽 for a better understanding

Mixpanel Token for project

Once you get the token you are good to initialize the library. 😊

private String mixpanel_token=”YOUR_TOKEN”;MixpanelAPI mixpanel =
MixpanelAPI.getInstance(context, mixpanel_token);

After the library is initialized in android studio, MixPanel will automatically collect some common events likeMixpanel fires 🔥 some default events like First App Open, App Update, App Crashed 😒 💥, and App Session, etc but you can also create your custom events easily as well, which I’ll talk about in this article below. 👇 👇

Easy peasy lemon squeezy 😁 😋 😎

Please check 👇🏻 👇🏻 for further details of default events.
https://help.mixpanel.com/hc/en-us/articles/115004596186-Default-Mobile-Events-Collection

⏩Debugging and Logging⏪

Mixpanel allows us to see debug output from the Mixpanel library. To enable debugging please add the below code inside your AndroidManifest.xml file in Application Tag. Check your logcat to see the Mixpanel default event by enabling debugging.
✍🏽Note: Make sure to remove this line in the Release build.

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

⏩Flushing Events⏪

For the sake of customer battery life and bandwidth, you must flush🧺 😳 the MixPanel instance.

@Override
protected void onDestroy() {
if (mixpanel != null) {
mixpanel.flush();
}
super.onDestroy();
}

⏩Sending Custom Events⏪

Let’s start sending some custom events. � 🤘 🤘�
You can add events by using mixpanel.track(); function. This function will get 2️⃣ parameters. One is JSONObject and another one is the event name.
Please check below sample code for adding custom events.

JSONObject jsonObject=new JSONObject();
try {
jsonObject.put("Name","Taimur");
jsonObject.put("age",25);
jsonObject.put("AndroidDeveloper",true);
} catch (JSONException e) {
}
mixpanel.track("Customer Info",jsonObject);

▶▶Run the application and now go to Mixpanel and check your event is tracked.

For more about custom events and other mixpanel stuff please check GitHub code. Link available at the end of article.

✍🏽Note: You can use custom event on click button, profile page open, home page open and etc.

Timing Events⏪ 🕔 🕕

Let’s talk about another amazing feature of MixPanel that is not provided by firebase. As you read the heading, it’s time⏳ events. 🕕 Yes, you are thinking right! Now you can track how much time it takes for a file to download and upload, and also you can check how much time⏱ it takes for a user to login and other time⏱ taking stuff you want to track as per your requirement. So it’s all about time.😉 😉⏱ 🕔⏰⏳

// start the timer for the event "Uploading CV" mixpanel.timeEvent("Uploading CV");// stop the timer if the uploadingCv() method returns true 
if(uploadingCv()){
mixpanel.track("Uploading CV");
}

Managing User Identity

The Mixpanel library will assign a default unique identifier (we call it “distinct_id”) to each install of your application. This distinct_id is saved in persistent device storage(local storage) and will be the same across executions of your app.

Mixpanel allows us to change the user identity if we want. Check the below code.

// Ensure all future events sent from // the device will have the distinct_id 12345
mixpanel.identify("12345");
// Ensure all future user profile properties sent from // the device will have the distinct_id 13793 mixpanel.getPeople().identify("12345");

Setting Profile Properties

Mixpanel allows us to set user profile properties like if you want to set user last name, age, location, and other user properties.

MixpanelAPI.People userProperties =  mixpanel.getPeople();
userProperties.set("Last name","abx");
userProperties.set("location","pakistan");
userProperties.set("Age",25);

✍🏽Note:

⚠️⚠️Pick your user properties wisely. Once you set the user properties there is no want to change them.⚠️⚠️

For more about Mixpanel implementation or documentation please go to the office link: Mixpanel Android Office Documentation.

Find the Git code here

https://github.com/taimur113311/mixpanel-android

Enjoy and feel 🆓 to leave a comment 💬 💬 if something is not clear or if you have questions. And if you like it please 👏 and share it!

Thank you for reading! 📕❤️🙌🙏😍✌

--

--