Google Analytics is a service provided by Google that makes it easy to track what users do. Recently, the Google Analytics team released an Analytics SDK 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.
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. The service does not provide “live” statistics per se. That is, there is 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. 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: Setup 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
Next, go download the Google Analytics SDK for Android zip file. 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: Setting Project Permissions
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
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.
You’ll need to import the tracker:
Get an instance of the tracker:
tracker = GoogleAnalyticsTracker.getInstance();
Start the tracker, updating to Google every 30 seconds:
tracker.start("UA-12345678-9", 30, context);
Track some usage (screens and dialogs map well to pageviews):
(There are other types of events you can log as well. See the SDK docs for such things as the trackEvent() method.)
Stop the tracker:
And with that, you’ve got simple usage tracking.
Note that there is approximately a 24 hour delay before new events are displayed on the Google Analytics website.
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.