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

Rail Fence Cipher Program in C and C++[Encryption & Decryption]

Here you will get rail fence cipher program in C and C++ for encryption and decryption. It is a kind of transposition cipher which is also known as zigzag cipher. Below is an example. Here Key = 3. For encryption we write the message diagonally in zigzag form in a matrix having total rows = key and total columns = message length. Then read the matrix row wise horizontally to get encrypted message. Rail Fence Cipher Program in C #include<stdio.h> #include<string.h> void encryptMsg(char msg[], int key){ int msgLen = strlen(msg), i, j, k = -1, row = 0, col = 0; char railMatrix[key][msgLen]; for(i = 0; i < key; ++i) for(j = 0; j < msgLen; ++j) railMatrix[i][j] = '\n'; for(i = 0; i < msgLen; ++i){ railMatrix[row][col++] = msg[i]; if(row == 0 || row == key-1) k= k * (-1); row = row + k; } printf("\nEncrypted Message: "); for(i = 0; i < key; ++i) f...

Data Encryption Standard (DES) Algorithm

Data Encryption Standard is a symmetric-key algorithm for the encrypting the data. It comes under block cipher algorithm which follows Feistel structure. Here is the block diagram of Data Encryption Standard. Fig1: DES Algorithm Block Diagram [Image Source: Cryptography and Network Security Principles and Practices 4 th Ed by William Stallings] Explanation for above diagram: Each character of plain text converted into binary format. Every time we take 64 bits from that and give as input to DES algorithm, then it processed through 16 rounds and then converted to cipher text. Initial Permutation: 64 bit plain text goes under initial permutation and then given to round 1. Since initial permutation step receiving 64 bits, it contains an 1×64 matrix which contains numbers from 1 to 64 but in shuffled order. After that, we arrange our original 64 bit text in the order mentioned in that matrix. [You can see the matrix in below code] After initial permutation, 64 bit text passed throug...

Experimental: Reducing the size of .NET Core applications with Mono's Linker

The .NET team has built a linker to reduce the size of .NET Core applications. It is built on top of the excellent and battle-tested mono linker . The Xamarin tools also use this linker so it makes sense to try it out and perhaps use it everywhere! "In trivial cases, the linker can reduce the size of applications by 50%. The size wins may be more favorable or more moderate for larger applications. The linker removes code in your application and dependent libraries that are not reached by any code paths. It is effectively an application-specific dead code analysis ." - Using the .NET IL Linker I recently updated a 15 year old .NET 1.1 application to cross-platform .NET Core 2.0 so I thought I'd try this experimental linker on it and see the results. The linker is a tool one can use to only ship the minimal possible IL code and metadata that a set of programs might require to run as opposed to the full libraries. It is used by the various Xamarin products to extract...