com.google.analytics.tracking.android
Class EasyTracker

java.lang.Object
  extended by com.google.analytics.tracking.android.EasyTracker

public class EasyTracker
extends java.lang.Object

EasyTracker is a class designed to easy the burden of adding tracking code to your application. Ensure that all of your Activities call EasyTracker on your activity's onStart and onStart:

 @Override
 public void onStart() {
   super.onStart();
   EasyTracker.getInstance().activityStart(this);
 }

 @Override
 public void onStop() {
   super.onStop();
   EasyTracker.getInstance().activityStop(this);
 }
 

Additionally, if you want to make EasyTracker calls in other classes or methods, you should ensure to call setContext(Context) in each Activity's onCreate method before making any such calls. You can make this call in your Application's onCreate method instead.

For convenience, TrackedActivity, TrackedExpandableListActivity, TrackedListActivity, TrackedPreferenceActivity, and TrackedTabActivity are provided whose only purpose is to override these two methods and call EasyTracker.

Next, create an .xml file (analytics.xml, perhaps) and set various resources within that file. You can turn on tracking by providing a String resource of the name ga_trackingId with a value of your tracking id (form UA-12345-6). You can provide various parameters as String, Bool or Integer resources (such as sampleRate) as well. Just use the proper type for the parameter (String for String, Bool for boolean and Integer for int).

Information about the application, by default, are retrieved programatically. The Application name will be retrieved from the app_name string in your Android resources. The application version will be retrieved from the versionName in your manifest. To override either of these, provide ga_appName or ga_appVersion String resources.

If you want to track your Activities as well as the application, you can add the Bool resource ga_autoActivityTracking and give it a value of true.

Here is a complete list of parameters that can be passed to EasyTracker:

Note you can use the tag <resources xmlns:tools="http://schemas.android.com/tools" tools:ignore="TypographyDashes"> in your Analytics.xml file to suppress the warning about using dashes. Do not encode dashes in the ga_trackingId string. Doing so will prevent you from seeing any data in your reports.


Method Summary
 void activityStart(Activity activity)
          Track the start of an Activity, but only if autoActivityTracking is true.
 void activityStop(Activity activity)
          Track the end of an Activity and/or application.
 void dispatch()
          Dispatches pending hits.
static EasyTracker getInstance()
          Returns a singleton instance of EasyTracker.
static Tracker getTracker()
          Get the Tracker used by EasyTracker.
 void setContext(Context ctx)
          Sets the context to use to the applicationContext of the input Context.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static EasyTracker getInstance()
Returns a singleton instance of EasyTracker.


getTracker

public static Tracker getTracker()
Get the Tracker used by EasyTracker. Note that EasyTracker must have been initialized by calling either setContext(Context) or activityStart(Activity) before calling this method. Otherwise an IllegalStateException will be thrown.

Returns:
the Tracker used by EasyTracker

setContext

public void setContext(Context ctx)
Sets the context to use to the applicationContext of the input Context. If the input is not null, this method will then go on to initialize EasyTracker with parameters from the resource files. If ga_trackingId is specified, this method will enable Google Analytics tracking. If not, it will leave tracking disabled.

Parameters:
ctx - the Context to use to fetch the applicationContext

activityStart

public void activityStart(Activity activity)
Track the start of an Activity, but only if autoActivityTracking is true. This method will start a new session if necessary, and will send an empty event to Google Analytics if autoActivityTracking is false to ensure proper application-level tracking. This method should be called from the onStart method in each Activity in your application. You may extend TrackedActivity (or its other cousins) instead of Activity to use this method.

Parameters:
activity - the Activity that is to be tracked

activityStop

public void activityStop(Activity activity)
Track the end of an Activity and/or application. This is done by sending an empty event to Google Analytics. Note that this method should be called from the onStop method of each Activity in your application.

Parameters:
activity - the Activity that is to be tracked

dispatch

public void dispatch()
Dispatches pending hits. See ServiceManager.dispatch() for details.