7th Oct

How to gather usage data from an Android application with Google Analytics

Wednesday, October 7, 2015 - 20:27
Author: Kohajda Dániel

What is Google Analytics?

Google Analytics is a free web analytics service. It was launched in November 2005 after Google acquiring Urchin Software. The service is available for tracking websites and mobile apps. Nowadays the Google Analytics is the most widely used web analytics service. It is also offered also available in two specific versions:

  • Google Analytics Premium: targeted at enterprise users
  • Google Analytics for Mobile Apps: An SDK is available to gather usage data from iOS, Android and Windows Phone

In October 2012 Google released the latest version of Analytics with many innovative new functions. This version has a new flexible tracking code to collect usage data from any device and new features such as real-time analytics and new charts.

The Google Analytics service is free, but there are some limitations:

  • On web the limit is 10 million hits/ month per Property (If we go over this limit, we can upgrade to Premium or reduce the amount of usage data by the client sampling)
  • On the mobile SDK the following limits are applied (If go over these limits, additional hits will not be processed):
    • ​​​200 000 hits per user per day
    • 500 hits per session (not including e-Commerce)


The registration is free, only a Google account is required. On the registration page we can choose what to track, a website or a mobile application. Besides that, we need to give an account name. This will be on the top of hierarchy and contains one or more Tracking-ID. Then we give the name of property. A Tracking-ID is generated for the property and we need this ID for tracking application in Analytics. After creation of the property it usually takes 24 hours to activate and see the statistics on the Analytics website. After this initialization time the statistics will be update in every half minute.  

For registration you can visit the following website: http://www.google.com/analytics/

Implement Google Analytics in Android application:

In this section, I would like to show how to implement Google Analytics in Android application. The following packages are available in the SDK.


  1. package com.google.android.gms.analytics.GoogleAnalytics;
  2. package com.google.android.gms.analytics.Tracker;

After we imported these packages in our Application class, we can make Tracker object that we need to send data to Google Analytics.

We should store Tracker names in Enum type and the Tracker objects in a Hashmap where key field is the Tracker name and the value is the Tracker object.

  1. public enum TrackerName {
  2.         APP_TRACKER, // Tracking this application.
  3.         GLOBAL_TRACKER, // Tracking all of business application.
  4.         ECOMMERCE_TRACKER, // Tracking e-Commerce in application.
  5. }
  7. static HashMap<TrackerName, Tracker> mTrackers =
  8. new HashMap<TrackerName, Tracker>();

There are three type of Tracker. This time we use only the App Tracker, because we want gather statistics from only this app. Global Tracker is also available to follow more applications in a business. The third type of tracker is for the e-Commerce, so we can track how many purchases happen in the application.

After the account and application registration we become a Property Key also know as Tracker-ID. This Property Key is responsible for identifying our Tracker in the Analytics service. The key is stored in a static field.

  1. private static final String APP_TRACKER_PROPERTY_KEY = "XX-00000000-1";

For initialize the Tracking object we need an instance of the Google Analytics class. Then create the new Tracker object and set the AutoActivityTracking property. We can set the Sample rate (in percentage) and the AdvertisingIdCollection for collect the demographic data.

  1. private static synchronized Tracker getTracker(TrackerName trackerId) {
  2.         if (!mTrackers.containsKey(trackerId)) {
  3.                 GoogleAnalytics analytics = GoogleAnalytics.getInstance(context);
  4.                 analytics.setLocalDispatchPeriod(30);
  6.                 if (trackerId == TrackerName.APP_TRACKER) {
  7.                         Tracker tracker = analytics.newTracker(APP_TRACKER_PROPERTY_KEY);
  9.                         tracker.enableAutoActivityTracking(true);
  10.                         tracker.enableExceptionReporting(true);
  11.                         tracker.setSampleRate(100);
  12.                         tracker.enableAdvertisingIdCollection(true);
  14.                         mTrackers.put(trackerId, tracker);
  15.                 }
  16.         }
  17.         return mTrackers.get(trackerId);
  18. }

To send the visited screen name to Analytics, we need to set the tracker with screen name. We can do it in the MainActivity using the following method:

  1. public static void sendAnalyticsScreenName(String screenName) {
  2.         Tracker tracker = getTracker(TrackerName.APP_TRACKER);         
  3.         tracker.setScreenName(screenName);
  4.         tracker.send(new HitBuilders.AppViewBuilder().build());
  6.         // Google Analytics LogCAT
  7.         Log.i("Google_Analytics_Screen_Name", screenName);
  8. }

In Android application a fragment defines its own view layout, so we need call the sendAnalyticsScreenName method when new fragment appear on the device screen.
We have to create a superclass for our fragment, where is an „abstract String getScreenName()” method.
In the inherited fragment classes we shall implement the abstract method body and return a String. This String is the Screen name and this is what we need to send to Google Analytics. Fragments have lifecycle events, so when appears or disappears on the screen called some methods.

For more information about fragment please read this page: http://developer.android.com/guide/components/fragments.html
There is an example to send fragment’s screen name to Analytics. This method should be called in the fragment’s onResume() method:

  1. @Override
  2. public void onResume() {
  3. . . .
  4.         MainActivity.sendAnalyticsScreenName(getScreenName());
  5.         super.onResume();
  6. }

In Google Analytics is possible to log events. For example an event is when the user push button or start an external application. We can see statistics on the Analytics website about what kind of events happened during interactions. The events can be classified by category, action name and label.

To send event to Analytics, we need to set the tracker with screen name, event category, event action and event label. The following method does this in the MainActivity:

  1. public static void sendAnalyticsEvent(String screenName, String category, String action, String label) {
  3.         Tracker tracker = getTracker(TrackerName.APP_TRACKER);
  4.         if (screenName != null){
  5.                 tracker.setScreenName(screenName);
  6.         }
  7. tracker.send(new HitBuilders.EventBuilder()
  8. .setCategory(category)
  9. .setAction(action)
  10. .setLabel(label).build());
  12.         // Google Analytics LogCAT
  13.         Log.i("Google_Analytics_Action_Name", screenName + " | "
  14. + category + " | " + action + " | " + label);
  15. }

There is an example for send an event to Analytics. This method called, when the user switches on the notifications:

  1. MainActivity.sendAnalyticsEvent(getScreenName(), getString(R.string.screen_action_category_switch), getString(R.string.screen_action_name_switch),
  2. getString(R.string.screen_action_label_notification_on));

Show the gathered statistics on chart:

On the Analytics website large number of chart is available. The chart’s data based on the collected statistics. We can see how many users use our application right now and which the top active screens are.

We can make a report about most visited screens on the last day, week or month. After we select a screen from the list, we can see more information, for example how much time spent by the users on the screen and how many events happened.


I think, the Google Analytics is the best way to gather usage statistics from a website or an application. For a mobile application it is important that most used functions show on the dashboard and the less used functions appears only in the menu. If we use the Google Analytics correctly, we can set the App’s functions in a priority order and we can make adjustments to our application what are based on real usage data.

Comments (1)
Friday, October 9, 2015 - 13:02