public class

Tracker

extends Object
java.lang.Object
   ↳ com.google.android.gms.analytics.Tracker

Class Overview

Class to send tracking hits to Google Analytics. You can get an instance of this class by calling newTracker(int). A Tracker must be initialized with a tracking id of an app profile before you can send any hits. You can change the id to send hits to a different profile. Use this class to set values for measurement protocol parameters using the setXYZ() methods. A param value set using set(String, String) method is sent with all subsequent hits until you overwrite it with another value or clear it by setting it to null. You can also override it by specifying a different value in a send(Map) call. The values passed in the send(Map) are sent only with that hit. The following example uses the HitBuilders.EventBuilder helper class to build a param map to pass to the send(Map) method.

 Tracker t = GoogleAnalytics.getInstance(context).newTracker(trackingId);
 t.setScreenName(screenName);
 t.send(new HitBuilders.EventBuilder()
         .setCategory("UX")
         .setAction("click")
         .setLabel("someButtonName")
         .build());
 t.send(new HitBuilders.EventBuilder()
         .setCategory("UX")
         .setAction("click")
         .setLabel("somePopupAction")
         .setScreenName("popupDialogName")
         .build());
 
A Tracker can also be initialized with configuration values from an XML resource file like this:

 Tracker t = GoogleAnalytics.getInstance(ctx).newTracker(R.xml.tracker_config);
Where R.xml.tracker_config is the resource id for an XML configuration file. The file should be stored in the app's res/values/xml/ directory and look like this:

 <?xml version="1.0" encoding="utf-8" ?>
 <resources>
     <screenName name="com.google.foo.FirstActivity">
         First Activity
     </screenName>
     <screenName name="com.google.foo.AnotherActivity">
         Another Activity
     </screenName>
     <string name="ga_trackingId">UA-XXXX-Y</string>
     <string name="ga_sampleFrequency">99.8</string>
     <integer name="ga_sessionTimeout">2000</integer>
     <bool name="ga_autoActivityTracking">true</bool>
     <bool name="ga_anonymizeIp">true</bool>
     <bool name="ga_reportUncaughtExceptions">true</bool>
 </resources>
Here is a complete list of tracker scoped configuration values that can be specified:
  • ga_trackingId(string) - tracking Id to report against. Required
  • ga_sampleFrequency(string) - sample rate to use. Default is 100.0. It can be any value between 0.0 and 100.0
  • ga_autoActivityTracking(bool) - if true, views (Activities) will be tracked. Default is false.
  • ga_anonymizeIp(bool) - if true, anonymizeIp will be set for each hit. Default is false.
  • ga_reportUncaughtExceptions(bool) - if true, uncaught exceptions will be tracked. Default is false. NOTE: This value can only be set to true for a single Tracker. If specified for multiple Trackers, then the last one to be initialized will be used.
  • ga_sessionTimeout(int) - time (in seconds) an app can stay in the background before a new session is started. Setting this to a negative number will result in a new session never being started. Default is 30 seconds.

If ga_autoActivityTracking is enabled, an alternate screen name can be specified to substitute for the full length canonical Activity name in screen view hit. In order to specify an alternate screen name use an <screenName> element, with the name attribute specifying the canonical name, and the value the alias to use instead.

 <screenName name="com.google.foo.SampleActivity">Sample Activity</screenName>

Summary

Public Methods
void enableAdvertisingIdCollection(boolean enabled)
Sets whether the advertising id and ad targeting preference should be collected while sending hits to GA servers.
void enableAutoActivityTracking(boolean enabled)
Specify whether Activity starts should automatically generate screen views from this Tracker.
void enableExceptionReporting(boolean enabled)
Enables or disables uncaught exception reporting for a given tracker.
String get(String key)
Gets the model value for the given key.
void send(Map<String, String> params)
Merges the model values set on this Tracker via send(Map) with params and generates a hit to be sent.
void set(String key, String value)
Sets the model value for the given key.
void setAnonymizeIp(boolean anonymize)
void setAppId(String appId)
Sets the id of the App for analytics reports.
void setAppInstallerId(String appInstallerId)
Sets the Installer Id of the App for analytics reports.
void setAppName(String appName)
Sets the name of the App for analytics reports.
void setAppVersion(String appVersion)
Sets the version identifier of the App for analytics reports.
void setCampaignParamsOnNextHit(Uri uri)
Includes the campaign parameters contained in the URI referrer in the next hit.
void setClientId(String clientId)
Sets a particular client Id for the device.
void setEncoding(String encoding)
void setHostname(String hostname)
void setLanguage(String language)
Sets the language based on user's preferred locale.
void setLocation(String location)
void setPage(String page)
void setReferrer(String referrer)
void setSampleRate(double sampleRate)
Set the sample rate for all hits generated by the app.
void setScreenColors(String screenColors)
void setScreenName(String screenName)
Set the screen name to be associated with all subsequent hits.
void setScreenResolution(int width, int height)
Sets the screen resolution of the device.
void setSessionTimeout(long sessionTimeout)
Specify the time (in seconds) an app can stay in the background before a new session is started.
void setTitle(String title)
void setUseSecure(boolean useSecure)
Sets whether hits should be sent securely over https.
void setViewportSize(String viewportSize)
[Expand]
Inherited Methods
From class java.lang.Object

