[This post is by Alexander Lucas, an Android Developer Advocate bent on saving the world 5 minutes. —Tim Bray]
With the addition of custom variables to the , it strikes me as a good time to cover something many of you might not have known was possible — using Google Analytics to easily track app usage. Using the mobile SDK is a handy way to get real data on how users interact with your Android apps. So today I'm going to explain how to track usage of your application with Google Analytics.
Prereqs Ahoy!
Before you take off running with this shiny new toy, there’s a few things you’ll need to set up first:
Download the mobile SDK. Download and installation instructions are available in the of the Mobile SDK docs, but the summarized version is:
Download the zip file from the download page
Put the libGoogleAnalytics.jar file in your project’s /libs directory
Be sure the following lines are in your AndroidManifest.XML file:
You’re going to need a Google Analytics account. Go to and set up an account if you don’t already have one. Then set up a profile for your Android application. When you’re done you’ll see a javascript snippet to insert into your “site”. Copy the part that looks like UA-XXXXXXX-X. You’ll use this in the Android application to tell Analytics which profile the data is being sent for.
Get Tracking
Previous Google Analytics users are going to find a lot of this familiar. In fact, we’ve made a point of keeping the interface as familiar as possible.
First, get your tracker object, and initialize it using the UA code for the Analytics profile you want to track. It makes the most sense to do this in theonCreate()
method for your activity main, so it only fires when your application starts up.