Add Google Analytics in Android Application

Developers know how much is Google analytics is important for their website to track users and session. Integration of Google analytics in a website is very easy we simply login to Google analytics and get analytics code and paste in our website page. Now in this post we  learn how to integrate Google analytics in our Android application. When we integrate analytics in our application we get various information like No. of users are active in our application, Location of users, Version which are they using and also some other information

 

Integrating Google Analytics in Android Application

Step 1. Go to Firebase console and create a new project .

Step 2. Create new project name your project and select country and click on create the project
 

Google Analytics

Step 3. Now click on Add Firebase to your Android app and name your application package name and your system SHA1 here

GoogleAnalytics

Step 4.  Now once you complete above step google-services.json is automatically downloaded on your browser if not you can download on link

 

Setup Google Analytics in App

Step 1. Now you need to move downloaded google-service.json file in your application Plz paste into specific location please follow bellow image

google-service.json

Step 2. Now open Project Gradle and add google service library in it

buildscript {
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
  }
  dependencies {
    ...
    // Add this line
    classpath 'com.google.gms:google-services:4.3.3'
  }
}

allprojects {
  ...
  repositories {
    // Check that you have the following line (if not, add it):
    google()  // Google's Maven repository
    ...
  }
}

 

Step 3. Now open your Application gradle and Google analytics library in it apply plugin as shown bellow

apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'

dependencies {
  // add the Firebase SDK for Google Analytics
  implementation 'com.google.firebase:firebase-analytics:17.2.2'
  // add SDKs for any other desired Firebase products
  // https://firebase.google.com/docs/android/setup#available-libraries
}

 

Step 4: Create Application class and add below code

package com.app.googleanalytics

import android.app.Application
import com.google.android.gms.measurement.module.Analytics
import com.google.firebase.analytics.FirebaseAnalytics

class MyApp():Application() {
    lateinit  var mFirebaseAnalytics: FirebaseAnalytics
    override fun onCreate() {
        super.onCreate()
        mFirebaseAnalytics= FirebaseAnalytics.getInstance(applicationContext) 
    }

    fun getFireAnalytic():FirebaseAnalytics{
        if(mFirebaseAnalytics==null)
        mFirebaseAnalytics= FirebaseAnalytics.getInstance(applicationContext)
        return mFirebaseAnalytics
    }
}

 

Step 5: Add Evetns inside Activities

Create Track Bundle

val bundle:Bundle=Bundle()
bundle.apply {
    putString(FirebaseAnalytics.Param.ITEM_NAME, "MainScreen")
}

 

 

Then you call the logEvent method of the FirebaseAnalytics object

(application as MyApp).getFireAnalytic().logEvent(FirebaseAnalytics.Event.VIEW_ITEM,bundle)

 

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        android:gravity="center"
        tools:context=".MainActivity">

   <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
   android:id="@+id/btnOne" android:text="Event One" android:textSize="20sp"
           android:layout_margin="10dp"
           android:minHeight="40dp"
           android:background="#FA3276"
           android:padding="10dp"
           android:textColor="#FFFFFF"
   />
    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:id="@+id/btnTwo" android:text="Event One" android:textSize="20sp"
            android:layout_margin="10dp"
            android:minHeight="40dp"
            android:background="#FA3276"
            android:textColor="#FFFFFF"
            android:padding="10dp"
    />
</LinearLayout>

MainActivity Code

package com.app.googleanalytics

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.google.firebase.analytics.FirebaseAnalytics
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val bundle:Bundle=Bundle()
        bundle.apply {
            putString(FirebaseAnalytics.Param.ITEM_NAME, "MainScreen")
        }
        (application as MyApp).getFireAnalytic().logEvent(FirebaseAnalytics.Event.VIEW_ITEM,bundle)

        btnOne.setOnClickListener(View.OnClickListener {
            val bundle:Bundle=Bundle().apply {
                putString(FirebaseAnalytics.Param.ITEM_NAME, "Button One Event")
            }
            (application as MyApp).getFireAnalytic().logEvent(FirebaseAnalytics.Event.VIEW_ITEM,bundle)


        })

        btnTwo.setOnClickListener(View.OnClickListener {
            val bundle:Bundle=Bundle().apply {
                putString(FirebaseAnalytics.Param.ITEM_NAME, "Button TWo Event")
            }
            (application as MyApp).getFireAnalytic().logEvent(FirebaseAnalytics.Event.VIEW_ITEM,bundle)


        })
    }
}

Firebase Analytics