com.google.analytics.tracking.android
Class GoogleTracker

java.lang.Object
  extended by com.google.analytics.tracking.android.GoogleTracker
All Implemented Interfaces:
Tracker

public class GoogleTracker
extends java.lang.Object
implements Tracker

Implementation of the Tracker interface. This class interacts with a GoogleAnalytics object for sending hits and tracking active Trackers.


Method Summary
 boolean anonymizeIpEnabled()
          Returns whether or not anonymize IP mode is on.
 void close()
          Closes the session.
 java.util.Map<java.lang.String,java.lang.String> constructEvent(java.lang.String category, java.lang.String action, java.lang.String label, java.lang.Long value)
          Creates model fields for an event.
 java.util.Map<java.lang.String,java.lang.String> constructException(java.lang.String exceptionDescription, boolean fatal)
          Creates model fields for an exception.
 java.util.Map<java.lang.String,java.lang.String> constructRawException(java.lang.String threadName, java.lang.Throwable exception, boolean fatal)
          Creates model fields for an exception.
 java.util.Map<java.lang.String,java.lang.String> constructSocial(java.lang.String network, java.lang.String action, java.lang.String target)
          Creates model fields for social.
 java.util.Map<java.lang.String,java.lang.String> constructTiming(java.lang.String category, long intervalInMilliseconds, java.lang.String name, java.lang.String label)
          Creates model fields for timing.
 java.util.Map<java.lang.String,java.lang.String> constructTransaction(Transaction trans)
          Creates model fields for a transaction.
 java.lang.String get(java.lang.String key)
          Gets the model value for the given key.
 java.lang.String getAppId()
           
 java.lang.String getAppInstallerId()
           
 ExceptionParser getExceptionParser()
           
 double getSampleRate()
          Returns the sample rate.
 java.lang.String getTrackingId()
           
 boolean getUseSecure()
          Returns the current setting for useSecure.
 void send(java.lang.String hitType, java.util.Map<java.lang.String,java.lang.String> params)
          Tracks the given hit.
 void set(java.lang.String key, java.lang.String value)
          Sets the model value for the given key.
 void setAnonymizeIp(boolean anonymizeIp)
          Turns on or off anonymize IP mode.
 void setAppId(java.lang.String appId)
          Set the appId.
 void setAppInstallerId(java.lang.String appInstallerId)
          Set the id of the app that installed this app.
 void setAppName(java.lang.String appName)
          An input of null or an empty String will be ignored.
 void setAppScreen(java.lang.String appScreen)
          Stores the place to be sent with subsequent track calls.
 void setAppVersion(java.lang.String appVersion)
          Null is allowed as is an empty String.
 void setCampaign(java.lang.String campaign)
          Set the campaign.
 void setCustomDimension(int slot, java.lang.String value)
          Sets the value for a custom dimension.
 void setCustomDimensionsAndMetrics(java.util.Map<java.lang.Integer,java.lang.String> dimensions, java.util.Map<java.lang.Integer,java.lang.Long> metrics)
          Sets the values for several custom dimensions and metrics at once.
 void setCustomMetric(int slot, java.lang.Long value)
          Sets the value for a custom metric.
 void setExceptionParser(ExceptionParser exceptionParser)
          Sets the ExceptionParser to be used by this Tracker.
 void setReferrer(java.lang.String referrer)
          Set the referrer.
 void setSampleRate(double sampleRate)
          Sets the sample rate.
 void setStartSession(boolean startSession)
          Specifies whether a new session should be started.
 void setUseSecure(boolean useSecure)
          Determines whether hits are sent securely.
 void trackEvent(java.lang.String category, java.lang.String action, java.lang.String label, java.lang.Long value)
          Tracks an event.
 void trackException(java.lang.String description, boolean fatal)
          Tracks that an exception occurred.
 void trackException(java.lang.String threadName, java.lang.Throwable exception, boolean fatal)
          Tracks that an exception occurred.
 void trackSocial(java.lang.String network, java.lang.String action, java.lang.String target)
          Tracks a social hit.
 void trackTiming(java.lang.String category, long intervalInMilliseconds, java.lang.String name, java.lang.String label)
          Tracks a user timing hit.
 void trackTransaction(Transaction transaction)
          Tracks an eCommerce transaction.
 void trackView()
          Tracks entering a view with the current app screen name.
 void trackView(java.lang.String appScreen)
          Tracks entering a view with a new app screen name.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

