How To Add Firebase Analytics To Android

April 18, 2018

Step 1: Add Firebase To Android.

Edit module/app build.gradle.

dependencies {
    // https://firebase.google.com/support/release-notes/android
    implementation 'com.google.firebase:firebase-core:12.0.1'
}

NOTE: com.google.firebase:firebase-core is a recommended alias for the com.google.firebase:firebase-analytics library.

Once you included com.google.firebase:firebase-core, Firebase Analytics automatically track all activty view event (you don’t have to write any code).

If you need to log custom events

val analytics = FirebaseAnalytics.getInstance(this)

val bundle = Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id)
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name)
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image")

analytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle)

NOTE: I have yet to deeper than activity event logging, so refer to Firebase Analytics Doc for more info.

Disable Analytics For Debug Build

I believe Firebase Analytics is enabled by default, even for Debug build.

To disable analytics for Debug build, edit app/module build.gradle.

android {
    // ...
    buildTypes {
        debug {
            // manifestPlaceholders = [crashlyticsEnabled: false, analyticsEnabled: false]
            manifestPlaceholders = [analyticsEnabled: false]
        }
        release {
            manifestPlaceholders = [analyticsEnabled: true]
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

Edit AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest ...>
    <application ...>
    
        <meta-data
            android:name="firebase_analytics_collection_enabled"
            android:value="${analyticsEnabled}" />

        ...

    </application>
</manifest>

Firebase Analytics Report

To retrieve the Analytics report, goto Firebase Console, select project, click Analytics -> Dashboard from the left panel.

Active Users are recorded by default.

Firebase Analytics Dashboard

Activity/Screen engagement are recorded by default.

Firebase Analytics Activity

Basic Events are recorded by default.

Firebase Analytics Events

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.