Using Google Analytics in Your Mobile App

Everyone knows about Google Analytics for tracking use of web pages. It is kind of creepy how much data you can see about a person using your website. Everything from the pages views, to the path and order of the pages (called flow in GA), the country the user is located in, the ip addresses, and more. You can do very similar things in a native mobile app too. You can read about this here, but when it comes to simple instructions for how to get this stuff working or code samples, you’ll find not a whole heck of a lot.

Getting GA-Mobile Working

Head over to the main Google page and download the libraries as described. I grabbed version 1.5.1 of the SDK. There’s not much in the SDK. There’s a single .jar file and one sample. The sample is pretty lame. It basically just shows how to instantiate the tracker and count an application start. Not too useful. How do I track events, time spent on an activity, etc. That’s what I want to do. The next thing is to check out the dev guide. Pretty straight forward to get the library installed and working. For my testing I’m just going to use one of the sample apps as a base for testing – the newsreader app.

The next thing you need to do is get an account and property setup for data to be sent to. I’m not going to go thru all that so f you don’t know how to do that you should go over to the Google Analytics site and read up.

Reading through the sample code on the GA page, there is a mention of “EasyTracker” and using that instead of the regular SDK if your app has more than one activity. My app does have more than one activity so I grabbed that. Um…there’s no samples, there’s no docs, sheesh. I’ll try the basic one first and see what happens. The newsreader app’s main activity in so I’m going to get an instance of the tracker class there and added tracking events to the code exdecuted when a user clicks on a headline or a news category. We’ll start there and see what happens. Here’s the code I added to onCreate:

public void onCreate(Bundle savedInstanceState) {

tracker = GoogleAnalyticsTracker.getInstance();
tracker.startNewSession(“UA-33394749-2”, 10, this);

to the news headline click:

public void onHeadlineSelected(int index) {
mArtIndex = index;
tracker.trackEvent(“Clicks”, “Headline”, “clicked”, index);

and to the category click:

public void onCategorySelected(int catIndex) {
tracker.trackEvent(“Clicks”, “Category”, “clicked”, catIndex);

I also added an onDestroy() method to free up the tracker instance:

protected void onDestroy() {
// Stop the tracker when it is no longer needed.

You notice in my call to get an instance I added the dispatch interval so that I didn’t have to add that in the code.

That should do the trick I sprinkled a few page views in there too using this command:


Just to get some more data entries, but now it is a waiting game since GA only refreshes once a day. So with that done I will publish away and update tomorrow. Hopefully I have some entries.

UPDATE 10/7/2012: It Worked!

Hey cool – it worked! I can see both my page views and my events tracked in the GA website. Here’s a screen shot of one of the pages. Its pretty cool that it can identify the exact model of the device I was using. I found a few other interesting things:

  1. It said I was in New York when I am in PA. My take is that the phone was on my WiFi address and somehow Verizon IP addresses around here are all associated with NY.
  2. The device model can be identified – there are even pics of the device – or at least of the one I used.
  3. It captures screen resolutions
  4. The events are captured and charted out – this is the best feature.

Pretty cool I think and relatively simple to implement.

If you want the sample code here’s a zip with the code for just using the standard tracking library.

What is Easy Tracker?

On the SDK page it briefly mentions using easy tracker if your application has more than one activity. There’s a download, but as with the standard tracker there is very little mention of what to do with this library. Since the news reader has more than one activity let’s see what Easy Tracker can do. As above the first thing to do is put the jar file in your libs directory.

There’s not a whole lot of documentation on this – just a single readme file. This file says that all you need to do is extend EasyTrackerActivity instead of the normal activity for this to work. It makes no mention of how to set the property ID. It also does not mention what to do if you are using fragments – which of course are being used in this sample. While searching the internet for tips, I stumbled across beta pages for V2 of the mobile GA SDK – hmmm…ok great. Looks like in SDK 2 EasyTracker is changed up quite a bit I’ll have to read up on this and post back later.


SDK 1.5 is pretty easy to implement  – just takes a little patience to sprinkle it around in your code. IMHO you should definitely use this in all your apps so that you can understand use patterns and learn/improve.

2 Responses

  1. huan says:

    Hey Beren – how about some iOS love over here? There’s at least a decent sample in the iOS SDK. Pretty wierd that there isn’t for their own platform.

  2. Beren says:

    Yeah – I’m working on it. I’m building an app and having trouble tracking down a problem. I did figure out that the NY IP address is from ATT cell phone, not verizon, but it is interesting and calls into question what you can do with an IP address vs. true geolocation.

Leave a Reply