Share

blog-header

App Tracking for Android with Firebase

By combining the app and web data in Google Analytics 4, app analysis has become more easily trackable. Firebase Analytics allows you to easily track your iOS or Android app with GA4. With numerous different tools available for mobile app tracking, being able to see both web and app data within the same property is advantageous for developing more accurate analyses and strategies.

Firebase doesn’t only serve as app tracking for Analytics; it also provides many features such as Cloud Storage, Hosting, Realtime Database, Authentication, Performance Monitoring, Test Lab, Google Analytics, A/B Testing, Remote Configuration, Dynamic Links, and more.

Below, we will try to explain in detail how to track an Android application with Firebase Analytics using an example we’ve created.

Primarily, this article focuses on how to track an Android application with Firebase Analytics, not on the content of the Android application itself. Therefore, only relevant points related to Android are mentioned.

You’ll need Android Studio and your project to meet certain requirements, which you can review here.

In the ‘build.gradle.kts’ file at the application level in Android Studio, we’ll use the application name specified as ‘applicationId’ when creating the Firebase project. The first step will be to make a note of this.

Creating a Firebase Project

You can create your Firebase project using two different methods:

  • GA4 -> Admin -> Data streams -> Add stream -> Android app
  • Firebase Console -> Add project or Create a project

We will continue our article by creating a new project through the Firebase Console.

Step 1 -> Create a new project, name it, and proceed to the next step. The project name will also be the name of the Google Cloud Platform project automatically generated for you.

Step 2 -> Proceed with the option “Enable Google Analytics for this project” active.

Step 3 -> In the Configure Google Analytics section, you can choose an existing Google Analytics account or create a new one.

To configure the general Firebase project to work in our application, click on the Android logo.

As you can see from the image below, it’s mandatory to enter the Android package name. We had noted down the application name of our Android project as ‘com.example.perfistapp’; now it’s time to use it. The application nickname is optional and will only be used as a stream name, so you can give it any desired name. After filling in the fields, we can proceed to the next step.

The panel will ask you to download the ‘google-services.json’ file and place it in your project. This file is required for the SDKs to send data to the Analytics server in subsequent steps. You can drag and drop the downloaded JSON file into the app module as shown below.

Once you complete this step, you’ll receive instructions for Firebase setup. You’ll be asked to add Firebase dependencies to your project and application-level Gradle files. Gradle is a build tool used by Android Studio applications, managing dependencies required for the application to run.

Integration of Firebase Analytics SDK into the Application

Add the following code provided in the instructions as a dependency to your project-level ‘build.gradle.kts’ file:

Next, in the application-level ‘build.gradle.kts’ file, add both the google-services plugin and the Firebase SDKs you want to use in your application:

Since we’re only using Analytics, we add “com.google.firebase:firebase-analytics”.

After including these dependencies and applying the plugins and necessary SDKs, we can click on the “Sync Now” link in the toolbar to locally install the new dependencies.

Up to this point, we’ve integrated the Analytics service of Firebase SDK into our application. At this stage, if you run the application, you’ll see that Firebase automatically logs events such as first_visit, session_start, and screen_view.

In the next steps, we’ll initialize Analytics in the application and begin tracking events stemming from user interactions within our application.

We add the FirebaseAnalytics class to our application and declare the FirebaseAnalytics object in our activity class.

import com.google.firebase.analytics.FirebaseAnalytics

private lateinit var firebaseAnalytics: FirebaseAnalytics

In the onCreate() method, we can initialize Firebase Analytics.

firebaseAnalytics = Firebase.analytics

After adding Firebase Analytics to our application, we complete the steps through the Firebase Console.

Now, our mobile application is ready to send data to Analytics. Before starting to send events, let’s check the connection of our application using Debugging methods.

Debugging Methods

We will debug using two different methods: Android Studio Logcat and the DebugView section in GA4.

  • Android Studio Logcat

We need to enable detailed logging with the following adb command sequences. These commands allow us to quickly verify our events by viewing them in Android Studio logcat.

  • Google Analytics 4 -> DebugView

By running the following code in the terminal, you can monitor events with DebugView in GA4. The ‘PACKAGE_NAME’ part corresponds to ‘com.example.perfistapp’ in our project.

adb shell setprop debug.firebase.analytics.app PACKAGE_NAME

Debugging mode continues until the following code is executed.

adb shell setprop debug.firebase.analytics.app .none.

We will inspect our event control via DebugView using the example below.

Setting Up Events

By default, Firebase collects more than 20 events. You can see the entire list here. Like on the web side, custom events and ecommerce events can also be created for applications.

The naming conventions for all ecommerce events are fixed, so we need to pay attention to some rules. GA4 reports are generated based on these rules. For more detailed information on naming conventions and parameter details, you can refer to the Google documentation. You can find examples for various programming languages. For example, in an application developed using the Android (Kotlin+KTX) programming language, you should use the following code structure for the product view (view_item) event.

val itemAyakkabi = Bundle().apply {
putString(FirebaseAnalytics.Param.ITEM_ID, "123456")
putString(FirebaseAnalytics.Param.ITEM_NAME, "xyz")
putString(FirebaseAnalytics.Param.ITEM_CATEGORY, "ayakkabı")
putString(FirebaseAnalytics.Param.ITEM_VARIANT, "beyaz")
putString(FirebaseAnalytics.Param.ITEM_BRAND, "Perfist")
putDouble(FirebaseAnalytics.Param.PRICE, 99.99)
}

firebaseAnalytics.logEvent(FirebaseAnalytics.Event.VIEW_ITEM) {
param(FirebaseAnalytics.Param.CURRENCY, "TRY")
param(FirebaseAnalytics.Param.VALUE, 99.99)
param(FirebaseAnalytics.Param.ITEMS, arrayOf(itemAyakkabi))
}

If you want to send custom events, you need to ensure that the relevant code is triggered along with the event name and parameters when the desired action occurs.

In the example below, we want to measure the number of users logging into the Perfist application.

Let’s assume that we have configured the following code to run when the Login process is completed.

firebaseAnalytics.logEvent(FirebaseAnalytics.Event.LOGIN) {
param(FirebaseAnalytics.Param.METHOD, "E-mail")
}

In the terminal section, we execute the relevant code that allows us to perform checks with GA4 DebugView.

Let’s run our application in debugging mode.

We should be able to see the custom event we created in the GA4 DebugView panel. We can see that along with the automatically collected “screen_view” and “user_engagement” events when the application is activated, the “login” event is also triggered. We can also see that the method parameter we set to view login methods is triggered as “Email”. 

I hope this article has been helpful. Feel free to reach out to us for any feedback or comments.”

 

Hello, I am Mehmet Akif ÇANDIR, after working as an engineer in the sector, I started working in the Web/App Analytics sector with the contribution of my curiosity and engineering knowledge. I am improving myself and providing service as a Web/App Analyst. I have been working as a Web/App Analyst at Perfist since July 2022.

Perfist Blog

Similar Articles

Other Articles