Google is committed to advancing racial equity for Black communities. See how.

Get started with Google Analytics

This quickstart shows you how to add Google Analytics to your app and begin logging events.

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.

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

Before you begin

If you haven't already, add Firebase to your Android project and make sure that Google Analytics is enabled in your Firebase project:

  • If you're creating a new Firebase project, enable Google Analytics during the project creation workflow.

  • If you have an existing Firebase project that doesn't have Google Analytics enabled, you can enable Google Analytics from the Integrations tab of your > Project settings.

When you enable Google Analytics in your project, your Firebase apps are linked to Google Analytics data streams.

Add the Analytics SDK to your app

  1. Add the dependency for the Google Analytics Android library to your module (app-level) Gradle file (usually app/build.gradle):

    Java

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

    Kotlin+KTX

    implementation 'com.google.firebase:firebase-analytics-ktx:17.4.4'
    
  2. Declare the com.google.firebase.analytics.FirebaseAnalytics object at the top of your activity:

    Java

    private FirebaseAnalytics mFirebaseAnalytics;

    Kotlin+KTX

    private lateinit var firebaseAnalytics: FirebaseAnalytics
  3. Initialize it in the onCreate() method:

    Java

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

    Kotlin+KTX

    // Obtain the FirebaseAnalytics instance.
    firebaseAnalytics = Firebase.analytics

Start logging events

After you have created a FirebaseAnalytics instance, you can begin to log events with the logEvent() method.

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.

Java

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+KTX

firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SELECT_ITEM) {
    param(FirebaseAnalytics.Param.ITEM_ID, id)
    param(FirebaseAnalytics.Param.ITEM_NAME, name)
    param(FirebaseAnalytics.Param.CONTENT_TYPE, "image")
}

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