Easytracker – Google Analytics integration for Android
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

Very helpful, thanks. It would be better if you explained how to use EasyTracker in your code. Like EasyTracker.getInstance().activityStart(this);
Hi Igor, sorry about the late response!
Everything here is the code that is needed (you can see the code snippets right?)
Extending your activity from TrackedActivity does all the initialisation. The only thing you need to do is send events (and pageviews if you aren’t doing this automatically)
Do I need to do anything in the Manifest file for this to work? I mean, Google’s tutorial about their analytics talks about registering a receiver.
You need the correct permissions – they are ones I have always used so I completely forgot to include them (updated).
The broadcastReceiver references in their doco refer to setup of the base analytics package.
Looks like it is used for referral tracking, but it doesn’t appear in any of the EasyTracker documentation I can find. That being said, that documentation is a little ambiguous at times!
Lol….Why all tuto forgot to say the basic?.
A good and exactly code example was:1º EasyTracker.getTracker().startNewSession();2º EasyTracker.getTracker().trackEvent(“App”, “Init”, “Aplication Start”, 0);
Where exactly do you put startNewSession()? I am using a FragmentActivity…
When you talk about the following settings, where exactly to do you enter them?
true
60
false
Same as the ga_api_key, in strings.xml. You can probably extract them out into their own config file if you wished to as well.
And where exactly do you refer to those settings? Do you have an example? Sorry if this is simplistic…
You don’t.
Once you have entered them in strings.xml that is it. Easytracker will look for string declarations with those key names and will take it from there.
Also, you said:
“ If you are tracking your activities manually…” What do you mean by that? ‘Manually’ as opposed to what?
Easytracker can track visits to each activity automatically. This isn’t great for everyone, especially when using fragments.
Manual tracking means logging visits explicitly in the code.
Should I do EasyTracker.getTracker().startNewSession(); in each of my activites that I want to track? Or is that one call global in your main activity?
No, EasyTracker should handle session management. I am not calling startNewSession() anywhere in my code at this point. If someone can show me a reason to call it outside of forcing a new session, please let me know!
if all you want to do is track each page view on regular activities just
leave ga_auto_activity_tracking as true and it should take care of it
without any more work
When you want to trigger a page visit just use the EasyTracker.getTracker().trackPageView(“/PageName”); call to log the page view. If you are switching fragments, you would call it when you switch. If it is a regular page. you would do it after super.onStart(); as I detailed in the post.
Thanks for your help. Another question: when you talk about tracking events, do the parameters – label & value – for trackEvent() have to be unique for every call? Or can they be identical across the whole application?
You probably don’t want them to be the same across the app, as you want to be able to track what each event does. That way, you will want to differentiate what each button posts. The methods signature is:-
void trackEvent(String category, String action, String label, int value)
In my case I use the same category of “Button Click” for all buttons, and I am currently leaving the label as null, while action lists what the button is actually doing.
You could post the label ID for ‘label’ and that is probably what I will end up doing as some buttons are listed in two places. In my app, the same button exists in the ‘first run’ activity as well as in the ‘settings’ activity.
I haven’t had a chance to actually go through all the data and work out how I want it all to be logged, but I suspect it is going to come down to personal tastes.
Hi, you mentioned extending from TrackedActivity for use of Fragments. Should I register TrackedActivity in the Manifest as well?
Thank you,
Igor
Definitely not! TrackedActivity is to be treated as an Abstract class and it is intended to be extended.
so if you want an activity to be tracked by EasyTracker, extend it rather than Activity:
public class MyActivity extends TrackedActivity {
….
}
You should only be adding Concrete class instances to the ApplicationManifest, so in this case, you would add MyActivity.
Yeah, ok… that was far from clear on my part. Updated.
Gracias. I only asked because Android lint gave me this warning.
Yep, I have that lint warning as well.
I also neglected to mention I renamed the copy to TrackedFragmentActivity so it doesn’t get confused with the original class.
Just want to say thanks for the patience in answering all the questions below. As a noobie dev they helped me a lot too!
No worries, glad my ramblings are useful!
Hello,
I think the following line in your article “true” should be “true”.
Greets,
Raffi
Ok, the Disqus plugin smashed my comment. What I would like to say is, that the debug line should end on “bool” and not closing with “pre”-tag.
Cheers @e02892eff1253a127dc8e0f28e1d630f:disqus , sorry missed your comment! Updated.