Firebase Analytics in Android

Firebase Analytics,, a free and unlimited analytics tool built from the ground up for mobile apps. Firebase Analytics is at the core of the Firebase platform, providing the insights you need to build successful apps.

SDK automatically captures a number of events and user properties and also allows you to define your own custom events to measure the things that uniquely matter to your business. Once the data is captured, it's available in a dashboard through the Firebase console. This dashboard provides detailed insights about your data — from summary data such as active users and demographics, to more detailed data such as identifying your most purchased items.
Firebase Analytics also integrates with a number of other Firebase features. For example, it automatically logs events that correspond to your Firebase Notifications and provides reporting on the impact of each campaign.

Lets start configuration. You need to complete few steps. Here is reference snap shot that helps you to make configuration done.

Now you got the google-services.json file. what you need to do, please paste this file inside the app folder of your project.

You have to add some dependency and project level and app level. Here is the detail.

build.gradle project level

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
    dependencies {
        classpath ''
        classpath ''
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files

allprojects {
    repositories {

task clean(type: Delete) {
    delete rootProject.buildDir

build.gradle app level

apply plugin: ''

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        applicationId "com.sunil.firebaseanalyticsandroid"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner ""
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), ''

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('', {
        exclude group: '', module: 'support-annotations'
    compile ''
    testCompile 'junit:junit:4.12'
    compile ''
// Add to the bottom of the file
apply plugin: ''

Wow You done almost steps. You need to get instance of FirebaseAnalytics and make enable for tracking your events.

import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {

    private FirebaseAnalytics mFirebaseAnalytics;

    protected void onCreate(Bundle savedInstanceState) {

        // Obtain the FirebaseAnalytics instance.
        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

        // make the firebase enabled and give session duration according to you

        Button subscribeButton = (Button) findViewById(;
        subscribeButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                Bundle bundle = new Bundle();
                String id = "music";
                String name = "Annual Menbership";

                bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
                bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
                bundle.putString(FirebaseAnalytics.Param.CURRENCY, "INR");
                bundle.putString(FirebaseAnalytics.Param.PRICE, "999.00");
                mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);


Make sure you have added these three permission in your AndroidManifest.xml file.

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

Once you click button then your event name -> SELECT_CONTENT will get reported in your Fire base console dashboard. It may delay in reporting you have to wait 24 hours or less. Here is my Tracking events in my firebase DashBoard.

Now you might be having question about the other events like first_open and session_start that already triggered default. So I can say some of events that already reserved. Here are details

The following event names are reserved and cannot be used:
  • app_clear_data
  • app_uninstall
  • app_update
  • error
  • first_open
  • in_app_purchase
  • notification_dismiss
  • notification_foreground
  • notification_open
  • notification_receive
  • os_update
  • session_start
  • user_engagement
You can learn more about other Events Here.. You can add your custom events also. 

From the doc "Custom parameters: Custom parameters are not represented directly in your Analytics reports, but they can be used as filters in audience definitions that can be applied to every report. Custom parameters are also included in data exported to BigQuery if your app is linked to a BigQuery project."

You can log your custom event like that:

Bundle params = new Bundle();
params.putString(FirebaseAnalytics.Param.VALUE, "play button clicked");
mFirebaseAnalytics.logEvent("Main_Activity_play_button_clicked", params);
Post Here.Thanks I hope it will help you to track your events. 

No comments:

Post a Comment

Debug Database in Android

In my last tutorial, we have learned how we can use persistence database in android. We also understood which database wrapper we can use...

Contact Me


Email *

Message *