Google Analytics是 Firebase 的分析引擎。当您在应用中同时使用 Analytics 和 Crashlytics 时,您会获得一些功能,这些功能可帮助您产生问题并以更细粒度的方式跟踪崩溃数据,例如未发生崩溃的用户、在崩溃之前跟踪特定事件的面包屑以及 BigQuery,其中您可以可视化应用的关键指标。
本指南介绍了如何将 Analytics 添加到已设置 Crashlytics 的应用程序(如果您尚未设置 Crashlytics,请将 Crashlytics 添加到您的应用程序)。
第 1 步:添加 Firebase 配置文件
将 Firebase Android 配置文件添加到您的应用程序:
打开您的项目设置。在您的应用卡中,选择您需要配置文件的应用程序包名称。
单击下载 google-services.json以获取您的 Firebase Android 配置文件 (
google-services.json
)。- 您可以随时再次下载您的Firebase Android 配置文件。
- 确保配置文件未附加其他字符,例如
(2)
。
将配置文件移动到应用程序的模块(应用程序级)目录中。
要在您的应用中启用 Firebase 产品,请将google-services 插件添加到您的 Gradle 文件中。
在您的根级(项目级)Gradle 文件 (
build.gradle
) 中,添加规则以包含 Google Services Gradle 插件。检查您是否也有 Google 的 Maven 存储库。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.15' // Google Services plugin } } allprojects { // ... repositories { // Check that you have the following line (if not, add it): google() // Google's Maven repository // ... } }
在您的模块(应用级)Gradle 文件(通常是
app/build.gradle
)中,应用 Google Services Gradle 插件。apply plugin: 'com.android.application' // Add the following line: apply plugin: 'com.google.gms.google-services' // Google Services plugin android { // ... }
第 2 步:将 Analytics SDK 添加到您的应用程序
在您的模块(应用程序级)Gradle 文件(通常为
<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:31.2.0') // 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.2.0' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:31.2.0') // 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.2.0' }
在活动顶部声明
com.google.firebase.analytics.FirebaseAnalytics
对象:Kotlin+KTX
private lateinit var firebaseAnalytics: FirebaseAnalytics
Java
private FirebaseAnalytics mFirebaseAnalytics;
在
onCreate()
方法中初始化它:Kotlin+KTX
// Obtain the FirebaseAnalytics instance. firebaseAnalytics = Firebase.analytics
Java
// Obtain the FirebaseAnalytics instance. mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
下一步
- 使用DebugView验证您的事件。
- 在Firebase 控制台中探索您的数据。
- 浏览有关事件和用户属性的指南。
- 了解如何将数据导出到BigQuery。