Google アナリティクスを使用してクラッシュ レポートの指標を取得する

Google アナリティクスは Firebase の分析エンジンです。アプリ内でアナリティクスと Crashlytics を組み合わせて使用すると、問題を生成してクラッシュ データを細かくトラッキングするための機能を利用できます。たとえば、クラッシュが発生していないユーザーをトラッキングしたり、クラッシュの前の特定のイベントを追跡するパンくずリストや BigQuery を利用したりすることができ、アプリの主要な指標を可視化できます。

このガイドでは、Crashlytics が設定されているアプリにアナリティクスを追加する方法について説明します(アプリに Crashlytics をまだ追加していない場合は追加してください)。

ステップ 1: Firebase 構成ファイルを追加する

  1. Firebase Android 構成ファイルをアプリに追加します。

    1. [プロジェクトの設定] を開きます。[マイアプリ] カードで、構成ファイルが必要なアプリのパッケージ名をリストから選択します。

    2. [google-services.json をダウンロード] をクリックして、Firebase Android 構成ファイル(google-services.json)を取得します。

      • Firebase Android 構成ファイルはいつでも再ダウンロードできます。
      • 構成ファイルに (2) のような文字が追加されていないことを確認してください。
    3. 構成ファイルをアプリのモジュール(アプリレベル)ディレクトリに移動します。

  2. アプリで Firebase プロダクトを有効にするには、Gradle ファイルに google-services プラグインを追加します。

    1. ルートレベル(プロジェクト レベル)の Gradle ファイル(<project>/build.gradle.kts または <project>/build.gradle)に、Google サービスの Gradle プラグインを含めるためのルールを追加します。Google の Maven リポジトリがあることも確認してください。

      Kotlin

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

      Groovy

      plugins {
         id 'com.android.application' version '7.3.0' apply false
         // ...
      
         // Add the dependency for the Google services Gradle plugin
         id 'com.google.gms.google-services' version '4.4.0' 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: アプリにアナリティクス SDK を追加する

  1. モジュール(アプリレベル)の Gradle ファイル(通常は <project>/<app-module>/build.gradle.kts または <project>/<app-module>/build.gradle)に、Android 用アナリティクス ライブラリの依存関係を追加します。ライブラリのバージョニングの制御には、Firebase Android 部品構成表(BoM)を使用することをおすすめします。

    dependencies {
        // Import the BoM for the Firebase platform
        implementation(platform("com.google.firebase:firebase-bom:32.6.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 部品構成表を使用すると、アプリは常に互換性のあるバージョンの 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.5.0'
    }
    
    Kotlin 固有のライブラリ モジュールをお探しの場合は、2023 年 10 月(Firebase BoM 32.5.0)以降、Kotlin と Java のどちらのデベロッパーもメイン ライブラリ モジュールを利用できるようになります(詳しくは、このイニシアチブに関するよくある質問をご覧ください)。

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

次のステップ