Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Firebase Analytics is a powerful tool for understanding your app's usage and user engagement. Here's a step-by-step guide on how to integrate Firebase Analytics into your Android app using Android Studio:
Set up a Firebase project:
Add your Android app to the Firebase project:
google-services.json
file when prompted and place it in your app's root directory (app/
).Add Firebase SDK to your app:
In your root-level build.gradle
file, make sure you have Google's Maven repository:
buildscript { repositories { // ... google() // This should be already added for most Android Studio projects. } // ... }
Add the classpath for the Google services plugin:
buildscript { dependencies { // ... classpath 'com.google.gms:google-services:4.3.8' // Check for the latest version } }
In your app-level build.gradle
file, apply the plugin at the bottom of the file:
apply plugin: 'com.google.gms.google-services'
Add the Firebase Analytics dependency:
dependencies { // ... implementation 'com.google.firebase:firebase-analytics:19.0.1' // Check for the latest version }
Initialize Firebase in your app:
Typically, Firebase initializes itself automatically in your app. However, if you ever need to initialize it manually, you can do so by adding the following code in your Application
class:
import com.google.firebase.FirebaseApp; public class YourApplicationClass extends Application { @Override public void onCreate() { super.onCreate(); FirebaseApp.initializeApp(this); } }
Use Firebase Analytics:
To log events, you can get an instance of FirebaseAnalytics
and use its methods:
import com.google.firebase.analytics.FirebaseAnalytics; // ... FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this); // To log an event Bundle params = new Bundle(); params.putString("item_name", "example_item_name"); mFirebaseAnalytics.logEvent("event_name", params);
Run your app:
After making these changes, run your app. Firebase will automatically start collecting data, and you should see user engagement metrics and any logged events in the Firebase console after a few hours.
Integrating Firebase Analytics in Android Studio:
Description: Integrate Firebase Analytics into your Android Studio project to track user interactions and events.
Code:
// Add the Firebase Analytics dependency in your app-level build.gradle file implementation 'com.google.firebase:firebase-analytics:20.0.2'
// Initialize Firebase Analytics in your Application class or MainActivity FirebaseApp.initializeApp(this);
Logging events and user properties with Firebase Analytics:
// Log a custom event Bundle params = new Bundle(); params.putString("category", "button_click"); params.putString("label", "login_button"); firebaseAnalytics.logEvent("button_click", params); // Set user properties firebaseAnalytics.setUserProperty("preferred_language", "en");
Implementing conversion tracking with Firebase Analytics:
// Log conversion event firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, params);
Handling screen views and navigation events in Firebase Analytics:
// Log screen view event Bundle screenParams = new Bundle(); screenParams.putString(FirebaseAnalytics.Param.SCREEN_NAME, "MainActivity"); screenParams.putString(FirebaseAnalytics.Param.SCREEN_CLASS, "MainActivity"); firebaseAnalytics.logEvent(FirebaseAnalytics.Event.SCREEN_VIEW, screenParams);