Skip to content

Instantly share code, notes, and snippets.

@jaisonfdo
Created November 9, 2016 12:22
Show Gist options
  • Save jaisonfdo/bb4608c6b493b077d2eb5dac4d598bf2 to your computer and use it in GitHub Desktop.
Save jaisonfdo/bb4608c6b493b077d2eb5dac4d598bf2 to your computer and use it in GitHub Desktop.
Track events with Mixpanel in Android.For more information, check out my detailed guide here : http://droidmentor.com/track-events-mixpanel-android/
import android.content.Context;
import android.util.Log;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import org.json.JSONException;
import org.json.JSONObject;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import droidmentor.checking.R;
import droidmentor.checking.Utils.MySharedPreferences;
/**
* Created by Jaison
*/
public class MixPanelHelper
{
private MixpanelAPI mixpanel;
private MySharedPreferences mySharedPreferences;
public MixPanelHelper(Context context)
{
mySharedPreferences=new MySharedPreferences(context);
String projectnumber=context.getString(R.string.google_project_number);
String projectToken = context.getString(R.string.mixpanel_token);
mixpanel = MixpanelAPI.getInstance(context, projectToken);
// mixpanel.getPeople().initPushHandling(projectnumber);
if(mySharedPreferences.get_mixpanel_people_registered())
{
mixpanel.identify(mySharedPreferences.getUserID());
mixpanel.getPeople().identify(mySharedPreferences.getUserID());
}
}
public void clearProfile()
{
mySharedPreferences.reset();
}
public void set_profile()
{
JSONObject profileProps = new JSONObject();
try {
mySharedPreferences.setUserID("12PCA103");
mySharedPreferences.setUsername("Jaison");
profileProps.put("$user_id",mySharedPreferences.getUserID());
profileProps.put("$platform", "android");
profileProps.put("$name",mySharedPreferences.getUserName());
profileProps.put("$created",getdate(new Date()));
if(!mySharedPreferences.get_mixpanel_people_registered())
{
mixpanel.getPeople().set(profileProps);
mixpanel.identify(mySharedPreferences.getUserID());
mixpanel.getPeople().identify(mySharedPreferences.getUserID());
mySharedPreferences.set_mixpanel_people_registered(true);
}
} catch (JSONException e) {
Log.e("MYAPP", "Unable to add properties to JSONObject", e);
}
}
private String getdate(Date d)
{
DateFormat df2 = new SimpleDateFormat("MMM dd,yyyy hh:mm aaa");
return df2.format(d);
}
private JSONObject get_basic_property()
{
JSONObject props = new JSONObject();
try {
props.put("$user_id",mySharedPreferences.getUserID());
props.put("$name",mySharedPreferences.getUserName());
props.put("$platform", "android");
} catch (JSONException e) {
Log.e("MYAPP", "Unable to add properties to JSONObject", e);
}
return props;
}
public void triggerEvent(String eventName)
{
JSONObject props;
props=get_basic_property();
mixpanel.track(eventName, props);
}
public void triggerEvent(String eventName,JSONObject proprety)
{
JSONObject props,merge;
props=get_basic_property();
mixpanel.track(eventName, mergeJSONObjects(props,proprety));
}
public static JSONObject mergeJSONObjects(JSONObject json1, JSONObject json2)
{
Iterator key=json2.keys();
try {
while (key.hasNext()) {
String k = key.next().toString();
json1.put(k,json2.get(k));
}
} catch (JSONException e) {
throw new RuntimeException("JSON Exception" + e);
}
return json1;
}
}
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import org.json.JSONObject;
import droidmentor.checking.R;
public class MixpanelActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_login,btn_logout,btn_event1,btn_event2;
MixPanelHelper mixPanelHelper;
String TAG="Mixpnel";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mixpanel);
mixPanelHelper=new MixPanelHelper(this);
btn_login=(Button)findViewById(R.id.btn_login);
btn_logout=(Button)findViewById(R.id.btn_logout);
btn_event1=(Button)findViewById(R.id.btn_event1);
btn_event2=(Button)findViewById(R.id.btn_event2);
btn_login.setOnClickListener(this);
btn_logout.setOnClickListener(this);
btn_event1.setOnClickListener(this);
btn_event2.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.btn_login:
mixPanelHelper.set_profile();
mixPanelHelper.triggerEvent("Login");
Log.d(TAG, "onClick: Login");
break;
case R.id.btn_logout:
mixPanelHelper.triggerEvent("Logout");
mixPanelHelper.clearProfile();
Log.d(TAG, "onClick: Logout");
break;
case R.id.btn_event1:
mixPanelHelper.triggerEvent("Event1");
Log.d(TAG, "onClick: Event1");
break;
case R.id.btn_event2:
JSONObject props=new JSONObject();
try {
props.put("$language","English");
}
catch(Exception e)
{
e.printStackTrace();
}
mixPanelHelper.triggerEvent("Event2",props);
Log.d(TAG, "onClick: Event2");
break;
}
}
}
import android.content.Context;
import android.content.SharedPreferences;
/**
* Created by Jaison
*/
public class MySharedPreferences {
private static final String APP_SHARED_PREFS = "CheckingPref";
private static final String userName="userName";
private static final String userID="userID";
private static final String mixpanel_people_registered="mixpanel_people_registered";
private SharedPreferences appSharedPrefs;
private SharedPreferences.Editor prefsEditor;
public MySharedPreferences(Context context)
{
this.appSharedPrefs = context.getSharedPreferences(APP_SHARED_PREFS, Context.MODE_PRIVATE);
this.prefsEditor = appSharedPrefs.edit();
}
public void reset()
{
prefsEditor.clear();
prefsEditor.commit();
}
//mixpanel registration
public boolean get_mixpanel_people_registered() {
return appSharedPrefs.getBoolean(mixpanel_people_registered, false);
}
public void set_mixpanel_people_registered(boolean i)
{
prefsEditor.putBoolean(mixpanel_people_registered, i);
prefsEditor.commit();
}
public String getUserName() {
return appSharedPrefs.getString(userName,"Guest");
}
public String getUserID() {
return appSharedPrefs.getString(userID,"12PCA100");
}
public void setUsername(String UserName)
{
prefsEditor.putString(userName, UserName);
prefsEditor.commit();
}
public void setUserID(String UserID)
{
prefsEditor.putString(userID, UserID);
prefsEditor.commit();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment