Last active
December 16, 2015 09:45
-
-
Save jfsso/1821fd5e3456b626b92f to your computer and use it in GitHub Desktop.
MixpanelAPI wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import com.mixpanel.android.mpmetrics.MixpanelAPI; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import java.util.HashSet; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
import javax.inject.Inject; | |
import javax.inject.Singleton; | |
import timber.log.Timber; | |
public class Mixpanel { | |
private MixpanelAPI mMixpanelAPI; | |
private PropertiesSetter mPropertiesSetter; | |
public Mixpanel(MixpanelAPI mixpanelAPI, PropertiesSetter eventPropertiesSetter) { | |
mMixpanelAPI = mixpanelAPI; | |
mPropertiesSetter = eventPropertiesSetter; | |
} | |
public void identify(String distinctId) { | |
mMixpanelAPI.identify(distinctId); | |
} | |
public void alias(String alias, String original) { | |
mMixpanelAPI.alias(alias, original); | |
} | |
public String getDistinctId() { | |
return mMixpanelAPI.getDistinctId(); | |
} | |
public void flush() { | |
mMixpanelAPI.flush(); | |
} | |
public Event event(String name) { | |
return new Event(mMixpanelAPI, name, mPropertiesSetter); | |
} | |
public void timeEvent(String name) { | |
mMixpanelAPI.timeEvent(name); | |
} | |
public PeopleProperties people() { | |
return new PeopleProperties(mMixpanelAPI); | |
} | |
public SuperProperties superProperties() { | |
return new SuperProperties(mMixpanelAPI); | |
} | |
public static class Event { | |
private final MixpanelAPI mMixpanelAPI; | |
private final JSONObject props; | |
private final String name; | |
private final PropertiesSetter propertiesSetter; | |
private Event(MixpanelAPI mixpanelAPI, String name, PropertiesSetter propertiesSetter) { | |
this.mMixpanelAPI = mixpanelAPI; | |
this.props = new JSONObject(); | |
this.name = name; | |
this.propertiesSetter = propertiesSetter; | |
} | |
public Event put(String property, Object value) { | |
try { | |
props.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error build properties object"); | |
} | |
return this; | |
} | |
public void track() { | |
try { | |
if (propertiesSetter != null) | |
propertiesSetter.execute(props); | |
} catch (JSONException e) { | |
Timber.e(e, "error build properties object"); | |
} | |
mMixpanelAPI.track(name, props); | |
} | |
} | |
public static class PeopleProperties { | |
private final MixpanelAPI mixpanelAPI; | |
private final JSONObject props; | |
private final JSONObject propsOnce; | |
private final JSONObject superProps; | |
private final Map<String, Double> increments; | |
protected PeopleProperties(MixpanelAPI mixpanelAPI) { | |
this.mixpanelAPI = mixpanelAPI; | |
this.props = new JSONObject(); | |
this.propsOnce = new JSONObject(); | |
this.superProps = new JSONObject(); | |
this.increments = new LinkedHashMap<>(); | |
} | |
public void identify(String distinctId) { | |
mixpanelAPI.getPeople().identify(distinctId); | |
} | |
public void deleteUser() { | |
mixpanelAPI.getPeople().deleteUser(); | |
} | |
public void initPushHandling(String senderId) { | |
mixpanelAPI.getPeople().initPushHandling(senderId); | |
} | |
public String getDistinctId() { | |
return mixpanelAPI.getPeople().getDistinctId(); | |
} | |
public void setPushRegistrationId(String registrationId) { | |
mixpanelAPI.getPeople().setPushRegistrationId(registrationId); | |
} | |
public void showNotificationIfAvailable(Activity activity) { | |
mixpanelAPI.getPeople().showNotificationIfAvailable(activity); | |
} | |
public void showSurveyIfAvailable(Activity activity) { | |
mixpanelAPI.getPeople().showSurveyIfAvailable(activity); | |
} | |
public PeopleProperties increment(String property, double value) { | |
increments.put(property, value); | |
return this; | |
} | |
public PeopleProperties put(String property, Object value) { | |
try { | |
props.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error building properties object"); | |
} | |
return this; | |
} | |
public PeopleProperties putOnce(String property, Object value) { | |
try { | |
propsOnce.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error building properties object"); | |
} | |
return this; | |
} | |
public PeopleProperties putWithSuper(String property, Object value) { | |
try { | |
props.put(property, value); | |
superProps.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error building properties object"); | |
} | |
return this; | |
} | |
public void send() { | |
if (props.length() > 0) | |
mixpanelAPI.getPeople().set(props); | |
if (propsOnce.length() > 0) | |
mixpanelAPI.getPeople().setOnce(propsOnce); | |
if (increments.size() > 0) | |
mixpanelAPI.getPeople().increment(increments); | |
if (superProps.length() > 0) | |
mixpanelAPI.registerSuperProperties(superProps); | |
} | |
} | |
public static class SuperProperties { | |
private final MixpanelAPI mixpanelAPI; | |
private final JSONObject props; | |
private final JSONObject propsOnce; | |
private final HashSet<String> removeProps; | |
private SuperProperties(MixpanelAPI mixpanelAPI) { | |
this.mixpanelAPI = mixpanelAPI; | |
this.props = new JSONObject(); | |
this.propsOnce = new JSONObject(); | |
this.removeProps = new HashSet<>(); | |
} | |
public SuperProperties put(String property, Object value) { | |
try { | |
props.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error building properties object"); | |
} | |
return this; | |
} | |
public SuperProperties putOnce(String property, Object value) { | |
try { | |
propsOnce.put(property, value); | |
} catch (JSONException e) { | |
Timber.e(e, "error building properties object"); | |
} | |
return this; | |
} | |
public SuperProperties remove(String property) { | |
removeProps.add(property); | |
return this; | |
} | |
public void save() { | |
if (props.length() > 0) | |
mixpanelAPI.registerSuperProperties(props); | |
if (propsOnce.length() > 0) | |
mixpanelAPI.registerSuperPropertiesOnce(propsOnce); | |
if (removeProps.size() > 0) | |
for (String prop : removeProps) | |
mixpanelAPI.unregisterSuperProperty(prop); | |
} | |
} | |
public interface PropertiesSetter { | |
void execute(JSONObject props) throws JSONException; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MixpanelEventPropertiesSetter implements Mixpanel.PropertiesSetter { | |
private Context mContext; | |
public MixpanelEventPropertiesSetter(Context context) { | |
mContext = context; | |
} | |
@Override | |
public void execute(JSONObject props) throws JSONException { | |
props.put("App Version Code", BuildConfig.VERSION_CODE); | |
props.put("Device Year Class", YearClass.get(mContext)); | |
props.put("Screen Orientation", ConfigurationUtils.getOrientationName(mContext)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Mixpanel mp = new Mixpanel(mixpanelAPI, eventPropertiesSetter); | |
// people props | |
mp.people() | |
.put("prop1", value) | |
.putWithSuper("notification setting", settingValue) | |
.increment("action count", 1) | |
.send(); | |
// super props | |
mp.superProperties() | |
.put("some config", configValue) | |
.remove("a prop") | |
.send(); | |
// events | |
mp.event("sign in button click") | |
.put("prop1", prop1) | |
.put("prop2", prop2) | |
.track(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment