Google Analytics is a service provided by Google that makes it easy to track what users do. The Google Analytics team released an Analytics SDKs for mobile platforms including Android, iOS (Apple), and mobile websites. In this tutorial, learn how to include and enable this technology within your Android projects to gather important information about how users are using your applications.
Note: This tutorial has been updated to work with the latest version of the Google Analytics for Android SDK Release 2 (in the Android SDK Manager), with Version 1.4.2 listed in the ReadMe.txt file and on the website download link. This tutorial was originally posted July 5, 2011.
The Google Analytics SDK for Android provides helpful classes and methods to track user activity and generate useful statistics about your Android app activities. The underlying service was originally designed for website tracking, but it’s been adapted for use with mobile apps. Recently the team launched a beta program for “live” statistics, so there is no longer a 24 hour delay from when the statistics are logged to the service, and when they appear in the online reports.
Google Analytics is a client-server solution. Android developers add hooks within their application code to log different types of user activities at key points in the app lifecycle and upload this data to the Google Analytics server. Then later, when the application is deployed and used, those statistics are collected from various users. The developer logs into the Google Analytics website and views the aggregate statistics by generating different types of reports and viewing graphs and charts.
Step 1: Getting Started
There’s no point in hooking up Google Analytics unless you’ve already designed an Android application. Therefore, you’ll want to start with an application that is already pretty complete, in order to add statistics gathering in the appropriate code locations. This tutorial assumes you have an existing Android project in Eclipse to work from.
Step 2: Registering for a Google Analytics Account
For the Google Analytics service to work, you must first create a developer account to send statistics to. Start by signing up for an account at http://www.google.com/analytics. This account must be linked to an underlying Google Account, so you may also want to create a Google account specifically for your application or company, so that it’s not tied to an individual. The accounts are free.
Account creation is easy. Login with your Google account, and enter some basic information. When prompted to enter a website for tracking purposes, choose a fake name (ideally including the name of your app and company domain, like http://myapp.mycompany.com). You will also need to set the location and time zone to normalize app statistics to. Finally, you’ll need to enter contact information for the account.
Step 3: Download the SDK
There are two ways to get your hands on the Google Analytics SDK for Android. You can download the SDK through the Android SDK Manager or you can download it directly from the Google Analytics website. Extract the zip and save the libGoogleAnalytics.jar file within a /libs sub-directory within your Android project directory (creating the directory, if necessary).
Step 4: Adding the Library to your Project
Next, you need to add the Google Analytics SDK for Android to your Android project. Adding a jar file to your project from within Eclipse is easy. Follow these steps:
- 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 /libs directory.
Step 5: Adding Permissions to Your Project
The Google Analytics service must send statistics over the Internet. This means that your Android application will require permissions to perform such operations. Therefore, add two permissions to your application, if they are not there already.
Click on the Permissions tab of the Android manifest file for your project. Add a new Uses Permission element for android.permission.INTERNET and another for android.permission.ACCESS_NETWORK_STATE. Save your Android manifest file.
Step 6: Adding Tracking Code To Your Project
You’re ready to start using the Google Analytics SDK for Android to collect statistics! Simply drop the tracking code into the appropriate areas of your application and go. This is highly dependent on your specific application and what sorts of statistics you want to gather.
You’ll need to import the tracker:
Get an instance of the tracker:
GoogleAnalyticsTracker tracker = GoogleAnalyticsTracker.getInstance();
Start the tracker, updating to Google every 30 seconds:
tracker.startNewSession("UA-12345678-9", 30, this);
Track some usage (screens and dialogs map well to pageviews):
Stop the tracker:
And with that, you’ve got simple usage tracking.
That’s all it takes to set up Google Analytics for Android with your project. The Google Analytics SDK for Android is an easy way to collect useful information about how people use your application. It supports the collection of a number of different types of statistical events. Most of the power and flexibility of Google Analytics comes from determining the proper functional code areas to add statistics collection hooks.
As always, we look forward to your feedback.
About the Authors
Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development, Second Edition and Sams Teach Yourself Android Application Development in 24 Hours, Second Edition. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to [email protected], via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.