Android Google Analytics Integration Example Tutorial

How to add implement Analytics in android app to track visitors and see live visitor from location filter step by step tutorial.

Google Analytics is one of the most popular and useful analytics engine provided by Google.com and used by millions of website including android applications. With the use of Analytics a android developer can easily track all its application users activity like where are the actually from and at the present time on which activity there are how much visitors is online. We can see live feed of all application activity an monitor users count . So here is the complete step by step tutorial for Android Google Analytics Integration Example Tutorial with step by step guide .

android-project-download-code-button

Important steps we have follow before getting started :

1. Start a new project in Android Studio .

2. Open your project’s build.gradle(Project) file .

3. Add classpath ‘com.google.gms:google-services:3.0.0’ inside this file .

4. Now open your project’s build.gradle(Module:app) file .

5. Add apply plugin: ‘com.google.gms.google-services’ and compile ‘com.google.android.gms:play-services-analytics:9.0.0’ in this file .

6. Next step is to Generate google-services.json configuration file from Google Developers .

7. GoTo Google Developers .

8. Log in with your Google Gmail ID .

9. Enter your App name & Application package name .

10. Now select Analytics and follow all the configurations like my below screen shot .

Android Google Analytics Integration Example Tutorial

11. Click on  ENABLE ANALYTIC SERVICE button .

12. Click on GENERATE CONFIGURATION FILE button .

13. Now again click on Download google-services-.json button .

14. Now after download the file put that file into your project’s app folder .

15. Here you go now all the major steps has been done now next step is to Start coding .

Android Google Analytics Integration Example Tutorial step by step Guide .

Code for MainActivity.java file.

package com.android_examples.googleanalytics_android_examplescom;
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 tracker ;
    AnalyticsApplication analyticsApplication ;

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

        analyticsApplication = (AnalyticsApplication) getApplication();

        tracker  = analyticsApplication.getDefaultTracker();


    }

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

        tracker.setScreenName("Main Activity Home");

        tracker.send(new HitBuilders.ScreenViewBuilder().build());
    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.android_examples.googleanalytics_android_examplescom.MainActivity"
    android:background="#FFF8E1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Google Analytics Integration Example Tutorial"
        android:gravity="center"
        android:textSize="25dp"
        android:textColor="#000000"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

Code for AnalyticsApplication.java file.

package com.android_examples.googleanalytics_android_examplescom;

/**
 * Created by Juned on 12/18/2016.
 */

import android.app.Application;

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

public class AnalyticsApplication extends Application {
    private Tracker mTracker;


    synchronized public Tracker getDefaultTracker() {
        if (mTracker == null) {
            GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
            // To enable debug logging use: adb shell setprop log.tag.GAv4 DEBUG
            mTracker = analytics.newTracker(R.xml.global_tracker);
        }
        return mTracker;
    }
}

Code for AndroidManifest.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android_examples.googleanalytics_android_examplescom">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name="com.android_examples.googleanalytics_android_examplescom.AnalyticsApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Screenshots:

Google Analytics dashboard screenshot :

Click here to download Android Google Analytics Integration Example Tutorial project with source code.

Leave a Reply

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