Android Google Analytics Integration Tutorial

This tutorial is about google analytics android integration.

Google Analytics will help you to track how many people are using your app, real time users, etc.

The tutorial is divided into three parts, first create an account in Google Analytics, then generate configuration file and finally implement google analytics in your app.

Android Google Analytics Integration Tutorial

1. Create Google Analytics Account

Go to https://analytics.google.com and login with your gmail credentials.

There select Mobile app type and add all details like Account Name, App Name, etc according to you. I have shown one example in below screenshot.

Android Google Analytics Integration Tutorial 1

Now click on Get Tracking ID button and then accept the terms to create the account. After creating the account you will see a screen as shown below. Here you can see the Tracking ID, just save it somewhere as we will need it later.

Android Google Analytics Integration Tutorial 2

2. Generate google-services.json Configuration File

1. Go to https://developers.google.com/mobile/add. Make sure you are logged in with same gmail account with which you created analytics account.

2. Click on Pick a platform and choose Android App. Give any app name and enter the package name of your android app in which you want to integrate analytics. See below screenshot as an example.

Android Google Analytics Integration Tutorial 3

Click on Choose and configure services to continue.

3. Then select Analytics as services, see below screenshot.

Android Google Analytics Integration Tutorial 4

4. Now select the google analytics account that you created earlier. As I created it with name Google Analytics so I selected it, see below screenshot.

Android Google Analytics Integration Tutorial 5

After that click on Enable Analytics Service and then click on Generate configuration files button.

5. In the next window click on Download button to download google-services.json file.

Android Google Analytics Integration Tutorial 6

3. Android App Integration

Here I have created a demo android project with name GoogleAnalytics as an example.

1. Paste the google-services.json configuration file inside app folder of your project, see below screenshot.

Android Google Analytics Integration Tutorial 7

2. Add following dependency in project level build.gradle file.

classpath 'com.google.gms:google-services:3.0.0'

3. Add another dependency in app level build.gradle file. After that sync the project.

compile 'com.google.android.gms:play-services-analytics:10.2.4'

4. Create AnalyticsApplication.java file inside your project package and add following code in it.

AnalyticsApplication.java

package com.googleanalytics;

import android.app.Application;

import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.Tracker;

public class AnalyticsApplication extends Application {

    private static GoogleAnalytics sAnalytics;
    private static Tracker sTracker;

    synchronized public Tracker getDefaultTracker() {
        sAnalytics = GoogleAnalytics.getInstance(this);

        if (sTracker == null) {
            sTracker = sAnalytics.newTracker(R.xml.global_tracker);
        }

        return sTracker;
    }
}

5. Create a directory with name xml in res folder of your project. Then create global_tracker.xml file inside xml folder with following code.

global_tracker.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="ga_trackingId">Tracking ID</string>
</resources>

Replace Tracking ID with the ID that you got after creating the analytics account.

5. Add internet and network access permission in AndroidManifest.xml file. Also add a property android:name=”.AnalyticsApplication” inside <application> tag. See below screenshot.

Android Google Analytics Integration Tutorial 8

6. Now for sending tracking details to google analytics first create tracker instance by using following lines.

AnalyticsApplication application = (AnalyticsApplication) getApplication();
mTracker = application.getDefaultTracker();

7. For sending tracking details you have to use following two lines inside onResume() method of an activity.

mTracker.setScreenName("Main Activity");
mTracker.send(new HitBuilders.ScreenViewBuilder().build());

For demonstration purpose below I have shown the code of MainActivity.java of my demo project.

MainActivity.java

package com.googleanalytics;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;

public class MainActivity extends AppCompatActivity {
    Tracker mTracker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Obtain the shared Tracker instance
        AnalyticsApplication application = (AnalyticsApplication) getApplication();
        mTracker = application.getDefaultTracker();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //sending tracking information
        mTracker.setScreenName("Main Activity");
        mTracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
}

8. Save and run your app. In google analytics console you will see real time user as shown in below screenshot.

Android Google Analytics Integration Tutorial 9

Comment below if you have any queries regarding above Android Google Analytics Integration tutorial.

Leave a Comment

Your email address will not be published. Required fields are marked *