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 (
build.gradle
), add rules to include the Google Services Gradle plugin. Check that you have Google's Maven repository, as well.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 // ... } }
In your module (app-level) Gradle file (usually
app/build.gradle
), apply the Google Services Gradle plugin.apply plugin: 'com.android.application' // Add the following line: apply plugin: 'com.google.gms.google-services' // Google Services plugin android { // ... }
Step 2: Add the Analytics SDK to your app
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle
), add the dependency for the Analytics Android library. We recommend using the Firebase Android BoM to control library versioning.Kotlin+KTX
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:32.1.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-ktx' }
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-ktx:21.3.0' }
Java
dependencies { // Import the BoM for the Firebase platform implementation platform('com.google.firebase:firebase-bom:32.1.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.3.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.