Use Google Analytics to get metrics for crash reports

Google Analytics is Firebase's analytics engine. When you use Analytics and Crashlytics together in your app, you get features that help you produce issues and keep track of crash data with more granularity, such as crash-free users, breadcrumbs that track specific events prior to a crash, and BigQuery, where you can visualize your app's key metrics. This guide describes how to add Analytics to an app that has Crashlytics set up (if you haven't already, add Crashlytics to your app).

Step 1: Add a Firebase configuration file

  1. Add the Firebase Android configuration file to your app:

    1. Open your Project Settings. In the Your apps card, select the package name of the app for which you need a config file.

    2. Click Download google-services.json to obtain your Firebase Android config file (google-services.json).

      • You can download your Firebase Android config file again at any time.
      • Make sure the config file is not appended with additional characters, like (2).
    3. Move your config file into the module (app-level) directory of your app.

  2. To enable Firebase products in your app, add the google-services plugin to your Gradle files.

    1. In your root-level (project-level) Gradle file (build.gradle), add rules to include the Google Services Gradle plugin. Check that you have Google's Maven repository, as well.

      buildscript {
      
        repositories {
          // Check that you have the following line (if not, add it):
          google()  // Google's Maven repository
        }
      
        dependencies {
          // ...
      
          // Add the following line:
          classpath 'com.google.gms:google-services:4.3.8'  // Google Services plugin
        }
      }
      
      allprojects {
        // ...
      
        repositories {
          // Check that you have the following line (if not, add it):
          google()  // Google's Maven repository
          // ...
        }
      }
      
    2. In your module (app-level) Gradle file (usually app/build.gradle), apply the Google Services Gradle plugin.

      apply plugin: 'com.android.application'
      // Add the following line:
      apply plugin: 'com.google.gms.google-services'  // Google Services plugin
      
      android {
        // ...
      }
      

Step 2: Add the Analytics SDK to your app

  1. Using the Firebase Android BoM, declare the dependency for the Analytics Android library in your module (app-level) Gradle file (usually app/build.gradle).

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:28.3.0')
    
        // Declare the dependency for the Analytics library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-analytics'
    }
    

    By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.

    (Alternative) Declare Firebase library dependencies without using the BoM

    If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.

    Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.

    dependencies {
        // Declare the dependency for the Analytics library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-analytics:19.0.0'
    }
    

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation platform('com.google.firebase:firebase-bom:28.3.0')
    
        // Declare the dependency for the Analytics library
        // When using the BoM, you don't specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-analytics-ktx'
    }
    

    By using the Firebase Android BoM, your app will always use compatible versions of the Firebase Android libraries.

    (Alternative) Declare Firebase library dependencies without using the BoM

    If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.

    Note that if you use multiple Firebase libraries in your app, we highly recommend using the BoM to manage library versions, which ensures that all versions are compatible.

    dependencies {
        // Declare the dependency for the Analytics library
        // When NOT using the BoM, you must specify versions in Firebase library dependencies
        implementation 'com.google.firebase:firebase-analytics-ktx:19.0.0'
    }
    

  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

Next steps