Firebase Android: Firebase Analytics

Tram Ho

1. Config project

  • SDK logs two basic types of information:
  • Event : what happens in your app, such as actions, system events or errors.
  • User properties : properties you define in the app, such as language preference, location. In addition, Analytics also automatically logs some events and user properties , so you do not need to add code to enable them.

First, your project must have firebase setup first, if not, please refer here .

Add the Analytics SDK to the app:

  1. In app / build.gradle, add the following dependency:

  1. Create a com.google.firebase.analytics.FirebaseAnalytics object in activity:

  1. Initiate it in the onCreate () function:

This is the preparation step.

2. Log events

After you create a FirebaseAnalytics instance, you can start to log events with the logEvent () function. There are some event names only reserved for firebase auto-log, you must not set the same name as these event names. Here is an example of how to log event:

It is possible to observe the log in Android Studio debug log, can log auto events and custom events. You can enable verbose logging with the adb commands:

And how to observe on the console again, I present the following.

3. Set user properties and user id

You can set Analytics User Properties to describe app users, when you view reports, you can analyze user behaviors. To set a user property:

  1. Open firebase console, go to Analytics then register properties on the “User Properties” page. For more information, see Set and register a user property
  2. Write code to set up an Analytics User Property with the setUserProperty () method . For example :

  • After registering a user property, it may take hours to view the console. You go to Analytics, select User Properties to be able to access this data. The dashboard will display a list of user properties that you have defined in the app. This data is regularly refreshed during the day.

4. Debug events

  • DebugView will help you track the events that you log on to firebase easily and intuitively, from here you can also see the error code or not.

To enable debug mode on your Android device, execute the following command: $ adb shell setprop debug.firebase.analytics.app package_name

Disable debug mode: $ adb shell setprop debug.firebase.analytics.app .none.

When debug mode is enabled on your device, go to the DebugView section of Analytics on the console:

You choose the device you are using to track the log:

When performing operations to log, the event will be logged, for example as follows:

There are 3 columns here:

  • The left column is Minutes stream, showing the event times within the past 30 minutes.

  • The column in the middle of the Seconds stream is the column of events logged every 60 seconds.

With the Seconds stream column, by default you will see the events are logged in 60s, each event displayed at a specified time. You can click on an event and see the list of params that the event brings, for example:

  • The right column is the Top Event logged within 30 minutes, including the user property.

5. Track Screens

Google Analytics will monitor screens and will log information when there is a transition between screens.

It will default to monitor a screen number and automatically log that screen information, for example, the names of the UIViewController or Activity classes that are in focus. When there is a screen transition, Analytics logs the event screenview .

You can set the event screen as follows:

After the set is completed, observe Debug View and log it with the following information:

And it will keep this name until the transition to another screen. So please check carefully the other events that are logged according to the screen name.

You can also set ids for users by using:

Then the event is logged as user_id with the id you set.

Above is what I have learned and shared for everyone to use if anyone needs it. Have fun coding (F).

Refer

Firebase Analytics docs

Share the news now

Source : Viblo