Integrate Google Analytics for Firebase into Android

Integrate Google Analytics for Firebase into Android

Google Analytics for Firebase is a web analytics service that can be integrated into Android application. It allows monitoring application usage, collect information about mobile devices and lets to understand who is using application. By default, Google Analytics for Firebase automatically captures certain events, but it allows defining custom events as well.

To use the Google Analytics for Firebase, we need to create a Firebase project. It can be done by following these steps:

  1. Login to the Firebase Console.
  2. Click "Add project".
  3. Provide project name and click "Continue".
  4. In the next screen, make sure "Enable Google Analytics for this project" is selected and click "Continue".
  5. Select existing Google Analytics account or create a new account.
  6. Click "Create project".
  7. When the project was created, click the Android icon.
  8. Provide Android package name. It can be found in the AndroidManifest.xml file (example: com.example.app).
  9. Click "Register app".
  10. Download the google-service.json file and add into the app directory.

We need to add Google services plugin for Gradle to project-level build.gradle file. This plugin process the google-services.json file and generates resources that can be used in the code.

build.gradle

buildscript {
    // Other configuration

    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.3.3'
    }
}

In the module’s build.gradle file, we need to apply the Google services Gradle plugin. Also, we need to add firebase-analytics dependency.

app/build.gradle

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

android {
    // ...
}

dependencies {
    // Other dependencies
    // ...
    implementation 'com.google.firebase:firebase-analytics:17.4.4'
}

We can log events by using logEvent() method. We must provide an event name, and a bundle of parameters (key-value pairs). We can use pre-defined events or create the own event.

app/src/main/java/com/example/app/MainActivity.kt

package com.example.app

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.google.firebase.analytics.FirebaseAnalytics

class MainActivity : AppCompatActivity()
{
    private lateinit var firebaseAnalytics: FirebaseAnalytics

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        firebaseAnalytics = FirebaseAnalytics.getInstance(this)

        var bundle = Bundle()
        bundle.putString(FirebaseAnalytics.Param.ITEM_ID, "MainActivity")
        firebaseAnalytics.logEvent(FirebaseAnalytics.Event.APP_OPEN, bundle)

        // Custom event
        bundle = Bundle()
        bundle.putString("custom_param", "Hello")
        firebaseAnalytics.logEvent("custom_event", bundle)
    }
}

In the Firebase dashboard, we can use DebugView that allows to view logged events by development devices in near real-time.

To enable Analytics Debug mode on an Android device, we can use the adb command-line tool, which can be found in Android SDK location. Open terminal and navigate to <ANDROID_SDK_LOCATION>/platform-tools directory.

Execute the following command with your application's package name:

adb shell setprop debug.firebase.analytics.app com.example.app

Run application again. In the Firebase dashboard, open DebugView. The device should be displayed in the Debug Device list. The stream shows the events which have been logged.

We can disable Analytics Debug mode by executing the following command:

adb shell setprop debug.firebase.analytics.app .none.

Leave a Comment

Cancel reply

Your email address will not be published.