Google アナリティクスは Firebase の分析エンジンです。アプリ内でアナリティクスと Crashlytics を組み合わせて使用すると、問題を生成してクラッシュ データを細かくトラッキングするための機能を利用できます。たとえば、クラッシュが発生していないユーザーをトラッキングしたり、クラッシュの前の特定のイベントを追跡するパンくずリストや BigQuery を利用したりすることができ、アプリの主要な指標を可視化できます。
このガイドでは、Crashlytics が設定されているアプリにアナリティクスを追加する方法について説明します(アプリに Crashlytics をまだ追加していない場合は追加してください)。
ステップ 1: Firebase 構成ファイルを追加する
Firebase Android 構成ファイルをアプリに追加します。
[プロジェクトの設定] を開きます。[マイアプリ] カードで、構成ファイルが必要なアプリのパッケージ名をリストから選択します。
[google-services.json をダウンロード] をクリックして、Firebase Android 構成ファイル(
google-services.json
)を取得します。- Firebase Android 構成ファイルはいつでも再ダウンロードできます。
- 構成ファイルに
(2)
のような文字が追加されていないことを確認してください。
構成ファイルをアプリのモジュール(アプリレベル)ディレクトリに移動します。
アプリで Firebase プロダクトを有効にするには、Gradle ファイルに google-services プラグインを追加します。
ルートレベル(プロジェクト レベル)の 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 }
モジュール(アプリレベル)の 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 を追加する
モジュール(アプリレベル)の Gradle ファイル(通常は
<project>/<app-module>/build.gradle.kts
または<project>/<app-module>/build.gradle
)に、アナリティクス 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 部品構成表を使用すると、アプリは常に互換性のあるバージョンの 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 部品構成表を使用すると、アプリは常に互換性のあるバージョンの 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' }
アクティビティの最上位で
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 にエクスポートする方法を学習する。