public class

GoogleAnalytics

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

Class Overview

This class is a singleton that provides methods for controlling global configuration and creating Tracker objects.

Applications can get an instance of this class by calling getInstance(Context). Applications can optionally provide a metadata reference to a global configuration XML resource file in the <application> element of their AndroidManifest.xml:

 <meta-data
     android:name="com.google.android.gms.analytics.globalConfigResource"
     android:resource="@xml/analytics_global_config" />
The configuration file should be stored in the applications res/values/xml directory and look like this:

 <?xml version="1.0" encoding="utf-8" ?>
 <resources>
     <string name="ga_appName">TestAppName</string>
     <string name="ga_appVersion">Version1.0</string>
     <string name="ga_logLevel">verbose</string>
     <integer name="ga_dispatchPeriod">1000</integer>
     <bool name="ga_dryRun">true</bool>
 </resources>
Following is a complete list of globally scoped configuration values that can be specified:
  • ga_appName(string) - name of application. Defaults to value found in the package.
  • ga_appVersion(string) - version of application. Defaults to value found in the package.
  • ga_dispatchPeriod(integer) - frequency of automatic dispatch (in seconds). Default is 1800.
  • ga_dryRun(bool) - if true, enable dry run mode. Default is false.
  • ga_logLevel(string) - One of "verbose", "info", "warning", "error".

Summary

Protected Constructors
GoogleAnalytics(Context context)
Public Methods
void dispatchLocalHits()
Dispatches queued hits (view, events, or transactions) to Google Analytics if a network connection is available, and the local dispatching service is in use.
void enableAutoActivityReports(Application application)
On devices running API level 14 (ICE_CREAM_SANDWICH) or above, applications can call this method in lieu of making explicit calls to reportActivityStart(Activity) and reportActivityStop(Activity).
boolean getAppOptOut()
Returns whether the state of the application-level opt is on.
static GoogleAnalytics getInstance(Context context)
Get the instance of the GoogleAnalytics class, creating it if necessary.
Logger getLogger()
Return the current Logger implementation in use.
boolean isDryRunEnabled()
Returns whether dry run mode is on.
Tracker newTracker(String trackingId)
Returns a Tracker instance with the given trackingId.
Tracker newTracker(int configResId)
Returns a Tracker instance preconfigured with the values specified in configResId.
void reportActivityStart(Activity activity)
Report the start of an Activity, so that it can be tracked by any Trackers that have enabled auto activity tracking (see enableAutoActivityTracking(boolean).) This will also start a new session if necessary.
void reportActivityStop(Activity activity)
Report the end of an Activity.
void setAppOptOut(boolean optOut)
Sets or resets the application-level opt out flag.
void setDryRun(boolean dryRun)
Toggles dry run mode.
void setLocalDispatchPeriod(int dispatchPeriodInSeconds)
Sets dispatch period for the local dispatcher.
void setLogger(Logger logger)
Provide a custom implementation of Logger to use.
[Expand]
Inherited Methods
From class java.lang.Object

Protected Constructors

protected GoogleAnalytics (Context context)

Public Methods

public void dispatchLocalHits ()

Dispatches queued hits (view, events, or transactions) to Google Analytics if a network connection is available, and the local dispatching service is in use. This method only works if local dispatching is in use. Local dispatching is only used in the absence of Google Play services on the device. In general, applications should not rely on the ability to dispatch hits manually.

public void enableAutoActivityReports (Application application)

On devices running API level 14 (ICE_CREAM_SANDWICH) or above, applications can call this method in lieu of making explicit calls to reportActivityStart(Activity) and reportActivityStop(Activity). This method is a noop if called on a device running API level less than 14.

Parameters
application The Application whose activities starts and stops should be automatically reported.

public boolean getAppOptOut ()

Returns whether the state of the application-level opt is on.

public static GoogleAnalytics getInstance (Context context)

Get the instance of the GoogleAnalytics class, creating it if necessary.

