Skip to main content

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.

The post Android Google Analytics Integration Tutorial appeared first on The Crazy Programmer.



from The Crazy Programmer http://www.thecrazyprogrammer.com/2017/08/android-google-analytics-integration-tutorial.html

Comments

Popular posts from this blog

dotnet sdk list and dotnet sdk latest

Can someone make .NET Core better with a simple global command? Fanie Reynders did and he did it in a simple and elegant way. I'm envious, in fact, because I spec'ed this exact thing out in a meeting a few months ago but I could have just done it like he did and I would have used fewer keystrokes! Last year when .NET Core was just getting started, there was a "DNVM" helper command that you could use to simplify dealing with multiple versions of the .NET SDK on one machine. Later, rather than 'switching global SDK versions,' switching was simplified to be handled on a folder by folder basis. That meant that if you had a project in a folder with no global.json that pinned the SDK version, your project would use the latest installed version. If you liked, you could create a global.json file and pin your project's folder to a specific version. Great, but I would constantly have to google to remember the format for the global.json file, and I'd constan...

15 Web Design Trends to Watch in 2018

The modern world is full of extraordinary things that influence our imagination and mood. Our soul needs a perfect atmosphere and impressive spots. To apply such things in practice, we have submitted the list of the web trends that deserve your attention. Robert frost design analysis will meet all your wishes and expectations. Image Source Web Design Trends to Watch in 2018 1. More Organic Shapes Until this year, web design, as well as mobile design, were based on the right-angled and sharp-edged shapes. However, it seems that this year will bring some significant changes in the field of web design. The recent trends will offer the absolute rounded corners. In addition, the web design of 2018 will make the real things look like the cartoonish ones. 2.   Bold Minimalism Although some of you may think that this web design trend will not attract the Internet users. Indeed, the notion of minimalism is often associated with boredom and dullness. However, in this case, bold ...

R vs Python for Machine Learning

There are so many things to learn before to choose which language is good for Machine Learning. We will discuss each and everything about R as well as Python and the situation or problem in which situation we have to use which language. Let’s start Python and R are the two most Commonly used Programming Languages for Machine Learning and because of the popularity of both the languages Novice or you can say fresher are getting confused, whether they should choose R or Python language to commence their career in the Machine learning domain. Don’t worry guys through this article we will discuss R vs Python for Machine Learning. So, without exaggerating this article let’s get started. We will start it from the very Basics things or definitions. R vs Python for Machine Learning Introduction R is a programming language made by statisticians and data miners for statistical analysis and graphics supported by R foundation for statistical computing. R also provides high-quality graphics and...