Google Analytics is Firebase's analytics engine. When you use Analytics and Crashlytics together in your app, you get features that help you produce issues and keep track of crash data with more granularity, such as crash-free users, breadcrumbs that track specific events prior to a crash, and BigQuery, where you can visualize your app's key metrics.
This guide describes how to add Analytics to an app that has Crashlytics set up (if you haven't already, add Crashlytics to your app).
Step 1: Add a Firebase configuration file
Add the Firebase Android configuration file to your app:
Open your Project Settings. In the Your apps card, select the package name of the app for which you need a config file.
Click Download google-services.json to obtain your Firebase Android config file (
google-services.json
).- You can download your Firebase Android config file again at any time.
- Make sure the config file is not appended with additional
characters, like
(2)
.
Move your config file into the module (app-level) directory of your app.
To enable Firebase products in your app, add the google-services plugin to your Gradle files.
In your root-level (project-level) Gradle file (
<project>/build.gradle.kts
or<project>/build.gradle
), add rules to include the Google services Gradle plugin. Check that you have Google's Maven repository, as well.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 }
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.kts
or<project>/<app-module>/build.gradle
), apply the Google services Gradle plugin.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' // ... }
Step 2: Add the Analytics SDK to your app
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 library for Android. We recommend using the Firebase Android BoM to control library versioning.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' }
By using the Firebase Android BoM, your app will always use compatible versions of Firebase Android libraries.
(Alternative) Add Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM, you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we strongly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
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' }
Declare the
com.google.firebase.analytics.FirebaseAnalytics
object at the top of your activity:Kotlin+KTX
private lateinit var firebaseAnalytics: FirebaseAnalytics
Java
private FirebaseAnalytics mFirebaseAnalytics;
Initialize it in the
onCreate()
method:Kotlin+KTX
// Obtain the FirebaseAnalytics instance. firebaseAnalytics = Firebase.analytics
Java
// Obtain the FirebaseAnalytics instance. mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
Next steps
- Use the DebugView to verify your events.
- Explore your data in the Firebase console.
- Explore the guides on events and user properties.
- Learn how to export your data to BigQuery.