send

public void send(java.lang.String hitType,
                 java.util.Map<java.lang.String,java.lang.String> params)
Description copied from interface: Tracker
Tracks the given hit. Note that hitType must be a value found in the ModelFields in order for that value to appear in the hit.

Specified by:
send in interface Tracker
Parameters:
hitType - the type of the hit
params - map of hit data to values which are appended to the existing values which are already set (using Tracker.set(java.lang.String, java.lang.String))

setStartSession

public void setStartSession(boolean startSession)
Description copied from interface: Tracker
Specifies whether a new session should be started. This method does not send a hit, but will add a parameters to the next hit marking it as the beginning of a new session if the input parameter is true.

If you don't explicitly mark session starts by calling setStartSession with a value of true, sessions will be created automatically on the backend.

By default, trackers are created with startSession set to true.

Specified by:
setStartSession in interface Tracker
Parameters:
startSession - true if a new session should be started with the next hit

setAppScreen

public void setAppScreen(java.lang.String appScreen)
Description copied from interface: Tracker
Stores the place to be sent with subsequent track calls.

Specified by:
setAppScreen in interface Tracker
Parameters:
appScreen - the new app screen name (null or empty string clears any existing app screen name)

trackView

public void trackView()
Description copied from interface: Tracker
Tracks entering a view with the current app screen name.

Specified by:
trackView in interface Tracker

trackView

public void trackView(java.lang.String appScreen)
Description copied from interface: Tracker
Tracks entering a view with a new app screen name.

Specified by:
trackView in interface Tracker
Parameters:
appScreen - the new app screen name

trackEvent

public void trackEvent(java.lang.String category,
                       java.lang.String action,
                       java.lang.String label,
                       java.lang.Long value)
Description copied from interface: Tracker
Tracks an event. Events have a category, action, label and value. This method can be used to track events such as button presses or other user interactions with your application.

Specified by:
trackEvent in interface Tracker
Parameters:
category - the event category
action - the event action
label - the event label
value - the event value. Pass null if no value is to be set.

trackTransaction

public void trackTransaction(Transaction transaction)
Description copied from interface: Tracker
Tracks an eCommerce transaction.

Specified by:
trackTransaction in interface Tracker

trackException

public void trackException(java.lang.String description,
                           boolean fatal)
Description copied from interface: Tracker
Tracks that an exception occurred. Note that the description is optional. If you don't want to send a description simply set the input parameter to null.

Specified by:
trackException in interface Tracker
Parameters:
description - the type of exception
fatal - true if the exception is fatal

trackException

public void trackException(java.lang.String threadName,
                           java.lang.Throwable exception,
                           boolean fatal)
Description copied from interface: Tracker
Tracks that an exception occurred. The exception reported will be parsed using an ExceptionParser class set for this tracker. See Tracker.setExceptionParser(com.google.analytics.tracking.android.ExceptionParser) and StandardExceptionParser.

Specified by:
trackException in interface Tracker
Parameters:
threadName - the name of the @{link Thread} that got the exception, or null
exception - the Throwable to report
fatal - true if the exception is fatal

trackTiming

public void trackTiming(java.lang.String category,
                        long intervalInMilliseconds,
                        java.lang.String name,
                        java.lang.String label)
Description copied from interface: Tracker
Tracks a user timing hit. Category and label are optional.

Specified by:
trackTiming in interface Tracker
Parameters:
category - the category of the timing
intervalInMilliseconds - the timing interval, in milliseconds
name - the optional name of the timing
label - the optional label

trackSocial

public void trackSocial(java.lang.String network,
                        java.lang.String action,
                        java.lang.String target)
Description copied from interface: Tracker
Tracks a social hit. Target is optional.

Specified by:
trackSocial in interface Tracker
Parameters:
network - the social network
action - the social action
target - the social target

constructEvent

public java.util.Map<java.lang.String,java.lang.String> constructEvent(java.lang.String category,
                                                                       java.lang.String action,
                                                                       java.lang.String label,
                                                                       java.lang.Long value)
Description copied from interface: Tracker
Creates model fields for an event. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructEvent in interface Tracker

