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.