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

How to Add Firebase Analytics to Android App in Android Studio?

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:

  1. Set up a Firebase project:

    • Go to the Firebase Console.
    • Click on "Add Project" and follow the prompts to create a new Firebase project.
  2. Add your Android app to the Firebase project:

    • In the Firebase console, click on the Android icon to add an Android app to your project.
    • Enter your app's package name.
    • Download the google-services.json file when prompted and place it in your app's root directory (app/).
  3. 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
      }
      
  4. 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);
        }
    }
    
  5. 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);
    
  6. 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.

  1. 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);
      
  2. Logging events and user properties with Firebase Analytics:

    • Description: Log custom events and user properties to capture meaningful data for analysis.
    • Code:
      // 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");
      
  3. Implementing conversion tracking with Firebase Analytics:

    • Description: Track conversions or specific user actions that contribute to your app's goals or success.
    • Code:
      // Log conversion event
      firebaseAnalytics.logEvent(FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, params);
      
  4. Handling screen views and navigation events in Firebase Analytics:

    • Description: Log screen views and navigation events to understand how users navigate through your app.
    • Code:
      // 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);