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 {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'
        classpath 'com.google.gms:google-services:3.0.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

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


build.gradle app level

apply plugin: 'com.android.application'

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.0.0'
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-core:9.8.0'
}
// Add to the bottom of the file
apply plugin: 'com.google.gms.google-services'


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

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.google.firebase.analytics.FirebaseAnalytics;

public class MainActivity extends AppCompatActivity {

    private FirebaseAnalytics mFirebaseAnalytics;


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

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

        // make the firebase enabled and give session duration according to you
        mFirebaseAnalytics.setAnalyticsCollectionEnabled(true);
        mFirebaseAnalytics.setMinimumSessionDuration(20000);

        Button subscribeButton = (Button) findViewById(R.id.analytics);
        subscribeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            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

Name

Email *

Message *