public Logger getLogger ()

Return the current Logger implementation in use. If no Logger has been set, a default Logger is provided that logs to android.util.Log with Logger.LogLevel set to WARNING.

public boolean isDryRunEnabled ()

Returns whether dry run mode is on.

public Tracker newTracker (String trackingId)

Returns a Tracker instance with the given trackingId. If the given trackingId is not null or empty, it will be set on the tracker and it is ready to send hits. Calling newTracker() multiple times with the same trackingId will create multiple Tracker objects with the same trackingId. If the trackingId is empty, you can still get a tracker, but you must set the tracking id before sending any hits. This is useful if you do not know the tracking id at the time of tracker creation, or if you want to use the same tracker instance to track multiple tracking ids. Using the same instance to track multiple tracking ids is not recommended since you need to be careful about not mixing the data you are sending to multiple profiles. It can be useful if you have a lot of tracking ids and you want to avoid object creation overhead involved in instantiating one tracker per tracking id.

Parameters
trackingId string of the form UA-xxxx-y

public Tracker newTracker (int configResId)

Returns a Tracker instance preconfigured with the values specified in configResId. Calling newTracker() multiple times with the same trackingId will create multiple Tracker objects with the same configuration. If the trackingId is empty, you can still get a tracker, but you must set the tracking id before sending any hits. This is useful if you do not know the tracking id at the time of tracker creation, or if you want to use the same tracker instance to track multiple tracking ids. Using the same instance to track multiple tracking ids is not recommended since you need to be careful about not mixing the data you are sending to multiple profiles. It can be useful if you have a lot of tracking ids and you want to avoid object creation overhead involved in instantiating one tracker per tracking id.

Parameters
configResId The resource id of your tracker configuration file. See Tracker for more information about what configuration elements can be included in that file.

public void reportActivityStart (Activity activity)

Report the start of an Activity, so that it can be tracked by any Trackers that have enabled auto activity tracking (see enableAutoActivityTracking(boolean).) This will also start a new session if necessary. This method should be called from the onStart() method in each Activity in your application that you'd like to track. If auto activity reports are enabled (see enableAutoActivityReports(Application)) on a device running API level 14 or above, this method will be a noop.

Parameters
activity the Activity that is to be tracked.

public void reportActivityStop (Activity activity)

Report the end of an Activity. Note that this method should be called from the onStop() method in each Activity in your application that you'd like to track. For proper operation, this method must be called in all Activities where reportActivityStart(Activity) is called. If auto activity reports are enabled (see enableAutoActivityReports(Application)) on a device running API level 14 or above, this method will be a noop.

Parameters
activity the Activity that is to be tracked.

public void setAppOptOut (boolean optOut)

Sets or resets the application-level opt out flag. If set, no hits will be sent to Google Analytics. The value of this flag will not persist across application starts. The correct value should thus be set in application initialization code.

Parameters
optOut true if application-level opt out should be enforced.

public void setDryRun (boolean dryRun)

Toggles dry run mode. In dry run mode, the normal code paths are executed locally, but hits are not sent to Google Analytics servers. This is useful for debugging calls to the Google Analytics SDK without polluting recorded data.

By default, this flag is disabled.

public void setLocalDispatchPeriod (int dispatchPeriodInSeconds)

Sets dispatch period for the local dispatcher. The dispatcher will check for hits to dispatch every dispatchPeriod seconds. If zero or a negative dispatch period is given, automatic dispatch will not be enabled, and the application will need to dispatch events manually using dispatchLocalHits(). This method only works if local dispatching is in use. Local dispatching is only used in the absence of Google Play services on the device. In general, applications should not rely on the ability to dispatch hits manually.

Parameters
dispatchPeriodInSeconds the new dispatch period

public void setLogger (Logger logger)

Provide a custom implementation of Logger to use. This custom Logger will replace a default Logger that logs to android.util.Log.

Parameters
logger The Logger implementation to use for logging.