background-shape

Firebase provides very powerful feature which is “Log Events”. The power of this feature is that you can check which feature of your app is used mostly by the users.

Let’s deep dive into the implementation (note that the complete implementation is in Java)

Add this library in your build.gradle file:

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

I had used firebase bom in my project so their is no need to give version to firebase-analytics library

Now let’s create a function which helps us to log the events

void logEvent(Context context, String eventName, Bundle bundle) {
        // Initialise FirebaseAnalytics
        FirebaseAnalytics firebaseAnalytics = FirebaseAnalytics.getInstance(context);
        // Log the event
        firebaseAnalytics.logEvent(eventName, bundle);
    }

Now you can use events in different scenerios, mine case is to log event whenever a user opens the app. Now you can call this function like this:

// Empty bundle to save strings for event
Bundle bundle = new Bundle();
bundle.putString("UserName", "Mohit Singh");
// You can put multiple strings in the bundle
logEvent(MainActivity.this, "AppOpen", bundle);

All the events will be shown in the Events section of your Firebase Console. It takes almost a day to show events their so to check if log events are working or not you can check the events in the “DebugView”.

To log events in the debug view, you have to run the below command in the terminal of your Android Studio (Events will show only after running the below command in debug view else nothing will show in the debug view):

adb shell setprop debug.firebase.analytics.app [package_name]

DebugView looks like this:

DebugView

When you click on any event you will find the strings you provide in the bundle

Thanks for reading 🙌

Follow me for more such articles 😃😃