Firebase Analytics Setup
Firebase Analytics tracks how users interact with your app — which pages they visit, how long they stay, which buttons they click, and where they drop off. It gives you real data to make product decisions instead of guessing what users want.
Analytics as a Store Camera System
A retail store installs cameras to understand shopper behavior — which aisles get the most traffic, which products people pick up and put down, which checkout lanes cause abandonment. Firebase Analytics is your app's camera system. It watches user behavior and turns observations into charts and reports you can act on.
Enabling Analytics
Analytics is enabled during project creation if you chose to add Google Analytics. If you skipped it, enable it now: go to Project Settings > Integrations and click Link Google Analytics account. Select or create a Google Analytics property and save.
Installing the Analytics SDK
import { initializeApp } from "firebase/app";
import { getAnalytics } from "firebase/analytics";
const app = initializeApp(firebaseConfig);
export const analytics = getAnalytics(app);
The Analytics SDK automatically collects several events without any additional code:
page_view— every time a page loadssession_start— when a user begins a new sessionfirst_visit— the first time a user ever opens your appuser_engagement— when the app is in the foreground
Logging Events
Beyond automatic events, log custom events when users take meaningful actions:
import { logEvent } from "firebase/analytics";
import { analytics } from "./firebase";
// Log a button click
logEvent(analytics, "button_click", {
button_name: "subscribe",
page: "pricing"
});
// Log a purchase
logEvent(analytics, "purchase", {
currency: "INR",
value: 999,
items: [{ item_name: "Premium Plan", quantity: 1 }]
});
// Log a search
logEvent(analytics, "search", {
search_term: "firebase tutorial"
});
Standard Event Names
Firebase Analytics defines standard event names for common actions. Use them instead of inventing custom names — they populate special reports automatically in the Analytics dashboard:
login— user logged insign_up— new user registeredsearch— user searched for somethingshare— user shared contentselect_content— user selected an itemview_item— user viewed a product or content itemadd_to_cart— ecommerce: added item to cartpurchase— ecommerce: completed a purchase
Setting User Properties
User properties describe your users and allow segmentation in reports:
import { setUserProperties } from "firebase/analytics";
import { analytics } from "./firebase";
setUserProperties(analytics, {
plan: "premium",
preferred_language: "en",
account_age_days: "90"
});
User properties appear in the Analytics audience builder, allowing you to create segments like "all premium users" or "users who registered more than 30 days ago."
Setting the User ID
Link analytics data to your user records by setting the user ID after login:
import { setUserId } from "firebase/analytics";
import { analytics } from "./firebase";
// After user logs in
setUserId(analytics, auth.currentUser.uid);
Viewing Analytics Data
Go to the Firebase Console and click Analytics in the left sidebar. The dashboard shows:
- Active users in the last 30 minutes
- Events logged in the past 7 days
- User demographics and geography
- Top pages and screens
- Retention charts showing how many users return
Data takes up to 24 hours to appear in reports because Firebase processes analytics data in batches.
DebugView for Real-Time Testing
Enable debug mode to see events appear in the Analytics DebugView within seconds:
// Add to your app during development
import { setAnalyticsCollectionEnabled } from "firebase/analytics";
// Not a debug mode flag in web SDK — use the browser extension instead
Install the Google Analytics Debugger Chrome extension and enable it. Open the Firebase Console, go to Analytics > DebugView, and interact with your app. Each event appears on the DebugView timeline in real time.
Key Takeaway
Firebase Analytics automatically tracks page views and sessions without custom code. Use logEvent to track specific user actions and setUserProperties to describe user segments. Set the user ID after login to connect analytics data to your user records. Events appear in the Analytics dashboard within 24 hours. Use DebugView during development to verify events fire correctly in real time.
