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.

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

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

AnalyticsApplication.java

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

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.

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

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

MainActivity.java

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 *