devxlogo

Getting Started with Google Analytics on Android

Getting Started with Google Analytics on Android

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:

  1. 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.
  2. Download the Google Analytics SDK for Android zip file and extract it.
  3. Create and configure a Google Analytics developer account to use for collecting statistics.
  4. Integrate the SDK into your application project in Eclipse.
  5. Start using the SDK, adding statistics-gathering hooks in the appropriate places.
  6. 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.

The resulting account creation generates a block of JavaScript for dropping into your “website” for tracking. For mobile developers, you’re really only interested in the unique Web Property or UA number associated with your account. This number is located in the JavaScript and starts with “UA-” followed by some numbers. Store this information somewhere — you will need to use this number within your application to send statistics to the proper Google Analytics account.

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:

  1. Add the libGoogleAnalytics.jar to your project.
  2. Add two permissions to your Android manifest file.

Adding the JAR file to your project within Eclipse is easy. Simply:

  1. Click on the Project properties for your Android project.
  2. Under the Java Build Path settings, select the Libraries tab.
  3. Click the Add JARs… button and choose the jar within the /libs directory.

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:

  1. Click on the Permissions tab of the Android manifest file for your project.
  2. Add a new Uses Permission element for android.permission.INTERNET.
  3. Add another Uses Permission element for android.permission.ACCESS_NETWORK_STATE.
  4. 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 onDestroy() method.

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:

tracker.trackPageView("/ClickTracker-Main-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:

tracker.close();

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:

Google Analytics dashboard, no stats

And then, about a day later, your dashboard should look more like this!

Google Analytics Dashboard with few stats

Conclusion

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.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist