iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Analytics

Firebase Analytics (Google Analytics for Firebase) collects events and user properties with no infrastructure on your side. The data flows into BigQuery if you flip the linked-export switch, which is where serious analysis happens. The pricing model is event-volume-based, so naming events meaningfully and avoiding accidental high-cardinality parameters matters.

Log events, set user properties, link to BigQuery

EXAMPLE
// ===== Web (Firebase JS SDK v10) =====
import { initializeApp } from 'firebase/app';
import {
  getAnalytics, logEvent, setUserProperties, setUserId,
  setConsent, isSupported,
} from 'firebase/analytics';

const app = initializeApp({/* config */});
if (await isSupported()) {
  const analytics = getAnalytics(app);

  // 1) Wait for the users consent decision before collecting analytics
  setConsent({
    analytics_storage: 'denied',
    ad_storage: 'denied',
  });
  // After the user accepts the cookie banner:
  // setConsent({ analytics_storage: 'granted', ad_storage: 'denied' });

  // 2) User identity (only after sign-in, never on the anonymous home page)
  setUserId(analytics, 'u-12345');
  setUserProperties(analytics, {
    plan: 'pro',
    signup_source: 'organic',
  });

  // 3) Log standard events — they show up in the GA4 reporting UI
  logEvent(analytics, 'login', { method: 'google' });
  logEvent(analytics, 'view_item', { items: [{ item_id: 'sku-1', item_name: 'Wool jacket', price: 199 }] });

  // 4) Custom events — keep names snake_case and finite
  logEvent(analytics, 'wishlist_added', {
    item_id: 'sku-1',
    list_name: 'spring-2026',
    // ❌ Do NOT pass raw search queries here — high cardinality blows up storage.
  });
}

// ===== Android (Kotlin) =====
// val analytics = Firebase.analytics
// analytics.logEvent(FirebaseAnalytics.Event.LOGIN) {
//   param(FirebaseAnalytics.Param.METHOD, "google")
// }
// analytics.setUserProperty("plan", "pro")

// ===== Link to BigQuery (one-off, in the Firebase console) =====
// Project Settings -> Integrations -> BigQuery -> Link.
// Then write SQL against firebase_<project>.analytics_<id>.events_*
// SELECT event_name, count(*) AS n
// FROM `proj.analytics_123456789.events_*`
// WHERE _TABLE_SUFFIX BETWEEN '20260601' AND '20260611'
//   AND event_name = 'wishlist_added'
// GROUP BY 1 ORDER BY n DESC;

Why it matters

Treat the event name as a stable column header and the parameters as the columns values — keep the *names* finite (a few dozen) and let the *values* vary. The opposite (one event per product or per query string) creates millions of unique event names that turn the GA reporting UI into mush and make BigQuery queries expensive.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
import { getAnalytics, logEvent } from 'firebase/analytics';
const analytics = getAnalytics(app);
logEvent(analytics, 'purchase', { value: 9.99, currency: 'USD' });
Try it Yourself »

Discussion

Loading…