使用 Google Analytics(分析)获取崩溃报告的指标

Google Analytics(分析)是 Firebase 的分析引擎。如果您在自己的应用中将 Analytics 与 Crashlytics 结合使用,可以实现各种功能来帮助重现问题并更细致地跟踪崩溃数据,例如统计未遇到崩溃问题的用户数、通过面包屑导航跟踪崩溃发生之前的特定事件,还可以借助 BigQuery 直观呈现应用的关键指标。

本指南介绍如何将 Google Analytics(分析)添加到设置了 Crashlytics 的应用。如果您尚未将 Crashlytics 添加到您的应用,请先添加。

第 1 步:添加 Firebase 配置文件

  1. 将 Firebase Android 配置文件添加到您的应用:

    1. 打开您的项目设置。在您的应用卡片中,选择需要配置文件的应用的软件包名称。

    2. 点击下载 google-services.json,获取 Firebase Android 配置文件 (google-services.json)。

    3. 将配置文件移到应用的模块(应用级)目录中。

  2. 如需在 Android 应用中启用 Firebase 产品,请将 google-services 插件添加到 Gradle 文件中。

    1. 根级(项目级)Gradle 文件(<project>/build.gradle.kts<project>/build.gradle)中添加规则,纳入 Google 服务 Gradle 插件。此外,请确认您拥有 Google 的 Maven 制品库。

      Kotlin

      plugins {
         id("com.android.application") version "7.2.0" apply false
         // ...
      
         // Add the dependency for the Google services Gradle plugin
         id("com.google.gms.google-services") version "4.3.15" apply false
      }
      

      Groovy

      plugins {
         id 'com.android.application' version '7.2.0' apply false
         // ...
      
         // Add the dependency for the Google services Gradle plugin
         id 'com.google.gms.google-services' version '4.3.15' apply false
      }
      
    2. 在您的模块(应用级)Gradle 文件(通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)中,采用 Google 服务 Gradle 插件。

      Kotlin

      plugins {
         id("com.android.application")
      
         // Add the Google services Gradle plugin
         id("com.google.gms.google-services")
         // ...
      }
      

      Groovy

      plugins {
         id 'com.android.application'
      
         // Add the Google services Gradle plugin
         id 'com.google.gms.google-services'
         // ...
      }
      

第 2 步:将 Analytics SDK 添加到您的应用

  1. 在您的模块(应用级)Gradle 文件(通常是 <project>/<app-module>/build.gradle.kts<project>/<app-module>/build.gradle)中,添加 Analytics Android 库的依赖项。我们建议使用 Firebase Android BoM 来实现库版本控制。

    Kotlin+KTX

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // Add 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'
    }
    

    借助 Firebase Android BoM,可确保您的应用使用的始终是 Firebase Android 库的兼容版本。

    (替代方法) 在不使用 BoM 的情况下添加 Firebase 库依赖项

    如果您选择不使用 Firebase BoM,则必须在每个 Firebase 库的依赖项行中指定相应的库版本。

    请注意,如果您在应用中使用多个 Firebase 库,我们强烈建议您使用 BoM 来管理库版本,从而确保所有版本都兼容。

    dependencies {
        // Add 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:21.3.0'
    }
    

    Java

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.3.1"))
    
        // Add 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'
    }
    

    借助 Firebase Android BoM,可确保您的应用使用的始终是 Firebase Android 库的兼容版本。

    (替代方法) 在不使用 BoM 的情况下添加 Firebase 库依赖项

    如果您选择不使用 Firebase BoM,则必须在每个 Firebase 库的依赖项行中指定相应的库版本。

    请注意,如果您在应用中使用多个 Firebase 库,我们强烈建议您使用 BoM 来管理库版本,从而确保所有版本都兼容。

    dependencies {
        // Add 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:21.3.0'
    }
    

  2. 在您的 Activity 的顶部声明 com.google.firebase.analytics.FirebaseAnalytics 对象:

    Kotlin+KTX

    private lateinit var firebaseAnalytics: FirebaseAnalytics

    Java

    private FirebaseAnalytics mFirebaseAnalytics;
  3. onCreate() 方法中初始化该对象:

    Kotlin+KTX

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

    Java

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

后续步骤