Firebase is back at Google I/O on May 10! Register now

使用 Google Analytics 獲取崩潰報告的指標

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

Google Analytics是 Firebase 的分析引擎。當您在應用中同時使用 Analytics 和 Crashlytics 時,您會獲得一些功能,這些功能可幫助您產生問題並以更細粒度的方式跟踪崩潰數據,例如未發生崩潰的用戶、在崩潰之前跟踪特定事件的麵包屑以及 BigQuery,其中您可以可視化應用的關鍵指標。

本指南介紹瞭如何將 Analytics 添加到已設置 Crashlytics 的應用程序(如果您尚未設置 Crashlytics,請將 Crashlytics 添加到您的應用程序)。

第 1 步:添加 Firebase 配置文件

  1. 將 Firebase Android 配置文件添加到您的應用程序:

    1. 打開您的項目設置。您的應用卡中,選擇您需要配置文件的應用程序包名稱。

    2. 單擊下載 google-services.json以獲取您的 Firebase Android 配置文件 ( google-services.json )。

    3. 將配置文件移動到應用程序的模塊(應用程序級)目錄中。

  2. 要在您的應用中啟用 Firebase 產品,請將google-services 插件添加到您的 Gradle 文件中。

    1. 在您的根級(項目級)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
          // ...
        }
      }
      
    2. 在您的模塊(應用級)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 添加到您的應用程序

  1. 在您的模塊(應用程序級)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.3')
    
        // 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.3')
    
        // 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'
    }
    

  2. 在活動頂部聲明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);

下一步