Get started with Google Analytics for Firebase on Android

Google Analytics for Firebase 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 user base, such as language preference or geographic location.

Analytics automatically logs some events and user properties; you don't need to add any code to enable them.

Prerequisites

Add Analytics to your app

Add the dependency for Google Analytics for Firebase to your app-level build.gradle file:

implementation 'com.google.firebase:firebase-core:16.0.6'

Declare the com.google.firebase.analytics.FirebaseAnalytics object at the top of your activity:

Java
Android

private FirebaseAnalytics mFirebaseAnalytics;

Kotlin
Android

private lateinit var firebaseAnalytics: FirebaseAnalytics

Then initialize it in the onCreate() method:

Java
Android

// Obtain the FirebaseAnalytics instance.
mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

Kotlin
Android

// Obtain the FirebaseAnalytics instance.
firebaseAnalytics = FirebaseAnalytics.getInstance(this)

Log events

After you have created a FirebaseAnalytics instance, you can use it to log either predefined or custom events with the logEvent() method. You can explore the predefined events and parameters in the FirebaseAnalytics.Event and FirebaseAnalytics.Param reference documentation.

The following code logs a SELECT_CONTENT event when a user clicks on a specific element in your app.

Java
Android

Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id);
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name);
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle);

Kotlin
Android

val bundle = Bundle()
bundle.putString(FirebaseAnalytics.Param.ITEM_ID, id)
bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, name)
bundle.putString(FirebaseAnalytics.Param.CONTENT_TYPE, "image")
firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_CONTENT, bundle)

Confirm Events

You can enable verbose logging to monitor logging of events by the SDK to help verify that events are being logged properly. This includes both automatically and manually logged events.

You can enable verbose logging with a series of adb commands:

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

This command displays your events in the Android Studio logcat, helping you immediately verify that events are being sent.

Next Steps

Gửi phản hồi về...

Bạn cần trợ giúp? Truy cập trang hỗ trợ của chúng tôi.