![JAR search and dependency download from the Maven repository](/logo.png)
com.segment.analytics.MixpanelIntegration Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core Show documentation
Show all versions of core Show documentation
The hassle-free way to add analytics to your Android app.
package com.segment.analytics;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import org.json.JSONObject;
import static com.segment.analytics.Utils.isNullOrEmpty;
/**
* Mixpanel is an event tracking tool targeted at web apps with lots of features: funnel, retention
* and people tracking; advanced segmentation; and sending email and notifications.
*
* @see Mixpanel
* @see Mixpanel Integration
* @see Mixpanel Android SDK
*/
class MixpanelIntegration extends AbstractIntegration {
static final String MIXPANEL_KEY = "Mixpanel";
MixpanelAPI mixpanelAPI;
boolean isPeopleEnabled;
boolean trackAllPages;
boolean trackCategorizedPages;
boolean trackNamedPages;
String token;
@Override void initialize(Context context, ValueMap settings, boolean debuggingEnabled)
throws IllegalStateException {
trackAllPages = settings.getBoolean("trackAllPages", false);
trackCategorizedPages = settings.getBoolean("trackCategorizedPages", true);
trackNamedPages = settings.getBoolean("trackNamedPages", true);
isPeopleEnabled = settings.getBoolean("people", false);
token = settings.getString("token");
mixpanelAPI = MixpanelAPI.getInstance(context, token);
}
@Override void onActivityCreated(Activity activity, Bundle savedInstanceState) {
super.onActivityCreated(activity, savedInstanceState);
// This is needed to trigger a call to #checkIntentForInboundAppLink.
// From Mixpanel's source, this won't trigger a creation of another instance. It caches
// instances by the application context and token, both of which remain the same.
MixpanelAPI.getInstance(activity, token);
}
@Override MixpanelAPI getUnderlyingInstance() {
return mixpanelAPI;
}
@Override String key() {
return MIXPANEL_KEY;
}
@Override void identify(IdentifyPayload identify) {
super.identify(identify);
String userId = identify.userId();
mixpanelAPI.identify(userId);
JSONObject traits = identify.traits().toJsonObject();
mixpanelAPI.registerSuperProperties(traits);
if (isPeopleEnabled) {
MixpanelAPI.People people = mixpanelAPI.getPeople();
people.identify(userId);
people.set(traits);
}
}
@Override void flush() {
super.flush();
mixpanelAPI.flush();
}
@Override void alias(AliasPayload alias) {
super.alias(alias);
mixpanelAPI.alias(alias.userId(), alias.previousId());
}
@Override void screen(ScreenPayload screen) {
if (trackAllPages) {
event(String.format(VIEWED_EVENT_FORMAT, screen.event()), screen.properties());
} else if (trackCategorizedPages && !isNullOrEmpty(screen.category())) {
event(String.format(VIEWED_EVENT_FORMAT, screen.category()), screen.properties());
} else if (trackNamedPages && !isNullOrEmpty(screen.name())) {
event(String.format(VIEWED_EVENT_FORMAT, screen.name()), screen.properties());
}
}
@Override void track(TrackPayload track) {
event(track.event(), track.properties());
}
void event(String name, Properties properties) {
JSONObject props = properties.toJsonObject();
mixpanelAPI.track(name, props);
if (isPeopleEnabled) {
double revenue = properties.revenue();
if (revenue != 0) {
mixpanelAPI.getPeople().trackCharge(revenue, props);
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy