Adding Firebase Custom Events for Analytics Conversion (original) (raw)

Last Updated : 23 Jul, 2025

Tracking **user interactions and understanding their journey within our app is important for driving **engagement and optimizing our apps performance. **Firebase Analytics provides a robust framework for logging **custom events, allowing us to measure key actions that contribute to conversions. In this article, we will explore how to add Firebase custom events for analytics conversion in detail.

What are Firebase Custom Events?

Why Use Custom Events?

Setting Up Firebase Analytics

Before understanding the custom events, we need to set up Firebase Analytics in our project:

St**ep 1: Create a Firebase Project

Step 2: Register Your App with Firebase

Step 3: Integrate Firebase SDK

**For Android

**Add Firebase SDK: Open your build.gradle file and add the Firebase SDK:

// Project-level build.gradle
buildscript {
dependencies {
classpath 'com.google.gms:google-services:4.3.3'
}
}

// App-level build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

dependencies {
implementation 'com.google.firebase:firebase-analytics:17.4.3'
}

**Add Configuration File: Place the google-services.json file in the app/ directory of your project.

**For iOS

**Add Firebase SDK: Add the Firebase SDK to your Podfile:

Podfile

platform :ios, '10.0'

target 'YourApp' do
use_frameworks!
pod 'Firebase/Analytics'
end

**Install Pods: Run pod install in your project directory.

Add Configuration File: Add the GoogleService-Info.plist file to your Xcode project.

**For Web:

**Install Firebase: Use npm to install Firebase:

npm install firebase

**Initialize Firebase: Initialize Firebase in your app:

import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";

// Your Firebase config object
const firebaseConfig = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR_PROJECT_ID.firebaseapp.com",
projectId: "YOUR_PROJECT_ID",
storageBucket: "YOUR_PROJECT_ID.appspot.com",
messagingSenderId: "YOUR_SENDER_ID",
appId: "YOUR_APP_ID",
measurementId: "YOUR_MEASUREMENT_ID"
};

// Initialize Firebase
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);

Adding Firebase Custom Events

Step 1: Define Your Custom Events

Start by identifying the key actions you want to track in your app. Examples of custom events include:

Step 2: Log Custom Events

Use the Firebase Analytics SDK to log these events. Here are examples for different platforms:

**For Android:

import com.google.firebase.analytics.FirebaseAnalytics;

// Initialize Firebase Analytics
FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);

// Log a custom event
Bundle params = new Bundle();
params.putString("button_name", "subscribe");
mFirebaseAnalytics.logEvent("button_click", params);

**For iOS:

import Firebase

// Initialize Firebase Analytics
let analytics = Analytics.self

// Log a custom event
analytics.logEvent("button_click", parameters: [
"button_name": "subscribe" as NSObject
])

**For Web:

import { logEvent } from "firebase/analytics";

// Log a custom event
logEvent(analytics, 'button_click', {
button_name: 'subscribe'
});

Example Custom Events

Example 1: Tracking Button Clicks

Log an event when a user clicks a specific button in your app.

**For Android:

Bundle params = new Bundle();
params.putString("button_name", "subscribe");
mFirebaseAnalytics.logEvent("button_click", params);

**For iOS:

analytics.logEvent("button_click", parameters: [
"button_name": "subscribe" as NSObject
])

**For Web:

logEvent(analytics, 'button_click', {
button_name: 'subscribe'
});

Example 2: Tracking User Sign-Ups

Log an event when a user signs up for your app.

**For Android:

Bundle params = new Bundle();
params.putString("method", "email");
mFirebaseAnalytics.logEvent("sign_up", params);

**For iOS:

analytics.logEvent("sign_up", parameters: [
"method": "email" as NSObject
])

**For Web:

logEvent(analytics, 'sign_up', {
method: 'email'
});

Example 3: Tracking In-App Purchases

Log an event when a user makes an in-app purchase.

**For Android:

Bundle params = new Bundle();
params.putString("transaction_id", "T12345");
params.putDouble("value", 9.99);
params.putString("currency", "USD");
params.putParcelableArray("items", new Parcelable[] {
new Item("P123", "Product Name", "Category")
});
mFirebaseAnalytics.logEvent("purchase", params);

**For iOS:

analytics.logEvent("purchase", parameters: [
"transaction_id": "T12345" as NSObject,
"value": 9.99 as NSObject,
"currency": "USD" as NSObject,
"items": [
["item_id": "P123", "item_name": "Product Name", "item_category": "Category"] as NSObject
]
])

**For Web:

logEvent(analytics, 'purchase', {
transaction_id: 'T12345',
value: 9.99,
currency: 'USD',
items: [
{ item_id: 'P123', item_name: 'Product Name', item_category: 'Category' }
]
});

Example 4: Tracking Feature Engagement

Log an event when a user engages with a specific feature in your app.

**For Android:

Bundle params = new Bundle();
params.putString("feature_name", "chat");
mFirebaseAnalytics.logEvent("feature_engagement", params);

**For iOS:

analytics.logEvent("feature_engagement", parameters: [
"feature_name": "chat" as NSObject
])

**For Web:

logEvent(analytics, 'feature_engagement', {
feature_name: 'chat'
});

Analyzing Custom Event Data

Using Firebase Console

Creating Custom Reports

Firebase lets us to make custom reports to dig deeper into specific metrics.

Example Outputs

Best Practices for Tracking Custom Events

Conclusion

Adding Firebase custom events for analytics conversion is a powerful way to track specific user interactions and measure key actions that contribute to your app’s success. By setting up custom events, logging relevant interactions, and analyzing the data, you can gain valuable insights into user behavior and improve your app.