Firebase Analytics Implementation in Android

Mar 10, 2023

8 Minutes Read

By Rajvi Shah

GA4FirebaseAndroid

Firebase analytics with a combination of Google Analytics 4 is a powerful pack for Mobile applications to track user engagement and usage of their apps. It provides detailed insights into user behavior, allowing developers to make informed decisions about how to improve their apps.

Firebase analytics can be easily implemented in any Android, iOS or web apps, providing marketers with the ability to track user activity and engagement. With Firebase analytics, developers can gain valuable insights into how users interact with their apps, enabling them to optimize the user experience and increase conversions.

By implementing Firebase analytics in Mobile apps, marketers can better understand their users’ needs and preferences. This will help them create more engaging experiences that will keep users coming back for more. Lets dive into a step by step guide to integrate Firebase SDK in Android Application.

Add Firebase Project in the App

  1. In Firebase console, click Add project, then select or enter a Project name.
    If you have an existing Google Cloud project, you can select the project from the dropdown menu to add Firebase resources to that project.
  2. (Optional) If you are creating a new project, you can edit the Project ID.

Firebase automatically assigns a unique ID to your Firebase project. Visit Understand Firebase Projects to learn about how Firebase uses the project ID.

  1. Click on Continue

  1. Choose an existing Google Analytics Account or Create a new account and choose GA4 property from the drop-down, then click Create project.

Firebase automatically provisions resources for your Firebase project. When the process completes, you'll be taken to the overview page for your Firebase project in the Firebase console.

Register the App with Firebase

  1. Go to the Firebase console.
  2. In the center of the project overview page, click the Android icon (plat_android) or Add app to launch the setup workflow.
  3. Enter your app's package name in the Android package name field.
  4. (Optional) Enter other app information: App nickname and Debug signing certificate SHA-1.
  5. Click Register app.

Add Firebase Configuration File

Add the Firebase Android configuration file to your app:

  1. To make the google-services.json config values accessible to Firebase SDKs, you need the Google services Gradle plug-in.
  2. Add the plug-in as a build script dependency to your project-level build.gradle file. Root-level (project-level) Gradle file (<project>/build.gradle):

  1. Click Download google-services.json to download Firebase Android config file, place config file into the module (app-level) directory of app.
  2. In root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google's Maven repository, for an updated version.

buildscript {

  repositories {

    // Check that you have the following line (if not, add it):

    google()  // Google's Maven repository

  }

  dependencies {

    // ...

    // Add the following line:

    classpath 'com.google.gms:google-services:4.3.5'  // Google Services plugin

  }

}

  1. In (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin:

apply plugin: 'com.android.application'

// Add the following line:

apply plugin: 'com.google.gms.google-services'  // Google Services plugin

android {

  // ...

}

  1. Include dependency for the Analytics Android library in your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle). Include the latest version of the SDK.
    Option 1: [Used to include other libraries] We recommend using the Firebase Android BoM to control library versioning.

dependencies {
   
// Declare the dependency for the Analytics library
   implementation 'com.google.firebase:firebase-analytics:
18.0.2'


// Import the Firebase BoM
  implementation platform('com.google.firebase:firebase-bom:31.2.0')

 
// When using the BoM, you don't specify versions in Firebase library dependencies

 // Add the dependency for the Firebase SDK for Google Analytics
 implementation '
com.google.firebase:firebase-analytics'

 // TODO: Add the dependencies for any other Firebase products you want to use
 // See https://firebase.google.com/docs/android/setup#available-libraries
 // For example, add the dependencies for Firebase Authentication and Cloud Firestore
  implementation 'com.google.firebase:firebase-auth'
 implementation
'com.google.firebase:firebase-firestore'

}

        Option 2: [Used to include only Analytics library] Include Analytics dependency with the latest version of the SDK. add the dependencies for the Firebase products that you want to use in your app.
        

dependencies {

    // Declare the dependency for the Analytics library

    implementation 'com.google.firebase:firebase-analytics:21.2.0'

}

Implement Custom Events

  1. Include Analytics Libraries and SDKs in your Android project (if not included), mentioned in above step.
  2. By doing the above step, the automated events will start recording in your Firebase account. Events like screen_view, first_open, session_start, app_remove, app_updated, and other automated events lists are accessible.
  3. Logging the events: Here logging the event when user clicks on the share image option of the app

Bundle params = new Bundle();
params.putString(
"screen_name", screenName);
params.putString(
"image_name", name);
params.putString(
"full_text", text);
mFirebaseAnalytics.logEvent(
"share_image", params);

Testing the Events

  1. Android Studio: Verify custom events with parameters in Android Studio on particular user interaction as highlighted below for log-enabled Android App.

 

  1. Validate in Debug view: Firebase facilitates support to see the events capture properly with associated events parameters in Debug view in realtime. This helps to identify whether events are registered properly or not within Firebase and rectify the same.


Recording of Firebase Events in Reports



Concluding thoughts..

Firebase provides events-based reporting. Over the past few years, Firebase has been entertaining marketers in terms of new features used to identify your mobile app user next level. Let’s start leveraging the power of Firebase.

We are very excited to include many more features, usage, analyse, and how to interpret data proving Firebase Analytics in our upcoming blogs.

If you are facing any queries related to Firebase Analytics integration please drop a comment.