public class MixpanelAPI
extends java.lang.Object
Call getInstance(Context, String) with
your main application activity and your Mixpanel API token as arguments
an to get an instance you can use to report how users are using your
application.
Once you have an instance, you can send events to Mixpanel
using track(String, JSONObject), and update People Analytics
records with getPeople()
The Mixpanel library will periodically send information to
Mixpanel servers, so your application will need to have
android.permission.INTERNET. In addition, to preserve
battery life, messages to Mixpanel servers may not be sent immediately
when you call track(String)or MixpanelAPI.People.set(String, Object).
The library will send messages periodically throughout the lifetime
of your application, but you will need to call flush()
before your application is completely shutdown to ensure all of your
events are sent.
A typical use-case for the library might look like this:
public class MainActivity extends Activity {
MixpanelAPI mMixpanel;
public void onCreate(Bundle saved) {
mMixpanel = MixpanelAPI.getInstance(this, "YOUR MIXPANEL API TOKEN");
...
}
public void whenSomethingInterestingHappens(int flavor) {
JSONObject properties = new JSONObject();
properties.put("flavor", flavor);
mMixpanel.track("Something Interesting Happened", properties);
...
}
public void onDestroy() {
mMixpanel.flush();
super.onDestroy();
}
}
In addition to this documentation, you may wish to take a look at
the Mixpanel sample Android application.
It demonstrates a variety of techniques, including
updating People Analytics records with MixpanelAPI.People and others.
There are also step-by-step getting started documents available at mixpanel.com
| Modifier and Type | Class and Description |
|---|---|
static interface |
MixpanelAPI.Group
Core interface for using Mixpanel Group Analytics features.
|
static interface |
MixpanelAPI.People
Core interface for using Mixpanel People Analytics features.
|
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
VERSION
String version of the library.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addGroup(java.lang.String groupKey,
java.lang.Object groupID)
Add a group to this user's membership for a particular group key
|
void |
alias(java.lang.String alias,
java.lang.String original)
This function creates a distinct_id alias from alias to original.
|
static Tweak<java.lang.Boolean> |
booleanTweak(java.lang.String tweakName,
boolean defaultValue)
Declare a boolean-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.Byte> |
byteTweak(java.lang.String tweakName,
byte defaultValue)
Declare byte-valued tweak, and return a reference you can use to read the value of the tweak.
|
void |
clearSuperProperties()
Erase all currently registered superProperties.
|
static Tweak<java.lang.Double> |
doubleTweak(java.lang.String tweakName,
double defaultValue)
Declare a double-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.Double> |
doubleTweak(java.lang.String tweakName,
double defaultValue,
double minimumValue,
double maximumValue)
Declare a double-valued tweak, and return a reference you can use to read the value of the tweak.
|
double |
eventElapsedTime(java.lang.String eventName)
Retrieves the time elapsed for the named event since timeEvent() was called.
|
static Tweak<java.lang.Float> |
floatTweak(java.lang.String tweakName,
float defaultValue)
Declare a float-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.Float> |
floatTweak(java.lang.String tweakName,
float defaultValue,
float minimumValue,
float maximumValue)
Declare a float-valued tweak, and return a reference you can use to read the value of the tweak.
|
void |
flush()
Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
|
protected void |
flushNoDecideCheck() |
protected java.lang.String |
getAnonymousId()
Returns the anonymoous id currently being used to uniquely identify the device and all
with events sent using
track(String, JSONObject) will have this id as a device
id |
java.util.Map<java.lang.String,java.lang.String> |
getDeviceInfo()
Returns an unmodifiable map that contains the device description properties
that will be sent to Mixpanel.
|
java.lang.String |
getDistinctId()
Returns the string id currently being used to uniquely identify the user associated
with events sent using
track(String, JSONObject). |
MixpanelAPI.Group |
getGroup(java.lang.String groupKey,
java.lang.Object groupID)
Returns a Mixpanel.Group object that can be used to set and increment
Group Analytics properties.
|
static MixpanelAPI |
getInstance(android.content.Context context,
java.lang.String token)
Get the instance of MixpanelAPI associated with your Mixpanel project token.
|
static MixpanelAPI |
getInstance(android.content.Context context,
java.lang.String token,
boolean optOutTrackingDefault)
Get the instance of MixpanelAPI associated with your Mixpanel project token.
|
static MixpanelAPI |
getInstance(android.content.Context context,
java.lang.String token,
boolean optOutTrackingDefault,
org.json.JSONObject superProperties)
Get the instance of MixpanelAPI associated with your Mixpanel project token.
|
static MixpanelAPI |
getInstance(android.content.Context context,
java.lang.String token,
org.json.JSONObject superProperties)
Get the instance of MixpanelAPI associated with your Mixpanel project token.
|
MixpanelAPI.People |
getPeople()
Returns a Mixpanel.People object that can be used to set and increment
People Analytics properties.
|
org.json.JSONObject |
getSuperProperties()
Returns a json object of the user's current super properties
|
protected java.lang.String |
getUserId()
Returns the user id with which identify is called and all the with events sent using
track(String, JSONObject) will have this id as a user id |
boolean |
hasOptedOutTracking()
Will return true if the user has opted out from tracking.
|
void |
identify(java.lang.String distinctId)
Associate all future calls to
track(String, JSONObject) with the user identified by
the given distinct id. |
static Tweak<java.lang.Integer> |
intTweak(java.lang.String tweakName,
int defaultValue)
Declare an int-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.Integer> |
intTweak(java.lang.String tweakName,
int defaultValue,
int minimumValue,
int maximumValue)
Declare an int-valued tweak, and return a reference you can use to read the value of the tweak.
|
boolean |
isAppInForeground()
Based on the application's event lifecycle this method will determine whether the app
is running in the foreground or not.
|
static Tweak<java.lang.Long> |
longTweak(java.lang.String tweakName,
long defaultValue)
Declare a long-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.Long> |
longTweak(java.lang.String tweakName,
long defaultValue,
long minimumValue,
long maximumValue)
Declare a long-valued tweak, and return a reference you can use to read the value of the tweak.
|
void |
optInTracking()
Use this method to opt-in an already opted-out user from tracking.
|
void |
optInTracking(java.lang.String distinctId)
Use this method to opt-in an already opted-out user from tracking.
|
void |
optInTracking(java.lang.String distinctId,
org.json.JSONObject properties)
Use this method to opt-in an already opted-out user from tracking.
|
void |
optOutTracking()
Use this method to opt-out a user from tracking.
|
void |
registerSuperProperties(org.json.JSONObject superProperties)
Register properties that will be sent with every subsequent call to
track(String, JSONObject). |
void |
registerSuperPropertiesMap(java.util.Map<java.lang.String,java.lang.Object> superProperties)
Register properties that will be sent with every subsequent call to
track(String, JSONObject). |
void |
registerSuperPropertiesOnce(org.json.JSONObject superProperties)
Register super properties for events, only if no other super property with the
same names has already been registered.
|
void |
registerSuperPropertiesOnceMap(java.util.Map<java.lang.String,java.lang.Object> superProperties)
Register super properties for events, only if no other super property with the
same names has already been registered.
|
void |
removeGroup(java.lang.String groupKey,
java.lang.Object groupID)
Remove a group from this user's membership for a particular group key
|
void |
reset()
Clears tweaks and all distinct_ids, superProperties, and push registrations from persistent storage.
|
void |
setGroup(java.lang.String groupKey,
java.util.List<java.lang.Object> groupIDs)
Set the groups this user belongs to.
|
void |
setGroup(java.lang.String groupKey,
java.lang.Object groupID)
Set the group this user belongs to.
|
static Tweak<java.lang.Short> |
shortTweak(java.lang.String tweakName,
short defaultValue)
Declare short-valued tweak, and return a reference you can use to read the value of the tweak.
|
static Tweak<java.lang.String> |
stringTweak(java.lang.String tweakName,
java.lang.String defaultValue)
Declare a string-valued tweak, and return a reference you can use to read the value of the tweak.
|
void |
timeEvent(java.lang.String eventName)
Begin timing of an event.
|
void |
track(java.lang.String eventName)
Equivalent to
track(String, JSONObject) with a null argument for properties. |
void |
track(java.lang.String eventName,
org.json.JSONObject properties)
Track an event.
|
protected void |
track(java.lang.String eventName,
org.json.JSONObject properties,
boolean isAutomaticEvent) |
void |
trackMap(java.lang.String eventName,
java.util.Map<java.lang.String,java.lang.Object> properties)
Track an event.
|
void |
trackWithGroups(java.lang.String eventName,
java.util.Map<java.lang.String,java.lang.Object> properties,
java.util.Map<java.lang.String,java.lang.Object> groups)
Track an event with specific groups.
|
void |
unregisterSuperProperty(java.lang.String superPropertyName)
Remove a single superProperty, so that it will not be sent with future calls to
track(String, JSONObject). |
void |
updateSuperProperties(SuperPropertyUpdate update)
Updates super properties in place.
|
public static final java.lang.String VERSION
public static Tweak<java.lang.String> stringTweak(java.lang.String tweakName, java.lang.String defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Boolean> booleanTweak(java.lang.String tweakName, boolean defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Double> doubleTweak(java.lang.String tweakName, double defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Double> doubleTweak(java.lang.String tweakName, double defaultValue, double minimumValue, double maximumValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.minimumValue - Minimum numeric value of your tweak.maximumValue - Maximum numeric value of your tweak.public static Tweak<java.lang.Float> floatTweak(java.lang.String tweakName, float defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Float> floatTweak(java.lang.String tweakName, float defaultValue, float minimumValue, float maximumValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.minimumValue - Minimum numeric value of your tweak.maximumValue - Maximum numeric value of your tweak.public static Tweak<java.lang.Long> longTweak(java.lang.String tweakName, long defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Long> longTweak(java.lang.String tweakName, long defaultValue, long minimumValue, long maximumValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.minimumValue - Minimum numeric value of your tweak.maximumValue - Maximum numeric value of your tweak.public static Tweak<java.lang.Integer> intTweak(java.lang.String tweakName, int defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Integer> intTweak(java.lang.String tweakName, int defaultValue, int minimumValue, int maximumValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.minimumValue - Minimum numeric value of your tweak.maximumValue - Maximum numeric value of your tweak.public static Tweak<java.lang.Short> shortTweak(java.lang.String tweakName, short defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static Tweak<java.lang.Byte> byteTweak(java.lang.String tweakName, byte defaultValue)
tweakName - Unique name to identify your tweak.defaultValue - Default value of your tweak.public static MixpanelAPI getInstance(android.content.Context context, java.lang.String token)
Use getInstance to get a reference to a shared instance of MixpanelAPI you can use to send events and People Analytics updates to Mixpanel.
getInstance is thread safe, but the returned instance is not, and may be shared with other callers of getInstance. The best practice is to call getInstance, and use the returned MixpanelAPI, object from a single thread (probably the main UI thread of your application).
If you do choose to track events from multiple threads in your application, you should synchronize your calls on the instance itself, like so:
MixpanelAPI instance = MixpanelAPI.getInstance(context, token);
synchronized(instance) { // Only necessary if the instance will be used in multiple threads.
instance.track(...)
}
context - The application context you are trackingtoken - Your Mixpanel project token. You can get your project token on the Mixpanel web site,
in the settings dialog.public static MixpanelAPI getInstance(android.content.Context context, java.lang.String token, boolean optOutTrackingDefault)
Use getInstance to get a reference to a shared instance of MixpanelAPI you can use to send events and People Analytics updates to Mixpanel.
getInstance is thread safe, but the returned instance is not, and may be shared with other callers of getInstance. The best practice is to call getInstance, and use the returned MixpanelAPI, object from a single thread (probably the main UI thread of your application).
If you do choose to track events from multiple threads in your application, you should synchronize your calls on the instance itself, like so:
MixpanelAPI instance = MixpanelAPI.getInstance(context, token);
synchronized(instance) { // Only necessary if the instance will be used in multiple threads.
instance.track(...)
}
context - The application context you are trackingtoken - Your Mixpanel project token. You can get your project token on the Mixpanel web site,
in the settings dialog.optOutTrackingDefault - Whether or not Mixpanel can start tracking by default. See
optOutTracking().public static MixpanelAPI getInstance(android.content.Context context, java.lang.String token, org.json.JSONObject superProperties)
Use getInstance to get a reference to a shared instance of MixpanelAPI you can use to send events and People Analytics updates to Mixpanel.
getInstance is thread safe, but the returned instance is not, and may be shared with other callers of getInstance. The best practice is to call getInstance, and use the returned MixpanelAPI, object from a single thread (probably the main UI thread of your application).
If you do choose to track events from multiple threads in your application, you should synchronize your calls on the instance itself, like so:
MixpanelAPI instance = MixpanelAPI.getInstance(context, token);
synchronized(instance) { // Only necessary if the instance will be used in multiple threads.
instance.track(...)
}
context - The application context you are trackingtoken - Your Mixpanel project token. You can get your project token on the Mixpanel web site,
in the settings dialog.superProperties - A JSONObject containing super properties to register.public static MixpanelAPI getInstance(android.content.Context context, java.lang.String token, boolean optOutTrackingDefault, org.json.JSONObject superProperties)
Use getInstance to get a reference to a shared instance of MixpanelAPI you can use to send events and People Analytics updates to Mixpanel.
getInstance is thread safe, but the returned instance is not, and may be shared with other callers of getInstance. The best practice is to call getInstance, and use the returned MixpanelAPI, object from a single thread (probably the main UI thread of your application).
If you do choose to track events from multiple threads in your application, you should synchronize your calls on the instance itself, like so:
MixpanelAPI instance = MixpanelAPI.getInstance(context, token);
synchronized(instance) { // Only necessary if the instance will be used in multiple threads.
instance.track(...)
}
context - The application context you are trackingtoken - Your Mixpanel project token. You can get your project token on the Mixpanel web site,
in the settings dialog.optOutTrackingDefault - Whether or not Mixpanel can start tracking by default. See
optOutTracking().superProperties - A JSONObject containing super properties to register.public void alias(java.lang.String alias,
java.lang.String original)
identify(String) is called.
This call does not identify the user after. You must still call both identify(String) and
MixpanelAPI.People.identify(String) if you wish the new alias to be used for Events and People.
alias - the new distinct_id that should represent original.original - the old distinct_id that alias will be mapped to.public void identify(java.lang.String distinctId)
track(String, JSONObject) with the user identified by
the given distinct id.
This call does not identify the user for People Analytics;
to do that, see MixpanelAPI.People.identify(String). Mixpanel recommends using
the same distinct_id for both calls, and using a distinct_id that is easy
to associate with the given user, for example, a server-side account identifier.
Calls to track(String, JSONObject) made before corresponding calls to identify
will use an anonymous locally generated distinct id, which means it is best to call identify
early to ensure that your Mixpanel funnels and retention analytics can continue to track the
user throughout their lifetime. We recommend calling identify when the user authenticates.
Once identify is called, the local distinct id persists across restarts of your application.
distinctId - a string uniquely identifying this user. Events sent to
Mixpanel using the same disinct_id will be considered associated with the
same visitor/customer for retention and funnel reporting, so be sure that the given
value is globally unique for each individual user you intend to track.MixpanelAPI.People.identify(String)public void timeEvent(java.lang.String eventName)
eventName - the name of the event to track with timing.public double eventElapsedTime(java.lang.String eventName)
eventName - the name of the event to be tracked that was previously called with timeEvent()timeEvent(String) was called for the given eventName.public void trackMap(java.lang.String eventName,
java.util.Map<java.lang.String,java.lang.Object> properties)
Every call to track eventually results in a data point sent to Mixpanel. These data points are what are measured, counted, and broken down to create your Mixpanel reports. Events have a string name, and an optional set of name/value pairs that describe the properties of that event.
eventName - The name of the event to sendproperties - A Map containing the key value pairs of the properties to include in this event.
Pass null if no extra properties exist.
See also track(String, org.json.JSONObject)public void trackWithGroups(java.lang.String eventName,
java.util.Map<java.lang.String,java.lang.Object> properties,
java.util.Map<java.lang.String,java.lang.Object> groups)
Every call to track eventually results in a data point sent to Mixpanel. These data points are what are measured, counted, and broken down to create your Mixpanel reports. Events have a string name, and an optional set of name/value pairs that describe the properties of that event. Group key/value pairs are upserted into the property map before tracking.
eventName - The name of the event to sendproperties - A Map containing the key value pairs of the properties to include in this event.
Pass null if no extra properties exist.groups - A Map containing the group key value pairs for this event.
See also track(String, org.json.JSONObject), trackMap(String, Map)public void track(java.lang.String eventName,
org.json.JSONObject properties)
Every call to track eventually results in a data point sent to Mixpanel. These data points are what are measured, counted, and broken down to create your Mixpanel reports. Events have a string name, and an optional set of name/value pairs that describe the properties of that event.
eventName - The name of the event to sendproperties - A JSONObject containing the key value pairs of the properties to include in this event.
Pass null if no extra properties exist.public void track(java.lang.String eventName)
track(String, JSONObject) with a null argument for properties.
Consider adding properties to your tracking to get the best insights and experience from Mixpanel.eventName - the name of the event to sendpublic void flush()
Events and People messages are pushed gradually throughout the lifetime of your application. This means that to ensure that all messages are sent to Mixpanel when your application is shut down, you will need to call flush() to let the Mixpanel library know it should send all remaining messages to the server. We strongly recommend placing a call to flush() in the onDestroy() method of your main application activity.
public org.json.JSONObject getSuperProperties()
SuperProperties are a collection of properties that will be sent with every event to Mixpanel, and persist beyond the lifetime of your application.
public java.lang.String getDistinctId()
track(String, JSONObject). Before any calls to
identify(String), this will be an id automatically generated by the library.
The id returned by getDistinctId is independent of the distinct id used to identify
any People Analytics properties in Mixpanel. To read and write that identifier,
use MixpanelAPI.People.identify(String) and MixpanelAPI.People.getDistinctId().
identify(String),
MixpanelAPI.People.getDistinctId()protected java.lang.String getAnonymousId()
track(String, JSONObject) will have this id as a device
idprotected java.lang.String getUserId()
track(String, JSONObject) will have this id as a user idpublic void registerSuperPropertiesMap(java.util.Map<java.lang.String,java.lang.Object> superProperties)
track(String, JSONObject).
SuperProperties are a collection of properties that will be sent with every event to Mixpanel, and persist beyond the lifetime of your application.
Setting a superProperty with registerSuperProperties will store a new superProperty,
possibly overwriting any existing superProperty with the same name (to set a
superProperty only if it is currently unset, use registerSuperPropertiesOnce(JSONObject))
SuperProperties will persist even if your application is taken completely out of memory.
to remove a superProperty, call unregisterSuperProperty(String) or clearSuperProperties()
superProperties - A Map containing super properties to register
See also registerSuperProperties(org.json.JSONObject)public void registerSuperProperties(org.json.JSONObject superProperties)
track(String, JSONObject).
SuperProperties are a collection of properties that will be sent with every event to Mixpanel, and persist beyond the lifetime of your application.
Setting a superProperty with registerSuperProperties will store a new superProperty,
possibly overwriting any existing superProperty with the same name (to set a
superProperty only if it is currently unset, use registerSuperPropertiesOnce(JSONObject))
SuperProperties will persist even if your application is taken completely out of memory.
to remove a superProperty, call unregisterSuperProperty(String) or clearSuperProperties()
superProperties - A JSONObject containing super properties to registerregisterSuperPropertiesOnce(JSONObject),
unregisterSuperProperty(String),
clearSuperProperties()public void unregisterSuperProperty(java.lang.String superPropertyName)
track(String, JSONObject).
If there is a superProperty registered with the given name, it will be permanently
removed from the existing superProperties.
To clear all superProperties, use clearSuperProperties()
superPropertyName - name of the property to unregisterregisterSuperProperties(JSONObject)public void registerSuperPropertiesOnceMap(java.util.Map<java.lang.String,java.lang.Object> superProperties)
Calling registerSuperPropertiesOnce will never overwrite existing properties.
superProperties - A Map containing the super properties to register.
See also registerSuperPropertiesOnce(org.json.JSONObject)public void registerSuperPropertiesOnce(org.json.JSONObject superProperties)
Calling registerSuperPropertiesOnce will never overwrite existing properties.
superProperties - A JSONObject containing the super properties to register.registerSuperProperties(JSONObject)public void clearSuperProperties()
Future tracking calls to Mixpanel will not contain the specific superProperties registered before the clearSuperProperties method was called.
To remove a single superProperty, use unregisterSuperProperty(String)
registerSuperProperties(JSONObject)public void updateSuperProperties(SuperPropertyUpdate update)
update - A function from one set of super properties to another. The update should not return null.public void setGroup(java.lang.String groupKey,
java.lang.Object groupID)
groupKey - The property name associated with this group type (must already have been set up).groupID - The group the user belongs to.public void setGroup(java.lang.String groupKey,
java.util.List<java.lang.Object> groupIDs)
groupKey - The property name associated with this group type (must already have been set up).groupIDs - The list of groups the user belongs to.public void addGroup(java.lang.String groupKey,
java.lang.Object groupID)
groupKey - The property name associated with this group type (must already have been set up).groupID - The new group the user belongs to.public void removeGroup(java.lang.String groupKey,
java.lang.Object groupID)
groupKey - The property name associated with this group type (must already have been set up).groupID - The group value to remove.public MixpanelAPI.People getPeople()
MixpanelAPI.People that you can use to update
records in Mixpanel People Analytics and manage Mixpanel Firebase Cloud Messaging notifications.public MixpanelAPI.Group getGroup(java.lang.String groupKey, java.lang.Object groupID)
groupKey - String identifying the type of group (must be already in use as a group key)groupID - Object identifying the specific groupMixpanelAPI.Group that you can use to update
records in Mixpanel Group Analyticspublic void reset()
public java.util.Map<java.lang.String,java.lang.String> getDeviceInfo()
public void optOutTracking()
flush() before calling this method if you want
to send all the queues to Mixpanel before.
This method will also remove any user-related information from the device.public void optInTracking()
optInTracking(String) and
optInTracking(String, JSONObject)
See also optOutTracking().public void optInTracking(java.lang.String distinctId)
distinctId - Optional string to use as the distinct ID for events.
This will call identify(String).
If you use people profiles make sure you manually call
MixpanelAPI.People.identify(String) after this method.
See also optInTracking(String), optInTracking(String, JSONObject) and
optOutTracking().public void optInTracking(java.lang.String distinctId,
org.json.JSONObject properties)
distinctId - Optional string to use as the distinct ID for events.
This will call identify(String).
If you use people profiles make sure you manually call
MixpanelAPI.People.identify(String) after this method.properties - Optional JSONObject that could be passed to add properties to the
opt-in event that is sent to Mixpanel.
See also optInTracking() and optOutTracking().public boolean hasOptedOutTracking()
optOutTracking() and
getInstance(Context, String, boolean, JSONObject) for more information.public boolean isAppInForeground()
protected void flushNoDecideCheck()
protected void track(java.lang.String eventName,
org.json.JSONObject properties,
boolean isAutomaticEvent)