firebase_analytics_feature_image

Firebase Analytics Integration in Android


This tutorial describes the usage of the Firebase Analytics service in Android. If you don’t know what is Firebase and how to setup in android studio, then please through this previous blog.

So lets starts with Analytics service:

To write your Firebase Analytics service, use the FirebaseAnalytics API. The instructions in this page assume that you have completed the steps for adding Firebase to your Android project.

Why Firebase Analytics?

Firebase Analytics collects usage and behavior data for your app. The SDK logs two primary types of information:

  • Events: What is happening in your app, such as user actions, system events, or errors.
  • User properties: Attributes you define to describe segments of your userbase, such as language preference or geographic location.

Automatically Collected Events

Automatically collected events are triggered by basic interactions with your app. As long as you use the Firebase SDK, you don’t need to write any additional code to collect these events.

Event name Triggered…
first_open the first time a user launches an app after installing or re-installing it.

This event is not triggered when a user downloads the app onto a device, but instead when he or she first uses it. To see raw download numbers, look in Google Play Developer Console or in iTunesConnect.

in_app_purchase when a user completes an in-app purchase that is processed by the App Store on iTunes or Google Play. The product ID, product name, currency, and quantity are passed as parameters.

This event is triggered only by versions of your app that include the Firebase SDK. Also, subscription revenue, paid app-purchase revenue, and refunds are not automatically tracked, and so your reported revenue may differ from the values you see in the Google Play Developer Console.

user_engagement periodically, while the app is in the foreground.
session_start when a user engages the app for more than the minimum session duration.
app_update when the app is updated to a new version and launched again. The previous app version id is passed as a parameter.

app_update varies from Daily upgrades by device, which is reported by Google Play Developer Console whether or not the app is launched again after update.

app_remove when an application package is removed or “uninstalled” from an Android device.

This event is different from the Daily uninstalls by device and Daily uninstalls by usermetrics, which are both reported by Google Play Developer Console. The app_removeevent counts the removal of application packages, regardless of the installation source, and the count changes depending on the date range you are using for the report. TheDaily uninstalls by device and Daily uninstalls by user metrics count the removal of application packages only when they were installed from Google Play, and are reported on a daily basis.

os_update when the device operating system is updated to a new version. The previous operating system version id is passed as a parameter.
app_clear_data when the user resets/clears the app data, removing all settings and sign-in data.
app_exception when the app crashes or throws an exception.
notification_foreground when a notification sent by Firebase Notifications is received while the app is in the foreground.
notification_receive when a notification sent by Firebase Notifications is received by a device when the app is in the background. Android apps only.
notification_open when a user opens a notification sent by Firebase Notifications.
notification_dismiss when a user dismisses a notification sent by Firebase Notifications. Android apps only.
dynamic_link_first_open when a user opens the app for the first time via a dynamic link.
dynamic_link_app_open when a user opens the app via a dynamic link.
dynamic_link_app_update when the app is updated to a new version via a dynamic link. Android apps only.

 

Automatically Collected User Properties

Firebase automatically collects user properties too such as App version, Device model, Gender, Age, Interests,OS version, New/Established. You don’t need to write any additional code to collect automatic user properties.

Set up Firebase and the Analytics SDK

In Android Studio, add the Analytics dependency to your app-level build.gradle file:

Permissions

We need request for internet permission.

Get Firebase Instance

Obtain the FirebaseAnalytics instance.

Enabled Analytics Collection

You can also sets whether analytics collection is enabled for this app on this device using setAnalyticsCollectionEnabled(boolean enabled) method.

Analytics Session Management

You can also handle analytics session for this app like after how long a session should a starts and stops on inactive using below methods.

Log Events

Analytics SDK defines a number of suggested events that are common among different types of apps, including retail and ecommerce, travel, and gaming apps.

You can find implementation details for suggested event types in the following locations:

The following example demonstrates how to log a suggested SELECT_CONTENT Event:

Custom Events

You can log your own custom events as shown below.

Set user properties

User properties are attributes you define to describe segments of your userbase, such as language preference or geographic location.

Analytics automatically logs some user properties; you don’t need to add any code to enable them. If your app needs to collect additional data, you can set up to 25 different Analytics User Properties in your app.

Set a user property as follows:

  1. Register the property in the Analytics page of the Firebase console.
  2. Add code to set an Analytics User Property with the setUserProperty() method.

The following example adds a hypothetical favorite food property, which assigns the value in mFavoriteFood to the active user:

Note that user property names are case-sensitive and that setting two user properties whose names differ only in case results in two distinct user properties being logged.

View Events in the Dashboard

You can access this data in the Firebase console as follows:

  1. In the Firebase console, open your project.
  2. Select Analytics from the menu to view the Analytics reporting dashboard.

The Events tab shows the event reports that are automatically created for each distinct type of event logged by your app.

firebase_analytics_console_events

Sample Code:

Conclusion

I hope you will like the article and it will definitely help you to make your apps more productive. In case of any confusion or query, feel free to ask. And if you found this tutorial to be useful, Share with others.


About Durga Chiranjeevi

I’m a normal guy, engineer by education who is passionate about Programming and Internet. An android enthusiast, now moving for cross platforms and game development.