constructTransaction

public java.util.Map<java.lang.String,java.lang.String> constructTransaction(Transaction trans)
Description copied from interface: Tracker
Creates model fields for a transaction. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructTransaction in interface Tracker

constructException

public java.util.Map<java.lang.String,java.lang.String> constructException(java.lang.String exceptionDescription,
                                                                           boolean fatal)
Description copied from interface: Tracker
Creates model fields for an exception. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructException in interface Tracker

constructRawException

public java.util.Map<java.lang.String,java.lang.String> constructRawException(java.lang.String threadName,
                                                                              java.lang.Throwable exception,
                                                                              boolean fatal)
                                                                       throws java.io.IOException
Description copied from interface: Tracker
Creates model fields for an exception. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructRawException in interface Tracker
Throws:
java.io.IOException

constructTiming

public java.util.Map<java.lang.String,java.lang.String> constructTiming(java.lang.String category,
                                                                        long intervalInMilliseconds,
                                                                        java.lang.String name,
                                                                        java.lang.String label)
Description copied from interface: Tracker
Creates model fields for timing. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructTiming in interface Tracker

constructSocial

public java.util.Map<java.lang.String,java.lang.String> constructSocial(java.lang.String network,
                                                                        java.lang.String action,
                                                                        java.lang.String target)
Description copied from interface: Tracker
Creates model fields for social. The results of this method can be used to call Tracker.send(java.lang.String, java.util.Map).

Specified by:
constructSocial in interface Tracker

close

public void close()
Description copied from interface: Tracker
Closes the session. Call when you are done with the Tracker.

Specified by:
close in interface Tracker

getTrackingId

public java.lang.String getTrackingId()
Specified by:
getTrackingId in interface Tracker
Returns:
the trackingId used by this Tracker

setAnonymizeIp

public void setAnonymizeIp(boolean anonymizeIp)
Description copied from interface: Tracker
Turns on or off anonymize IP mode. In anonymize IP mode, Google Analytics will anonymize the IP address information collected by zeroing out some of the least significant bits of the address. By default, this flag is disabled.

In the case of IPv4 addresses, the last octet is set to zero. For IPv6 addresses, the last 10 octets are set to zero, although this is subject to change in the future.

Specified by:
setAnonymizeIp in interface Tracker
Parameters:
anonymizeIp - if true, turns on anonymize IP mode

setSampleRate

public void setSampleRate(double sampleRate)
Description copied from interface: Tracker
Sets the sample rate. The sampleRate parameter controls the probability that the visitor will be sampled. By default, sampleRate is 100, which signifies no sampling. sampleRate may be set to any double value between 0 and 100, inclusive. A value of 90 requests 90% of visitors to be sampled (10% of visitors to be sampled out).

When a visitor is not sampled, no data is submitted to Google Analytics about that visitor's activity. If your application is subject to heavy traffic spikes, you may wish to adjust the sample rate to ensure uninterrupted report tracking. Sampling in Google Analytics occurs consistently across unique visitors, ensuring integrity in trending and reporting even when sampling is enabled, because unique visitors remain included or excluded from the sample, as set from the initiation of sampling.

Note that sampleRate can be set down to hundredths of a percent.

Specified by:
setSampleRate in interface Tracker
Parameters:
sampleRate - a value between 0.0 and 100.0

anonymizeIpEnabled

public boolean anonymizeIpEnabled()
Description copied from interface: Tracker
Returns whether or not anonymize IP mode is on.

Specified by:
anonymizeIpEnabled in interface Tracker

getSampleRate

public double getSampleRate()
Description copied from interface: Tracker
Returns the sample rate.

Specified by:
getSampleRate in interface Tracker

setReferrer

public void setReferrer(java.lang.String referrer)
Description copied from interface: Tracker
Set the referrer. Note that referrer is similar to HTTP referrer. This field, if set, will be sent with the next hit for this tracker.

Specified by:
setReferrer in interface Tracker
Parameters:
referrer - the new referrer information

setCampaign

public void setCampaign(java.lang.String campaign)
Description copied from interface: Tracker
Set the campaign. The input string may be a set of URL parameters or a full URL. The URL parameters may or may not be encoded. Valid campaign parameters are:

