Class MixpanelAPI


  • public class MixpanelAPI
    extends java.lang.Object
    Core class for interacting with Mixpanel Analytics.

    Call getInstance(Context, String, boolean) 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

    See Also:
    getting started documentation for tracking events, getting started documentation for People Analytics, The Mixpanel Android sample application
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class 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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String VERSION
      String version of the library.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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 distinct_id)
      This function creates a distinct_id alias from alias to distinct_id.
      void clearSuperProperties()
      Erase all currently registered superProperties.
      void clearTimedEvent​(java.lang.String eventName)
      Clears the event timing for an event.
      void clearTimedEvents()
      Clears all current event timings.
      double eventElapsedTime​(java.lang.String eventName)
      Retrieves the time elapsed for the named event since timeEvent() was called.
      void flush()
      Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.
      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.
      int getFlushBatchSize()
      Get maximum number of events/updates to send in a single network request
      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, boolean trackAutomaticEvents)
      Get the instance of MixpanelAPI associated with your Mixpanel project token.
      static MixpanelAPI getInstance​(android.content.Context context, java.lang.String token, boolean optOutTrackingDefault, boolean trackAutomaticEvents)
      Get the instance of MixpanelAPI associated with your Mixpanel project token.
      static MixpanelAPI getInstance​(android.content.Context context, java.lang.String token, boolean optOutTrackingDefault, java.lang.String instanceName, boolean trackAutomaticEvents)
      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, java.lang.String instanceName, boolean trackAutomaticEvents)
      Get the instance of MixpanelAPI associated with your Mixpanel project token.
      static MixpanelAPI getInstance​(android.content.Context context, java.lang.String token, java.lang.String instanceName, boolean trackAutomaticEvents)
      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, boolean trackAutomaticEvents)
      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, java.lang.String instanceName, boolean trackAutomaticEvents)
      Get the instance of MixpanelAPI associated with your Mixpanel project token.
      int getMaximumDatabaseLimit()
      Get the maximum size limit to the Mixpanel database.
      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
      java.lang.Boolean getTrackAutomaticEvents()  
      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)
      Equivalent to identify(String, boolean) with a true argument for usePeople.
      void identify​(java.lang.String distinctId, boolean usePeople)
      Associate all future calls to track(String, JSONObject) with the user identified by the given distinct id.
      boolean isAppInForeground()
      Based on the application's event lifecycle this method will determine whether the app is running in the foreground or not.
      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 setEnableLogging​(boolean enableLogging)
      Controls whether to enable the run time debug logging
      void setFlushBatchSize​(int flushBatchSize)
      Set maximum number of events/updates to send in a single network request
      void setGroup​(java.lang.String groupKey, java.lang.Object groupID)
      Set the group this user belongs to.
      void setGroup​(java.lang.String groupKey, java.util.List<java.lang.Object> groupIDs)
      Set the groups this user belongs to.
      void setMaximumDatabaseLimit​(int maximumDatabaseLimit)
      Set an integer number of bytes, the maximum size limit to the Mixpanel database.
      void setServerURL​(java.lang.String serverURL)
      Set the base URL used for Mixpanel API requests.
      void setUseIpAddressForGeolocation​(boolean useIpAddressForGeolocation)
      Controls whether to automatically send the client IP Address as part of event tracking.
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • VERSION

        public static final java.lang.String VERSION
        String version of the library.
        See Also:
        Constant Field Values
    • Method Detail

      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - Your Mixpanel project token. You can get your project token on the Mixpanel web site, in the settings dialog.
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              java.lang.String instanceName,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - Your Mixpanel project token. You can get your project token on the Mixpanel web site, in the settings dialog.
        instanceName - The name you want to uniquely identify the Mixpanel Instance. It is useful when you want more than one Mixpanel instance under the same project token
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              boolean optOutTrackingDefault,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - 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().
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              boolean optOutTrackingDefault,
                                              java.lang.String instanceName,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - 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().
        instanceName - The name you want to uniquely identify the Mixpanel Instance. It is useful when you want more than one Mixpanel instance under the same project token.
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              org.json.JSONObject superProperties,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - 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.
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              org.json.JSONObject superProperties,
                                              java.lang.String instanceName,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - 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.
        instanceName - The name you want to uniquely identify the Mixpanel Instance. It is useful when you want more than one Mixpanel instance under the same project token
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • getInstance

        public static MixpanelAPI getInstance​(android.content.Context context,
                                              java.lang.String token,
                                              boolean optOutTrackingDefault,
                                              org.json.JSONObject superProperties,
                                              java.lang.String instanceName,
                                              boolean trackAutomaticEvents)
        Get the instance of MixpanelAPI associated with your Mixpanel project 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(...)
         }
         
         
        Parameters:
        context - The application context you are tracking
        token - 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.
        instanceName - The name you want to uniquely identify the Mixpanel Instance. It is useful when you want more than one Mixpanel instance under the same project token
        trackAutomaticEvents - Whether or not to collect common mobile events include app sessions, first app opens, app updated, etc.
        Returns:
        an instance of MixpanelAPI associated with your project
      • setUseIpAddressForGeolocation

        public void setUseIpAddressForGeolocation​(boolean useIpAddressForGeolocation)
        Controls whether to automatically send the client IP Address as part of event tracking.

        With an IP address, geo-location is possible down to neighborhoods within a city, although the Mixpanel Dashboard will just show you city level location specificity.

        Parameters:
        useIpAddressForGeolocation - If true, automatically send the client IP Address. Defaults to true.
      • setEnableLogging

        public void setEnableLogging​(boolean enableLogging)
        Controls whether to enable the run time debug logging
        Parameters:
        enableLogging - If true, emit more detailed log messages. Defaults to false
      • setFlushBatchSize

        public void setFlushBatchSize​(int flushBatchSize)
        Set maximum number of events/updates to send in a single network request
        Parameters:
        flushBatchSize - int, the number of events to be flushed at a time, defaults to 50
      • getFlushBatchSize

        public int getFlushBatchSize()
        Get maximum number of events/updates to send in a single network request
        Returns:
        the integer number of events to be flushed at a time
      • setMaximumDatabaseLimit

        public void setMaximumDatabaseLimit​(int maximumDatabaseLimit)
        Set an integer number of bytes, the maximum size limit to the Mixpanel database.
        Parameters:
        maximumDatabaseLimit - an integer number of bytes, the maximum size limit to the Mixpanel database.
      • getMaximumDatabaseLimit

        public int getMaximumDatabaseLimit()
        Get the maximum size limit to the Mixpanel database.
        Returns:
        an integer number of bytes, the maximum size limit to the Mixpanel database.
      • setServerURL

        public void setServerURL​(java.lang.String serverURL)
        Set the base URL used for Mixpanel API requests. Useful if you need to proxy Mixpanel requests. Defaults to https://api.mixpanel.com. To route data to Mixpanel's EU servers, set to https://api-eu.mixpanel.com
        Parameters:
        serverURL - the base URL used for Mixpanel API requests
      • getTrackAutomaticEvents

        public java.lang.Boolean getTrackAutomaticEvents()
      • alias

        public void alias​(java.lang.String alias,
                          java.lang.String distinct_id)
        This function creates a distinct_id alias from alias to distinct_id. If distinct_id is null, then it will create an alias to the current events distinct_id, which may be the distinct_id randomly generated by the Mixpanel library before identify(String) is called.

        This call does not identify the user after. You must still call identify(String) if you wish the new alias to be used for Events and People.

        Parameters:
        alias - the new value that should represent distinct_id.
        distinct_id - the old distinct_id that alias will be mapped to.
      • identify

        public void identify​(java.lang.String distinctId,
                             boolean usePeople)
        Associate all future calls to track(String, JSONObject) with the user identified by the given distinct id.

        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.

        Parameters:
        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.
        usePeople - boolean indicating whether or not to also call MixpanelAPI.People.identify(String)
      • timeEvent

        public void timeEvent​(java.lang.String eventName)
        Begin timing of an event. Calling timeEvent("Thing") will not send an event, but when you eventually call track("Thing"), your tracked event will be sent with a "$duration" property, representing the number of seconds between your calls.
        Parameters:
        eventName - the name of the event to track with timing.
      • clearTimedEvents

        public void clearTimedEvents()
        Clears all current event timings.
      • clearTimedEvent

        public void clearTimedEvent​(java.lang.String eventName)
        Clears the event timing for an event.
        Parameters:
        eventName - the name of the timed event to clear.
      • eventElapsedTime

        public double eventElapsedTime​(java.lang.String eventName)
        Retrieves the time elapsed for the named event since timeEvent() was called.
        Parameters:
        eventName - the name of the event to be tracked that was previously called with timeEvent()
        Returns:
        Time elapsed since timeEvent(String) was called for the given eventName.
      • trackMap

        public void trackMap​(java.lang.String eventName,
                             java.util.Map<java.lang.String,​java.lang.Object> properties)
        Track an event.

        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.

        Parameters:
        eventName - The name of the event to send
        properties - 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)
      • trackWithGroups

        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)
        Track an event with specific 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.

        Parameters:
        eventName - The name of the event to send
        properties - 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)
      • track

        public void track​(java.lang.String eventName,
                          org.json.JSONObject properties)
        Track an event.

        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.

        Parameters:
        eventName - The name of the event to send
        properties - A JSONObject containing the key value pairs of the properties to include in this event. Pass null if no extra properties exist.
      • track

        public void track​(java.lang.String eventName)
        Equivalent to track(String, JSONObject) with a null argument for properties. Consider adding properties to your tracking to get the best insights and experience from Mixpanel.
        Parameters:
        eventName - the name of the event to send
      • flush

        public void flush()
        Push all queued Mixpanel events and People Analytics changes to Mixpanel servers.

        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.

      • getSuperProperties

        public org.json.JSONObject getSuperProperties()
        Returns a json object of the user's current super properties

        SuperProperties are a collection of properties that will be sent with every event to Mixpanel, and persist beyond the lifetime of your application.

        Returns:
        Super properties for this Mixpanel instance.
      • getDistinctId

        public java.lang.String getDistinctId()
        Returns the string id currently being used to uniquely identify the user. Before any calls to identify(String), this will be an id automatically generated by the library.
        Returns:
        The distinct id that uniquely identifies the current user.
        See Also:
        identify(String)
      • getAnonymousId

        public 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
        Returns:
        The device id associated with event tracking
      • getUserId

        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
        Returns:
        The user id associated with event tracking
      • registerSuperPropertiesMap

        public 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).

        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()

        Parameters:
        superProperties - A Map containing super properties to register See also registerSuperProperties(org.json.JSONObject)
      • unregisterSuperProperty

        public void unregisterSuperProperty​(java.lang.String superPropertyName)
        Remove a single superProperty, so that it will not be sent with future calls to 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()

        Parameters:
        superPropertyName - name of the property to unregister
        See Also:
        registerSuperProperties(JSONObject)
      • registerSuperPropertiesOnceMap

        public 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.

        Calling registerSuperPropertiesOnce will never overwrite existing properties.

        Parameters:
        superProperties - A Map containing the super properties to register. See also registerSuperPropertiesOnce(org.json.JSONObject)
      • registerSuperPropertiesOnce

        public 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.

        Calling registerSuperPropertiesOnce will never overwrite existing properties.

        Parameters:
        superProperties - A JSONObject containing the super properties to register.
        See Also:
        registerSuperProperties(JSONObject)
      • clearSuperProperties

        public void clearSuperProperties()
        Erase all currently registered superProperties.

        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)

        See Also:
        registerSuperProperties(JSONObject)
      • updateSuperProperties

        public void updateSuperProperties​(SuperPropertyUpdate update)
        Updates super properties in place. Given a SuperPropertyUpdate object, will pass the current values of SuperProperties to that update and replace all results with the return value of the update. Updates are synchronized on the underlying super properties store, so they are guaranteed to be thread safe (but long running updates may slow down your tracking.)
        Parameters:
        update - A function from one set of super properties to another. The update should not return null.
      • setGroup

        public void setGroup​(java.lang.String groupKey,
                             java.lang.Object groupID)
        Set the group this user belongs to.
        Parameters:
        groupKey - The property name associated with this group type (must already have been set up).
        groupID - The group the user belongs to.
      • setGroup

        public void setGroup​(java.lang.String groupKey,
                             java.util.List<java.lang.Object> groupIDs)
        Set the groups this user belongs to.
        Parameters:
        groupKey - The property name associated with this group type (must already have been set up).
        groupIDs - The list of groups the user belongs to.
      • addGroup

        public void addGroup​(java.lang.String groupKey,
                             java.lang.Object groupID)
        Add a group to this user's membership for a particular group key
        Parameters:
        groupKey - The property name associated with this group type (must already have been set up).
        groupID - The new group the user belongs to.
      • removeGroup

        public void removeGroup​(java.lang.String groupKey,
                                java.lang.Object groupID)
        Remove a group from this user's membership for a particular group key
        Parameters:
        groupKey - The property name associated with this group type (must already have been set up).
        groupID - The group value to remove.
      • getPeople

        public MixpanelAPI.People getPeople()
        Returns a Mixpanel.People object that can be used to set and increment People Analytics properties.
        Returns:
        an instance of MixpanelAPI.People that you can use to update records in Mixpanel People Analytics.
      • getGroup

        public 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.
        Parameters:
        groupKey - String identifying the type of group (must be already in use as a group key)
        groupID - Object identifying the specific group
        Returns:
        an instance of MixpanelAPI.Group that you can use to update records in Mixpanel Group Analytics
      • reset

        public void reset()
        Clears tweaks and all distinct_ids, superProperties, and push registrations from persistent storage. Will not clear referrer information.
      • getDeviceInfo

        public 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. These are not all of the default properties, but are a subset that are dependant on the user's device or installed version of the host application, and are guaranteed not to change while the app is running.
        Returns:
        Map containing the device description properties that are sent to Mixpanel.
      • optOutTracking

        public void optOutTracking()
        Use this method to opt-out a user from tracking. Events and people updates that haven't been flushed yet will be deleted. Use 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.
      • optInTracking

        public void optInTracking()
        Use this method to opt-in an already opted-out user from tracking. People updates and track calls will be sent to Mixpanel after using this method. This method will internally track an opt-in event to your project. If you want to identify the opt-in event and/or pass properties to the event, see optInTracking(String) and optInTracking(String, JSONObject) See also optOutTracking().
      • optInTracking

        public void optInTracking​(java.lang.String distinctId)
        Use this method to opt-in an already opted-out user from tracking. People updates and track calls will be sent to Mixpanel after using this method. This method will internally track an opt-in event to your project.
        Parameters:
        distinctId - Optional string to use as the distinct ID for events. This will call identify(String). See also optInTracking(String), optInTracking(String, JSONObject) and optOutTracking().
      • optInTracking

        public void optInTracking​(java.lang.String distinctId,
                                  org.json.JSONObject properties)
        Use this method to opt-in an already opted-out user from tracking. People updates and track calls will be sent to Mixpanel after using this method. This method will internally track an opt-in event to your project.
        Parameters:
        distinctId - Optional string to use as the distinct ID for events. This will call identify(String).
        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().
      • isAppInForeground

        public boolean isAppInForeground()
        Based on the application's event lifecycle this method will determine whether the app is running in the foreground or not. If your build version is below 14 this method will always return false.
        Returns:
        True if the app is running in the foreground.
      • track

        protected void track​(java.lang.String eventName,
                             org.json.JSONObject properties,
                             boolean isAutomaticEvent)