The Google Analytics SDK is an SDK that you can use with your existing Android applications. The Google Analytics SDK project is hosted at the Google Code site. It’s an active, ongoing project so check back for updates regularly; this tutorial was written using Version 1.2. The Google Analytics SDK for Android can be used to track and collect many different types of information about your users and applications, including:
- Track page views
- Track specific events
- Use custom variables to branch behavior (e.g. find out which work flow works better)
- Track ecommerce events
- Track Android Market referrals (Android 1.6+)
Keep in mind that you will be collecting what should be considered private data from individual users. Your application should make it clear that statistics collection is occurring within the application, either as part of your application’s terms of service, or by other obvious means.
Setting Up Google Analytics on Android
To enable Google Analytics within your application requires a number of steps. You will need to:
- Be developing an existing Android application. Identify the types of statistics you want to gather and determine the appropriate code points to drop the statistics gathering code into.
- Download the Google Analytics SDK for Android zip file and extract it.
- Create and configure a Google Analytics developer account to use for collecting statistics.
- Integrate the SDK into your application project in Eclipse.
- Start using the SDK, adding statistics-gathering hooks in the appropriate places.
- View and interpret the collected data on the online Google Analytics dashboard.
Now let’s look at these steps in more detail. We assume you have an existing application that you wish to integrate Google Analytics support into. However, we have also provided a very simple sample project to illustrate some of the types of statistics that can be easily generated by an Android application. The sample code that accompanies this tutorial is available online in open source form. The code listings you find here are part of this sample project, called ClickTracker.
Download the Google Analytics SDK and Create an Account
The first thing you need to do to get started is download the Google Analytics SDK for Android zip file. Note that by downloading and using the SDK, you are agreeing to additional terms of service. Extract the zip. Inside you will find a Readme, the SDK jar file, and a simple sample project that you can use if you need more help. Create a
/libs directory within your Android project and save the
libGoogleAnalytics.jar file within that directory.
Next, you need to create a developer account to send your application statistics to at google.com/analytics. The setup is primarily targeted at website statistics tracking, but mobile developers use the same workflow so don’t get confused and think you’re in the wrong place.
This account will be tied to an underlying Google Account (Gmail, etc.). It’s recommended that you create your Google Analytics account with a generic Google Account, as opposed to an individual’s account. The accounts are free to start and this saves your company the hassle of tracking down some errant individual who set up the account in their name and went off on vacation.
As part of the account creation, you’ll be asked to log in with your Google account. Then you’ll be prompted to enter some information. When you are prompted to enter a website for tracking purposes, choose a fake name (ideally including the name of your app and company domain, like clicktracker.mamlambo.com). You will also need to set the territory and time zone you want to normalize statistics to. Next, you’ll enter contact information for the account. Finally, you’ll need to agree to the terms of service.
Integrate the Google Analytics SDK into Your Android Project
Next, you need to integrate the SDK into your existing Android project. We’re going to assume you’re using the popular Eclipse development environment, although using other IDEs should involve very similar steps. To integrate the SDK into your project, you must:
- Add the
libGoogleAnalytics.jarto your project.
- Add two permissions to your Android manifest file.
Adding the JAR file to your project within Eclipse is easy. Simply:
- Click on the Project properties for your Android project.
- Under the Java Build Path settings, select the Libraries tab.
- Click the Add JARs… button and choose the jar within the
Since you are sending information over the network, you will need to add two permissions to your application, in order to access the network state and use the Internet. To do this:
- Click on the Permissions tab of the Android manifest file for your project.
- Add a new Uses Permission element for
- Add another Uses Permission element for
- Save your Android manifest file.
Start Using the Google Analytics SDK Features
You’re now ready to start using the SDK!
To start tracking within an Activity class, retrieve an instance of the GoogleAnalyticsTracker. You’ll typically want to start tracking in the
onCreate() method of your Activity, track various events throughout your Activity lifecycle, and stop tracking in your
To start tracking, supplying your unique UA account number, and an interval (seconds) at which to dispatch events to the server, like this:
GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
tracker.start("UA-1234567-89", 30, this); // Fake UA number here… supply your own!
Track page views by supplying the name of the page or screen:
Track events by specifying a category, action, label and value (all developer-defined fields):
tracker.trackEvent("Clicks", "Button", "High Road", 0);
When you’re done tracking, close the tracker:
Interpreting App Statistics Using the Google Analytics Dashboard
Once you’ve run your application and collected some statistics, you can head over to the Google Analytics dashboard and start interpreting your results. Unfortunately, the statistics are not shown in real time; you have to wait about 24 hours after sending your first event to see it on the site. So your first day’s dashboard will look something like this:
And then, about a day later, your dashboard should look more like this!
This concludes your brief introduction to Google Analytics for Android. The Google Analytics SDK for Android is an effective way to generate application statistics. It’s flexible, powerful, and easy to integrate into your applications. There are different types of statistics you can generate for your applications: page views and event tracking, ecommerce events, and other useful information. Remember that whenever you collect data from users, you should make sure they are aware of your activities, so that privacy concerns are addressed. This tutorial just begins to scratch the surface of what you can achieve by using the Google Analytics SDK for Android. Much of the “magic” involves dropping statistics-gathering hooks at clever places within your specific applications.