Integrate Google Analytics into your Android app

Google Analytics data about your Android app will help you see if your design is logical and may lead you to think of ways to increase user engagement.

Google Analytics is a great tool for tracking metrics about how users are interacting with your applications or websites. It is so simple to set up that I highly recommend you use it in your Android development. In this tutorial, I describe how to set up and use Google Analytics for Android and share insights into the information I've gleaned from it.

Google Analytics setup

First, you must sign up to use Google Analytics through an existing Google account. Next, you must create an account for your application. It doesn't matter what URL you use -- just enter something meaningful that you'll be able to remember later when you look up statistics.

Android setup

In order to set up Google Analytics inside your Android application, download the zip file for Android. Extract the file once it downloads and copy the jar file inside into the lib folder of your Android project. (If a lib folder does not already exist, simply create one.) Then, if you're using Eclipse, follow these steps:

  1. Right-click on your project.
  2. Choose Properties.
  3. Choose Java Build Path from the menu on the left.
  4. Click the Libraries tab at the top of the right side.
  5. Click Add JARs....
  6. Choose the jar file inside the lib folder.
  7. Click the OK button.

Next, add the following permissions to your application's Manifest.xml file:

  • <uses-permission android:name="android.permission.INTERNET" />
  • <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Now that your project is set up to start using the Google Analytics API, you should insert the necessary code and start testing. To keep things simple, let's focus on two main aspects of tracking: page views and events. You can get more advanced by tracking custom variables, Ecommerce data, setting sampling rates, and even tracking installation referrals, but I'll leave you to investigate all those options once are you comfortable with the basics.

Page views

It makes sense to track each Activity of your application as a page, so below is a sample onCreate method showing you how to track a page view:

private GoogleAnalyticsTracker mGATracker;


public void onCreate(Bundle savedInstanceState) {

try {



mGATracker = GoogleAnalyticsTracker.getInstance();

mGATracker.startNewSession( "UA-YOUR-ACCOUNT-HERE", this );

mGATracker.trackPageView( "/YourActivity" );


catch( Exception error ) {

Log.e( "<YOUR_TAG>", "onCreate: " + error.toString() );



Alternatively, you could have specified a dispatch interval for startNewSession as such, where 20 is the number of seconds between automatic dispatches of data:

mGATracker.startNewSession( "UA-YOUR-ACCOUNT-HERE", 20, this );

By tracking each activity as a page view, you can see how deep users are navigating through your application, as well as which sections are the most and the least popular.


Events are the next thing you'll want to track. If you're having trouble deciding which events to track, start with all of your buttons' onClick listeners. Below is a sample listener:

public void ButtonClick( View v ) {

try {

// Category, Action, Label, Value

mGATracker.trackEvent( "Clicks", "Button", "clicked", 0 );


catch( Exception error ) {

Log.e("<YOUR_TAG>", "ButtonClick: " + error.toString() );



By tracking and analyzing user clicks in your application, you'll be able to figure out which options in your application are most popular, which UI elements might be difficult for users to identify as a clickable area, and overall user engagement.

I also highly suggest you track an event every time the user long-presses an item and/or uses the hardware menu key. I find the two UI elements associated with these actions (context menus and the options menu) in Android incredibly useful when trying to design a UI because it allows the interface to be much cleaner and simpler, while also still providing many actionable choices. I've learned through experience and analytics that the average user hardly ever thinks to long-press items or hit the menu button. As a power user, these are the first two things I try when I'm not sure what to do next inside an app, so this information has made me re-think which elements I stick in the options menu and has also forced me to stop using long-press context menus.


When you call startNewSession on your GoogleAnalyticsTracker object, you may pass in a dispatch interval parameter that will automatically dispatch (send) the collected data to Google's servers. However, Google suggests that you batch your dispatch requests to reduce the amount of overall network traffic, which will also cut down on battery usage, so I prefer to call dispatch manually in the onDestroy method of my activities. This is also a good place to call stopSession on your GoogleAnalytics object.


public void onDestroy() {

try {





catch( Exception error ) {

Log.e("<YOUR_TAG>", "onDestroy: " + error.toString() );




The benefits you'll reap from using Google Analytics in your Android applications are well worth the little bit of effort it takes to integrate. All the information gathered can help you figure out if you've designed and organized your app in a logical manner and may help you think of ways to increase user engagement, all of which will go a long way towards making you a better developer.