Easytracker is a library that makes analytics integration easier. It handles session management and can automatically track activity views.
It is pretty simple to integrate, just download the zip and add the EasyTracker.jar into your project. If you are using fragments (I will cover that later) or would like to have to source on hand, you can download the source distribution and include it as a project on the build path.
Account Setup
If you don’t already have an account for Google Analytics you should do that now, then setup a profile for your Android application. In the dropdown next to ‘Website’s URL:” you can select “Not a website”. You will need your tracking code, so note that down somewhere for later.
Configuration
Configuration is pretty simple, just need to add a few lines to your strings.xml file, and add permissions in ApplicationManifest.xml (if they already don’t exist). The following is where you set your tracking code:-
<string name="ga_api_key">UA-XXXXXXXX-X</string>
The next few are optional.
If you wish to check what is being logged you can enable debug. As results don’t appear for 24 to 48 hours, this can be handy for initial setup.
<bool name="ga_debug">true</bool>
If you want your activities to be tracked automatically, you can set the following:-
<bool name="ga_auto_activity_tracking">true</bool>
You can configure how frequently (in seconds) information is sent to google, with the following:-
<item format="integer" name="ga_dispatchPeriod" type="integer">120</item>
In ApplicationManifest.xml you should ensure you have the following permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Setting Up Tracked Activities
All activities you wish to track should extend TrackedActivity rather than Activity, TrackedListActivity rather than ListActivity.
You can track activities automatically by extending from these classes and setting the config option ‘ga_auto_activity_tracking’ to true. If you want to manually track, e.g. if you are using fragments, you should do this in, or after onStart(). Ensure you have invoked the super.onStart() else you will get NullPointerExceptions.
@Override
public void onStart(){
super.onStart();
...
EasyTracker.getTracker().trackPageView("/Settings");
...
}Fragment Support
I am using fragments in my app and currently you can only extend from TrackedActivity which extends Activity. You can get around this easily enough by making a copy the TrackedActivity class (I renamed it to TrackedFragmentActivity so it doesn’t clash) and changing its extends clause to the relevant FragmentActivity implementation (don’t forget to use the Support class if you are using the compatibility library). You will likely have to comment out an overridden method: onRetainNonConfigurationInstance() as it is marked as final in FragmentActivity and cannot be overridden.
Sending Events
You can also send events such as button clicks
EasyTracker.getTracker().trackEvent("Button Click", "Clicked Hello", "", 0);The parameters being category, action, label and value.
UPDATE: I noticed that things weren’t being logged to analytics, and the clue was the following:-
07-24 16:19:11.558: V/GoogleAnalyticsTracker(13431): dispatching hits in dry run mode
I needed to add another config option:-
<bool name="ga_dryRun">false</pre>
now I see the expected:
07-24 16:29:01.711: V/GoogleAnalyticsTracker(13697): HTTP Response Code: 200
Also, here is a good write-up on troubleshooting Google Analytics and EasyTracker
Follow Us on…