11 minutes

Integrating Google Analytics in Android Example

By Nilanchala @nilan, On Sep 17, 2023 Android 2.31K Views

1. Why Analytics in Mobile app ?

Analytics is one of the major parts in applications that drive revenue.  Mobile App Analytics help you set and track the goal conversions you want most: purchases, clicks, or simply time spent on your app. It is required to make business inclined towards user’s interest. For example, if you developing an application for Mobile Shopper app, it is important to track the below things.

  1. Number of users/active users from across the world
  2. Most viewed products, categories, brands, etc.
  3. Number of users placing request, etc.

The above information is required in order to improve the product and make the service is better.

2. Introduction to Google Analytics

The Google Analytics Platform lets you measure user interactions with your business across various devices and environments. The platform provides all the computing resources to collect, store, process, and report on these user-interactions.

3. Supported Features

Below are the currently supported features. Read more

  1. Google Play Integration
  2. Crash and Exception Reporting
  3. Custom Reports
  4. Event Tracking
  5. Flow Visualization
  6. Real-Time Reporting

4. How to use Google Analytics in android

This section of tutorial we will see detailed steps to install Google Analytics for your android application. Before begin with Analytics integration we need the following items

  1. Android developer SDK and required configurations. I assume you have it already, if not you can follow Android official documentation
  2. Google Analytics SDK for Android v3, with libGoogleAnalyticsServices.jar file. Copy this file in your project’s /libs directory.
  3. Codebase of an Android app that you like to implement the Google Analytics
  4. Create a Google Analytics account (Requires Gmail), and then create a new app property and view in the Analytics console.

5. Using Google Analytics Detailed Steps

Before starting with section, I assume you already have Codebase of an Android app that you like to implement the Google Analytics. But to simplify my example, I am creating an simple Android App and integrating with Google Analytics.

5.1. Creating Google Analytics Account and App Property

  1. Login to your Google Analytics Account. Then press “Admin” button on the right side of your screen.
  2. Create a new Account, if you don’t have one and then create a new property. While creating a new property choose Mobile App.
  3. After property is created, you will get the tracking Id. This tracking id will be used as configuration in android app.

5.2. Add analytics.xml configuration file

Add analytics.xml configuration file in your application values folder

<?xml version="1.0" encoding="utf-8"?>

    <!-- Replace placeholder ID with your tracking ID -->
    <string name="ga_trackingId">UA-xxxxxxxxxx</string>

    <!-- Enable automatic activity tracking -->
    <bool name="ga_autoActivityTracking">true</bool>

    <!-- Enable automatic exception tracking -->
    <bool name="ga_reportUncaughtExceptions">true</bool>    


5.3. Android Activity Layout (activity_main.xml)

For this example, I have created an sample Activity with two buttons for tracking event and exceptions.

<LinearLayout xmlns:android=""
    tools:context=".MainActivity" >

        android:text="@string/info" />

        android:text="Track Event" />

        android:text="Track Crash" />


5.4. Android Activity (

Now, you need to write the tracking code in your activity class. For that simply add the activiyStart() and activityStop() methods to the onStart() and onStop() methods of each of your Activities as in the following example. This two method enables basic activity tracking.

package com.javatechig.ganalytics;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

	private EasyTracker easyTracker = null;

	protected void onCreate(Bundle savedInstanceState) {

		easyTracker = EasyTracker.getInstance(MainActivity.this);

		findViewById( OnClickListener() {

			public void onClick(View view) {
						"envet_name", "button_name/id", null).build());

		findViewById( OnClickListener() {

			public void onClick(View view) {

				try {
					int a[] = new int[2];
					int num = a[4];
				} catch (ArrayIndexOutOfBoundsException e) {
									new StandardExceptionParser(MainActivity.this, null)
											.getDescription(Thread.currentThread().getName(), e), false).build());


	public void onStart() {

	public void onStop() {

	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(, menu);
		return true;


In this above activity code sample, we are tracking event an crashes on two button clicks. Just for demonstation purpose I am creating sample exception and tracking it.

6. Google Analytics Output

Google Analytics Report

7. Download Complete Example

[download url=””]
nilan avtar


I'm a blogger, educator and a full stack developer. Mainly focused on Java, Spring and Micro-service architecture. I love to learn, code, make and break things.