com.google.android.apps.analytics.easytracking
Class EasyTracker

java.lang.Object
  extended by com.google.android.apps.analytics.easytracking.EasyTracker

public class EasyTracker
extends Object

EasyTracker is a class designed to easy the burden of adding tracking code to your application. Simply add this class to your application and extend TrackedActivity instead of Activity for each Activity in your application.

This class is designed to be used from a single Thread. Use on the Main UI Thread is acceptable.

Note that all of your Activities must extend TrackedActivity (or an equivalent Activity, like TrackedListActivity instead of ListActivity) for this Class to properly track application usage and time. If you have an Activity Class that doesn't extend one provided in this package, you can create one by copying TrackedActivity and having it extend the Activity you want. For example, if you have an Activity subclass called FragmentActivity, simply create a class called TrackedFragmentActivity and have your classes extend that instead of FragmentActivity. The code for TrackedFragmentActivity should be the same as the code in TrackedActivity.

You can turn on tracking by providing a String resource of the name ga_api_key with a value of your account 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).

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


Field Summary
static String LOG_TAG
           
 
Method Summary
 void addItem(com.google.android.apps.analytics.Item item)
          Adds an Item to the Transaction identified by Item.orderId.
 void addTransaction(com.google.android.apps.analytics.Transaction transaction)
          Adds a Transaction to be sent to Google Analytics.
 void clearTransactions()
          Clears all pending Transactions and Items from the internal queue.
 void dispatch()
          Dispatch up to 30 queued hits to the Google Analytics servers, but only if another dispatch is not in progress.
static EasyTracker getTracker()
           
 void setContext(Context ctx)
          Sets the context to use to the applicationContext of the Context ctx.
 void setReferrer(String referrer)
          Set the campaign referral to the values in the input.
 void startNewSession()
          Start a new session using the parameters stored in the EasyTracker Class.
 void stopSession()
          Stops the automatic dispatch from continuing to run.
 void trackActivityRetainNonConfigurationInstance()
          Track Activity restarts due to configuration changes (i.e.
 void trackActivityStart(Activity activity)
          Track the start of an Activity, but only if autoActivityTracking is true.
 void trackActivityStop(Activity activity)
          Track the end of an Activity and/or application.
 void trackEvent(String category, String action, String label, int value)
          Track an Event.
 void trackPageView(String name)
          Track a pageview, which is analogous to an Activity.
 void trackTransactions()
          Sends all the pending Transactions and Items to dispatch.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

LOG_TAG

public static final String LOG_TAG
See Also:
Constant Field Values
Method Detail

getTracker

public static EasyTracker getTracker()

setContext

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

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

trackActivityStart

public void trackActivityStart(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. Developers should not call this method directly. Extend TrackedActivity (or its other cousins) instead of Activity to use this method. Note that this method should be called from the Activity's onStart method.

Parameters:
activity - the Activity that is to be tracked

trackActivityRetainNonConfigurationInstance

public void trackActivityRetainNonConfigurationInstance()
Track Activity restarts due to configuration changes (i.e. orientation change). There is no need to start a new session in this case. Note that this method should be called from the Activity's onRetainNonConfigurationInstance callback.

Note that the GoogleAnalytics SDK supports Android versions back to 1.5. The onRetainNonConfigurationInstance method is deprecated in Android 3.0, but its replacement is only supported in Android 2.1 and beyond.


trackActivityStop

public void trackActivityStop(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 Activity's onStop callback.

Parameters:
activity - the Activity that is to be tracked

addItem

public void addItem(com.google.android.apps.analytics.Item item)
Adds an Item to the Transaction identified by Item.orderId. A new Transaction will be created if one doesn't already exist. This call will overwrite an existing Item object with the same orderId and itemSKU.

Parameters:
item - the Item to add

addTransaction

public void addTransaction(com.google.android.apps.analytics.Transaction transaction)
Adds a Transaction to be sent to Google Analytics. If a Transaction with the same orderId already exists, it will be replaced with this Transaction.

Parameters:
transaction - the Transaction to add

clearTransactions

public void clearTransactions()
Clears all pending Transactions and Items from the internal queue. This method will not affect Transactions and Items already sent with the trackTransactions() call.


dispatch

public void dispatch()
Dispatch up to 30 queued hits to the Google Analytics servers, but only if another dispatch is not in progress.


setReferrer

public void setReferrer(String referrer)
Set the campaign referral to the values in the input. If the input is not valid, the method fails. If the input is valid, the referrer information is changed and a new session is started. Each hit after a successful call to setReferrer will have the referrer information attached.

Parameters:
referrer - the campaign referral information to set

startNewSession

public void startNewSession()
Start a new session using the parameters stored in the EasyTracker Class. This method flags GoogleAnalyticsTracker to start a new session with the next hit it generates. As such, calling this multiple times in a row will have the same effect of calling it once.


stopSession

public void stopSession()
Stops the automatic dispatch from continuing to run.


trackEvent

public void trackEvent(String category,
                       String action,
                       String label,
                       int value)
Track an Event.

Parameters:
category - the category of the event
action - the action of the event
label - the label of the event, can be null
value - the value of the event

trackPageView

public void trackPageView(String name)
Track a pageview, which is analogous to an Activity. If null is passed in as input, no pageview will be tracked.

Parameters:
name - The name of the Activity or view to be tracked.

trackTransactions

public void trackTransactions()
Sends all the pending Transactions and Items to dispatch. Once this method is called, all the Transactions and Items added previously will not be cleared by a clearTransactions call and will eventually be dispatched to the GoogleAnalytics servers.