開始使用 Google Analytics (分析)

本快速入門向您展示如何將 Google Analytics 添加到您的應用並開始記錄事件。

Google Analytics 收集您的應用程序的使用情況和行為數據。 SDK 記錄兩種主要類型的信息:

  • 事件:應用程序中發生的情況,例如用戶操作、系統事件或錯誤。
  • 用戶屬性:您定義的用於描述用戶群細分的屬性,例如語言偏好或地理位置。

分析自動記錄一些事件用戶屬性;您不需要添加任何代碼來啟用它們。

在你開始之前

如果您尚未將 Firebase 添加到您的 Android 項目,並確保在您的 Firebase 項目中啟用了 Google Analytics(分析):

  • 如果您要創建新的 Firebase 項目,請在項目創建工作流程期間啟用 Google Analytics。

  • 如果您使用的現有 Firebase 項目未啟用 Google Analytics(分析),請轉到集成選項卡 ​​>項目設置以啟用它。

當您在項目中啟用 Google Analytics 時,您的 Firebase 應用程序將鏈接到 Google Analytics 數據流。

將 Analytics SDK 添加到您的應用程序

  1. In your module (app-level) Gradle file (usually <project>/<app-module>/build.gradle.kts or <project>/<app-module>/build.gradle ), add the dependency for the 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);

開始記錄事件

創建FirebaseAnalytics實例後,您可以開始使用logEvent()方法記錄事件。

建議所有應用程序執行某些事件;其他建議用於特定業務類型或垂直領域。您應該發送建議的事件及其規定的參數,以確保報告中提供最大的可用詳細信息,並從未來可用的功能和集成中受益。本節演示記錄預定義事件,有關記錄事件的更多信息,請參閱記錄事件

當用戶單擊應用中的特定元素時,以下代碼會記錄SELECT_CONTENT事件。

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")
}

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);

您可以啟用詳細日誌記錄以監視 SDK 的事件記錄,以幫助驗證事件是否已正確記錄。這包括自動和手動記錄的事件。

您可以使用一系列adb命令啟用詳細日誌記錄:

adb shell setprop log.tag.FA VERBOSE
adb shell setprop log.tag.FA-SVC VERBOSE
adb logcat -v time -s FA FA-SVC

此命令會在 Android Studio logcat 中顯示您的事件,幫助您立即驗證事件是否正在發送。

下一步