![]() |
VOOZH | about |
Tracking conversions is essential for understanding how effectively our app meets its goals and drives user actions. Firebase provides robust tools for conversion tracking, enabling us to measure key events, optimize user journeys and improve our app’s performance.
In this article, We will learn about Conversion Tracking with Firebase by setting up conversion tracking in Firebase in detail.
Conversion tracking involves measuring specific actions that users take within our app that are valuable to our business. These actions known as conversions can include purchases, sign-ups or any other goal that indicates user engagement and success.
By tracking these conversions, we can:
// 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 our project.
For iOS
Add Firebase SDK: Add the Firebase SDK to our Podfile:
# Podfile
platform :ios, '10.0'
target 'YourApp' do
use_frameworks!
pod 'Firebase/Analytics'
end
Install Pods: Run pod install in our 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 firebaseInitialize 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);
This code initializes a Firebase app using the `initializeApp` function from the `firebase/app` module and initializes Firebase Analytics using the `getAnalytics` function from the `firebase/analytics` module. The `firebaseConfig` object contains the configuration settings for your Firebase project, including the API key, project ID, and other identifiers.
Identify the key actions you want to track as conversions. Common conversion events include:
Use the Firebase AnalyticsSDK 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 sign-up event
Bundle params = new Bundle();
params.putString("method", "email");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.SIGN_UP, params);
// Log a purchase event
Bundle purchaseParams = new Bundle();
purchaseParams.putString("transaction_id", "T12345");
purchaseParams.putDouble("value", 9.99);
purchaseParams.putString("currency", "USD");
mFirebaseAnalytics.logEvent(FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, purchaseParams);
This code snippet uses the Firebase Analytics library for Android to log events. It initializes Firebase Analytics and then logs two events: a sign-up event with the method of sign-up (email), and a purchase event with the transaction ID, value, and currency.
For iOS
import Firebase
// Initialize Firebase Analytics
let analytics = Analytics.self
// Log a sign-up event
analytics.logEvent(AnalyticsEventSignUp, parameters: [
AnalyticsParameterMethod: "email" as NSObject
])
// Log a purchase event
analytics.logEvent(AnalyticsEventEcommercePurchase, parameters: [
AnalyticsParameterTransactionID: "T12345" as NSObject,
AnalyticsParameterValue: 9.99 as NSObject,
AnalyticsParameterCurrency: "USD" as NSObject
])
The code snippet uses Firebase Analytics to log events. The first event logs a sign-up event with the method of sign-up (email), while the second event logs a purchase event with the transaction ID, value, and currency.
For Web
import { logEvent } from "firebase/analytics";
// Log a sign-up event
logEvent(analytics, 'sign_up', {
method: 'email'
});
// Log a purchase event
logEvent(analytics, 'purchase', {
transaction_id: 'T12345',
value: 9.99,
currency: 'USD'
});
The code snippet logs events using Firebase Analytics. The first event logs a sign-up event with the method of sign-up (email), while the second event logs a purchase event with the transaction ID, value, and currency.
Example Outputs
Conversion tracking with Firebase is a powerful way to measure and optimize key user actions within your app. By setting up and logging custom events, marking them as conversions, and analyzing the data through the Firebase console, you can gain valuable insights into user behavior and make data-driven decisions to enhance your app’s performance.