/firebase-tutorials

How to track screen views in Firebase for web?

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.

Matt Graham, CEO of Rapid Developers

Book a call with an Expert

Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.

Book a free No-Code consultation

How to track screen views in Firebase for web?

 

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.

  • Go to the Firebase Console.
  • Click on "Add Project" or select an existing project.
  • Navigate to "Project Settings" and locate the "Your Apps" section.
  • Click "Add App" and choose "Web" for a web application.
  • Register your app, then you'll receive a Firebase configuration object.

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.

  • Identify each page or view within your web app you want to track.
  • Utilize the 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.

  • Return to the Firebase Console.
  • Navigate to the "Analytics" section.
  • Check the "Dashboard" or "DebugView" (found under Analytics) to see if screen views are being recorded.

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:

  • Ensure the Firebase configuration object is correctly implemented in your project.
  • Verify if logEvent is being called appropriately with screen_view in each desired location of your app.
  • Check the network requests in your browser’s developer tools for any issues when logging events.
  • Use the Firebase DebugView to track real-time events and assess if there are issues in setup.

 

By following these steps, you'll be able to track screen views effectively in your web app using Firebase.

Want to explore opportunities to work with us?

Connect with our team to unlock the full potential of no-code solutions with a no-commitment consultation!

Book a Free Consultation

Client trust and success are our top priorities

When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.

Rapid Dev was an exceptional project management organization and the best development collaborators I've had the pleasure of working with. They do complex work on extremely fast timelines and effectively manage the testing and pre-launch process to deliver the best possible product. I'm extremely impressed with their execution ability.

CPO, Praction - Arkady Sokolov

May 2, 2023

Working with Matt was comparable to having another co-founder on the team, but without the commitment or cost. He has a strategic mindset and willing to change the scope of the project in real time based on the needs of the client. A true strategic thought partner!

Co-Founder, Arc - Donald Muir

Dec 27, 2022

Rapid Dev are 10/10, excellent communicators - the best I've ever encountered in the tech dev space. They always go the extra mile, they genuinely care, they respond quickly, they're flexible, adaptable and their enthusiasm is amazing.

Co-CEO, Grantify - Mat Westergreen-Thorne

Oct 15, 2022

Rapid Dev is an excellent developer for no-code and low-code solutions.
We’ve had great success since launching the platform in November 2023. In a few months, we’ve gained over 1,000 new active users. We’ve also secured several dozen bookings on the platform and seen about 70% new user month-over-month growth since the launch.

Co-Founder, Church Real Estate Marketplace - Emmanuel Brown

May 1, 2024 

Matt’s dedication to executing our vision and his commitment to the project deadline were impressive. 
This was such a specific project, and Matt really delivered. We worked with a really fast turnaround, and he always delivered. The site was a perfect prop for us!

Production Manager, Media Production Company - Samantha Fekete

Sep 23, 2022