Learn to track screen views in Firebase for your web app with our step-by-step guide. Set up Firebase, enable Analytics, log events, verify data, and debug seamlessly.

Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Set Up Firebase in Your Web App
To track screen views in Firebase for a web application, you first need to set up Firebase in your project.
Here's an example of a Firebase configuration snippet you might receive:
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"
};
Include this configuration in your web app to initialize Firebase:
import { initializeApp } from 'firebase/app';
import { getAnalytics } from "firebase/analytics";
const app = initializeApp(firebaseConfig);
const analytics = getAnalytics(app);
Step 2: Enable Google Analytics
Ensure that Google Analytics is enabled for your Firebase project. This is typically set up when you first add Analytics to Firebase. Navigate to your Firebase console, select your project, and ensure Analytics is set up in the "Analytics" section.
Step 3: Track Screen Views
You'll need to manually log screen views using Firebase's logEvent function. This function helps you send logging events, including screen_view events.
logEvent function to log screen views with the screen_view event. The event can be customized with parameters like screen_name and screen_class.Here's an example code to track screen views:
import { logEvent } from "firebase/analytics";
// Assume "analytics" is your Firebase Analytics instance
function trackScreenView(screenName, screenClass) {
logEvent(analytics, 'screen\_view', {
firebase\_screen: screenName,
firebase_screen_class: screenClass
});
}
For each view in your app, call trackScreenView with the appropriate screen name and class:
// Example usage
trackScreenView('HomePage', 'MainActivity');
trackScreenView('ProfilePage', 'UserDetails');
Step 4: Verify Analytics Data
After setting up screen view tracking, you should verify if data is being logged correctly.
You might need to wait a short amount of time for data to populate in the console.
Step 5: Debugging
If you do not see the expected screen view events in Firebase Analytics:
logEvent is being called appropriately with screen_view in each desired location of your app.
By following these steps, you'll be able to track screen views effectively in your web app using Firebase.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.