Implementations should handle input of the form http://my.site.com/index.html?utm_campaign=wow&utm_source=source as well as input of the form utm_campaign=wow&utm_source=source.

For more information on auto-tagging, see http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55590

For more information on manual tagging, see http://support.google.com/googleanalytics/bin/answer.py?hl=en&answer=55518

Specified by:
setCampaign in interface Tracker
Parameters:
campaign - the campaign information, as a sequence of URL parameters separated by &

get

public java.lang.String get(java.lang.String key)
Description copied from interface: Tracker
Gets the model value for the given key. Returns null if no model value has been set.

Specified by:
get in interface Tracker

set

public void set(java.lang.String key,
                java.lang.String value)
Description copied from interface: Tracker
Sets the model value for the given key. All subsequent track or send calls will send this key-value pair along as well.

Specified by:
set in interface Tracker

setAppName

public void setAppName(java.lang.String appName)
An input of null or an empty String will be ignored.

Specified by:
setAppName in interface Tracker

setAppVersion

public void setAppVersion(java.lang.String appVersion)
Null is allowed as is an empty String.

Specified by:
setAppVersion in interface Tracker

setAppId

public void setAppId(java.lang.String appId)
Description copied from interface: Tracker
Set the appId. By default, the appId will be set to the packageName of the application. This works for Google Play, but other stores will have different appIds. This method allows changing the appId for other stores.

Specified by:
setAppId in interface Tracker
Parameters:
appId - the new appId or null

getAppId

public java.lang.String getAppId()
Specified by:
getAppId in interface Tracker

setAppInstallerId

public void setAppInstallerId(java.lang.String appInstallerId)
Description copied from interface: Tracker
Set the id of the app that installed this app. By default, the app installer id will be set based on the PackageManager#getInstallerPackageName method.

Specified by:
setAppInstallerId in interface Tracker
Parameters:
appInstallerId - the new appInstallerId or null

getAppInstallerId

public java.lang.String getAppInstallerId()
Specified by:
getAppInstallerId in interface Tracker

setExceptionParser

public void setExceptionParser(ExceptionParser exceptionParser)
Description copied from interface: Tracker
Sets the ExceptionParser to be used by this Tracker.

Specified by:
setExceptionParser in interface Tracker

getExceptionParser

public ExceptionParser getExceptionParser()
Specified by:
getExceptionParser in interface Tracker
Returns:
the ExceptionParser currently in use for this Tracker.

setUseSecure

public void setUseSecure(boolean useSecure)
Description copied from interface: Tracker
Determines whether hits are sent securely. If set, hits are sent using Https if possible. Otherwise, hits are sent using Http. Default is not secure.

Specified by:
setUseSecure in interface Tracker
Parameters:
useSecure - if true, use Https to send hits, otherwise use Http

getUseSecure

public boolean getUseSecure()
Description copied from interface: Tracker
Returns the current setting for useSecure.

Specified by:
getUseSecure in interface Tracker

setCustomDimension

public void setCustomDimension(int slot,
                               java.lang.String value)
Description copied from interface: Tracker
Sets the value for a custom dimension. Note that all custom dimensions are sent with the next hit and then cleared.

Specified by:
setCustomDimension in interface Tracker
Parameters:
slot - the dimension slot
value - the dimension value, or null to clear a previously set value

setCustomMetric

public void setCustomMetric(int slot,
                            java.lang.Long value)
Description copied from interface: Tracker
Sets the value for a custom metric. Note that all custom metrics are sent with the next hit then cleared.

Specified by:
setCustomMetric in interface Tracker
Parameters:
slot - the metric slot
value - the metric value, or null to clear a previously set value

setCustomDimensionsAndMetrics

public void setCustomDimensionsAndMetrics(java.util.Map<java.lang.Integer,java.lang.String> dimensions,
                                          java.util.Map<java.lang.Integer,java.lang.Long> metrics)
Description copied from interface: Tracker
Sets the values for several custom dimensions and metrics at once. Note that the custom dimension slot namespace is separate from the custom metric slot namespace.

Specified by:
setCustomDimensionsAndMetrics in interface Tracker
Parameters:
dimensions - a Map of the custom dimension slots to custom dimension values
metrics - a Map of the custom metric slots to custom metric values