Monday, April 19, 2010

Google Analytics for Android

Did you know Google Analytics supports mobile?

Didn't think so, most developers aren't aware this powerful tool even exists for mobile. Here are the steps to implement the most basic feature of Google Analytics for Mobile.

1. Download the Google Analytics library for Android. Direct link to current version 0.7.

2. Sign up for Google Analytics if you don't already have an account.

3. Create a new UA, also known as a Web property ID. Once you are signed into Google Analytics you will see a drop down list in the top right corner. From the list, select "Create new account" and follow the steps provided. Google documentation suggests "using a fake but descriptive website URL (e.g. http://mymobileapp.mywebsite.com)". Whatever you choose is entirely up to you.



4. Write down your UA. This will be generated after you complete step 3. If you forget to write it down you can view the UA by selecting the product from your analytics home screen.

5. Add the Google Analytics library to your Android project. In Eclipse right click on your project (control + click for OSX) and select Properties. Choose Java Build Path and then select the Libraries tab. Then you will need to select the Add External JARs button and navigate to where you saved the library on your computer.
Note: The exact steps may vary depending on OS and what version of Eclipse you are running.



6. Add the following permissions to your AndroidManifest.xml file.
android.permission.INTERNET & android.permission.ACCESS_NETWORK_STATE

7. Here is the very basic logic of implementing analytics.

Declare our Analytics object
public GoogleAnalyticsTracker mAnalyticsTracker;

Obtain tracker instance and start in manual mode, usually done in onCreate(Bundle savedInstanceState)
mAnalyticsTracker = GoogleAnalyticsTracker.getInstance();
mAnalyticsTracker.start("insert_UA", this);

You can also start the tracker with a dispatch interval by specifying a seconds parameter. The following code will dispatch request every 60s.
mAnalyticsTracker.start("insert_UA", 60, this);
Note: don't forget to substitute insert_UA with your actual UA account.

The Google documentation suggests using trackPageView as the best way to track which areas of your application are being viewed by users.

Say we want to track when the users view the help screen, we simply pass HelpScreen.
mAnalyticsTracker.trackPageView("/HelpScreen");

If we want to track when users viewed the highscore screen we would call
mAnalyticsTracker.trackPageView("/HighscoreScreen");

The string value passed to trackPageView is entirely up to you. It makes sense to give them obvious names because when you view the Analytics dashboard you want to make it simple to understand.

If you start the tracker in manual mode you will need to call dispatch() to fire off the tracking events. This call is not required if you passed a time interval to the start call.
mAnalyticsTracker.dispatch();

Finally, remember to stop the tracker when you no longer need it's services.
mAnalyticsTracker.stop();


Once you have gone through your application and implemented tracking on the desired screens, kick back and relax while analytics does it magic. From my experience the analytics dashboard is updated daily, if anyone has a definitive answer please let me know. After a couple of days you will have clear indication of what areas of your application are most frequented visited.

You may be in for a surprise as to what your users are actually doing.


The analytics download includes a sample Android project as well for you to check out

4 comments:

chris_fagan said...

Great post Jason!

Barmalei4ik said...

Thanks for your post.
I have followed your steps but my tracking status is still "Tracking Not Installed". Does it require some time to update status?

Jason Tomlinson said...

Are you using the correct UA?

Post a code snippet so we can look at what you are doing.

Anonymous said...

you forgot to mention that a disclaimer is required about the usage of analytics.