Understanding Android Firebase Analytics Integration

In this android tutorial, we are going to learn how to integrate Firebase Analytics in our android application.

Android user analytics is an insight tool that can help us understand how users interaction with our app. We can also understand different metrics as relate to user conversion, in-app purchasing and much more.

Before now, using Google analytic is one of the major ways to analyze important statistics in application. Since Google migrate to new Firebase platform, the recommended way to integrate Google analytics now in your app is by using Google Firebase Analytics.

I wrote a tutorial before on how to integrate Google analytics in your app but I will suggest you use the new Firebase Analytics.

According to Google Firebase Analytics – “a free and unlimited analytics solution. Analytics integrates across Firebase features and provides you with unlimited reporting for up to 500 distinct events that you can define using the Firebase SDK. Firebase Analytics reports help you understand clearly how your users behave, which enables you to make informed decisions regarding app marketing and performance optimizations.”

Before we can use Firebase in our android app, we have to create a Firebase application that we will link to our android app.


Firebase Account Registration

Go the Firsbase webite and register if you don’t have an account already. The registration is very simple. After registration, go to the console dashboard and click on create new project button.

In the window modal, enter the name of your project and country as shown in the image below

Android firebase

Click on the create project button to continue. In the console, click the button add Firebase to your android app as shown below

Android firebase

In the next window, add the Package name of your application and click the Add App button as shown below.

Android firebase

Click the continue button to download a Json configuration file which will be added in the root App directory of your project. See the image below

Android firebase

Following the instructions on how to add the Google Services plugin and the Firebase SDK in build.gradle as shown below.

Android firebase

We have successfully added Firebase to our project. We will now focus on our android project.


Create Android Project

In this project, we will learn to add Firebase analytic to our android project. Thereafter, we will go further to implement how to log events using Firebase Analytics Event.

The application UI will contain three images. We will find out through Firebase Event log which image users like the most. This will be determine through the number of clicks each image receives.

Before we dive into more details, it is important for us to understand what we are planning to achieve. Below is the screen-shot of the application we will be creating.


Lets start to soil our hands in code. Start up your IDE. For this tutorial, I am using the following tools and environment, feel free to use what works for you.

Windows 10

Android Studio

Sony Xperia ZL

Min SDK 14

Target SDK 23

To create a new android application project, follow the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidFirebaseAnalytics

Package: com.inducesmile.androidfirebaseanalytics

Select Blank Activity

Name your activity : MainActivity

Keep other default selections

Continue to click on next button until Finish button is active, then click on Finish Button.



We are going to update our project strings.xml file located in the values folder inside the res folder. Open the file and add the code below to it.

<string name="app_name">Firebase Analytic Example</string>
    <string name="app_name">Firebase Analytic Example</string>
    <string name="pick_a_car">If you are given to change to choose between these cars which one will you choose?</string>
    <string name="car_one">First Car</string>
    <string name="car_two">Second Car</string>
    <string name="car_three">Third Car</string>



Open the colors.xml file in the same location as the strings.xml file and add the code below to the file.

<?xml version="1.0" encoding="utf-8"?>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="colorBlack">#000000</color>



Open the default layout of the app, we are going to add a TextView and three ImageButton controls. The sample code is as shown below.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:text="@string/pick_a_car" />


MainActivity Class

In the MainActivity class we will get the instances of the three ImageButtons. Then a click event is wired to the ImageButton objects.

Imagine that these three ImageButtons represent three different projects we are selling. We will like to use the FirebaseAnalytic Event to determine which project will sell the most.

Please note that there are some FirebaseAnalytics Events and User Properties that Firebase Analytics automatically collects so you don’t need to write any code expect to integrate Firebase SDK

Android firebase

You can find more details about Firebase automatically collected events click here

Open the MainActivity class and paste the code below.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
import com.google.firebase.analytics.FirebaseAnalytics;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final String TAG = MainActivity.class.getSimpleName();
    private FirebaseAnalytics firebaseAnalytics;
    protected void onCreate(Bundle savedInstanceState) {
        firebaseAnalytics = FirebaseAnalytics.getInstance(MainActivity.this);
        ImageButton firstCar = (ImageButton)findViewById(R.id.car_one);
        ImageButton secondCar = (ImageButton)findViewById(R.id.car_two);
        ImageButton thirdCar = (ImageButton)findViewById(R.id.car_three);
        assert firstCar != null;
        assert secondCar != null;
        assert thirdCar != null;
    public void onClick(View v) {
        int carImageId = v.getId();
        String carImageName = null;
        if(v instanceof ImageButton){
            ImageButton clickedImage = (ImageButton)v;
            carImageName = clickedImage.getTag().toString();
        Toast.makeText(MainActivity.this, "You have clicked on " + carImageName, Toast.LENGTH_LONG).show();
        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.ITEM_ID, String.valueOf(carImageId));
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, carImageName);
        bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
        firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

The result of clicking on a particle image is shown below. Please note that if you created a new app in Firebase for analytics it might take around 24 hours before the analytic results start appearing in your Firebase console.

firebase event

This brings us to the end of this tutorial. I hope that you have learn something. Run your app and see for yourself.

You can download the code for this tutorial below. If you are having hard time downloading the tutorial, kindly contact me.

Remember to subscribe with your email address so that you will be among the first to receive my new android blog post once it is published.

Please if you love this tutorial, kindly download my android app – Complete Mathematics – in Google Play Store and let me know what you think about it.

Add a Comment