Google 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 user base, such as language preference or geographic location.
Before you begin
If you're creating a new project, add Firebase to your Android project and follow the steps to set up Google Analytics for your project during project creation.
If you have an existing project and did not configure Google Analytics, you can link Google Analytics to your project from the Integrations tab of your > Project settings.
Add the Analytics SDK to your app
Add the dependency for the Google Analytics Android library to your module (app-level) Gradle file (usually
com.google.firebase.analytics.FirebaseAnalyticsobject at the top of your activity:
private FirebaseAnalytics mFirebaseAnalytics;
private lateinit var firebaseAnalytics: FirebaseAnalytics
Initialize it in the
// Obtain the FirebaseAnalytics instance. mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
// Obtain the FirebaseAnalytics instance. firebaseAnalytics = FirebaseAnalytics.getInstance(this)
Start logging events
After you have created a
FirebaseAnalytics instance, you can begin to log
events with the
Certain events are recommended for all apps; others are recommended for specific business types or verticals. You should send suggested events along with their prescribed parameters, to ensure maximum available detail in your reports and to benefit from future features and integrations as they become available. This section demonstrates logging a pre-defined event, for more information on logging events, see Log events.
The following code logs a
SELECT_CONTENT event when
a user clicks on a specific element in your app.
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);
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)
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 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.