Public Methods

public void enableAdvertisingIdCollection (boolean enabled)

Sets whether the advertising id and ad targeting preference should be collected while sending hits to GA servers. The collection of advertising id and the ad targeting preference is disabled by default and needs to be turned on for mobile audience features and any other features that require advertising id.

Parameters
enabled true if the advertising information should be collected. False otherwise.

public void enableAutoActivityTracking (boolean enabled)

Specify whether Activity starts should automatically generate screen views from this Tracker.

Parameters
enabled True if screen views should be automatically generated.

public void enableExceptionReporting (boolean enabled)

Enables or disables uncaught exception reporting for a given tracker. This method is equivalent to using 'ga_reportUncaughtExceptions' in the tracker configuration file. Note that as with the configuration setting, only the uncaught exceptions are reported using this method.

public String get (String key)

Gets the model value for the given key. Returns null if no model value has been set.

public void send (Map<String, String> params)

Merges the model values set on this Tracker via send(Map) with params and generates a hit to be sent. The hit may not be dispatched immediately. Note that the hit type must set for the hit to be considered valid. More information on this can be found at http://goo.gl/HVIXHR.

Parameters
params map of hit data to values which are merged with the existing values which are already set (using set(String, String)). Values in this map will override the values set earlier. The values in this map will not be reused for the subsequent hits. If you need to send a value in multiple hits, you can use the set(String, String) method.

public void set (String key, String value)

Sets the model value for the given key. All subsequent track or send calls will send this key-value pair along as well. To remove a particular field, simply set the value to null.

Parameters
key The key of the field that needs to be set. It starts with "&" followed by the parameter name. The complete list of fields can be found at http://goo.gl/M6dK2U.
value A string value to be sent to Google servers. A null value denotes that the value should not be sent over wire.

public void setAnonymizeIp (boolean anonymize)

public void setAppId (String appId)

Sets the id of the App for analytics reports. This value is populated by default using the information provided by the android package manager. This value is usually the package name used by the app.

Parameters
appId String representing the name of the app.

public void setAppInstallerId (String appInstallerId)

Sets the Installer Id of the App for analytics reports. This value is populated by default using the information provided by the android package manager and the play store.

Parameters
appInstallerId String representing the name of the app.

public void setAppName (String appName)

Sets the name of the App for analytics reports. This value is populated by default using the information provided by the android package manager.

Parameters
appName String representing the name of the app.

public void setAppVersion (String appVersion)

Sets the version identifier of the App for analytics reports. This value is populated by default using the information provided by the android package manager.

Parameters
appVersion String representing the version of the app.

public void setCampaignParamsOnNextHit (Uri uri)

Includes the campaign parameters contained in the URI referrer in the next hit. If there is no referrer, or the referrer does not contain campaign parameters, this method does not add anything to the next hit.

Use this method to track in-app events driven by advertising with deep linking campaigns.

Valid campaign parameters are:

  • utm_id
  • utm_campaign
  • utm_content
  • utm_medium
  • utm_source
  • utm_term
  • dclid
  • gclid

Example: http://my.site.com/index.html?referrer=utm_source%3Dsource%26utm_campaign%3Dwow

Parameters
uri the uri containing the referrer

public void setClientId (String clientId)

Sets a particular client Id for the device. This Id should be a valid UUID (version 4) string as described in http://goo.gl/0dlrGx. If not specified, the SDK automatically generates one for you and sets it.

Parameters
clientId A valid version 4 UUID string.

public void setEncoding (String encoding)

public void setHostname (String hostname)

public void setLanguage (String language)

Sets the language based on user's preferred locale. The string should be of the format ll-cc where ll is the language and cc is the country. If no value is provided, the default value from the android SDK is used.

Parameters
language string that denotes a particular language/locale.

public void setLocation (String location)

public void setPage (String page)

public void setReferrer (String referrer)

public void setSampleRate (double sampleRate)

Set the sample rate for all hits generated by the app. The sampling is done at app level. The default value is 100. To enable sampling, the minimum rate required is 0.01%.

Parameters
sampleRate A value between 0 and 100, specifying the percentage of devices that should send hits.

public void setScreenColors (String screenColors)

public void setScreenName (String screenName)

Set the screen name to be associated with all subsequent hits.

public void setScreenResolution (int width, int height)

Sets the screen resolution of the device. If no resolution is specified, the default resolution from the android SDK is used.

Parameters
width integer representing the width in pixels.
height integer representing the height in pixels.

public void setSessionTimeout (long sessionTimeout)

Specify the time (in seconds) an app can stay in the background before a new session is started. Setting this to a negative number will result in a new session never being started. Default is 30 seconds.

Parameters
sessionTimeout Session timeout time in seconds.

public void setTitle (String title)

public void setUseSecure (boolean useSecure)

Sets whether hits should be sent securely over https. The default value is true.

public void setViewportSize (String viewportSize)