Skip to content
Writing

· 2 min read

Analytics that answer questions: GA4, events and Web Vitals in Next.js

Pageviews don't tell you much. How I set up Google Analytics in a Next.js app around real questions: clean events, campaign tracking, server-side conversions and Web Vitals.

Jose Rai

Senior software engineer

When I owned a product end to end, the questions that mattered weren't engineering questions. Where do our users come from? Which campaign actually converts? Where do people give up? Analytics is how a developer gets a seat in those conversations, but only if it's set up to answer questions instead of counting pageviews.

Start from the questions

Before adding a single tag, I write a short tracking plan: the five or six questions the business cares about, and the events needed to answer each one. Signup started and completed, checkout steps, the key feature used. Everything else is noise until someone asks for it.

Google Analytics in the App Router

Next.js ships an official component for GA4 in @next/third-parties. It loads the script after hydration, so it doesn't slow down the first paint:

tsx
// app/layout.tsx
import { GoogleAnalytics } from "@next/third-parties/google";

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>{children}</body>
      <GoogleAnalytics gaId={process.env.NEXT_PUBLIC_GA_ID!} />
    </html>
  );
}

Events with a naming convention

Event names are an API, and the analysts are its users. I use object_action names in snake case (signup_completed, plan_selected), a small fixed set of parameters, and never any personal data in them:

tsx
"use client";

import { sendGAEvent } from "@next/third-parties/google";

export function PlanButton({ plan }: { plan: "starter" | "pro" }) {
  return (
    <button onClick={() => sendGAEvent("event", "plan_selected", { plan, location: "pricing" })}>
      Choose {plan}
    </button>
  );
}

Campaigns and market moves

Tracking how marketing moves the numbers comes down to discipline more than tooling. Every link in an ad, email or post carries UTM parameters from one shared spreadsheet, so utm_source doesn't arrive in five spellings. Releases and campaigns get logged with dates, so when traffic jumps, the cause is a lookup, not a guess.

Count conversions on the server

Ad blockers and privacy settings drop a real share of client-side events, and the ones you lose are often the ones you care about most. So purchases and signups are also sent from the backend through the GA4 Measurement Protocol, keyed to the same client ID the browser uses. The browser tells you about the journey; the server is the source of truth for the outcome.

Web Vitals belong in the same place

Speed affects conversion, so I report Core Web Vitals into the same analytics as everything else. Next.js exposes them through useReportWebVitals:

tsx
"use client";

import { useReportWebVitals } from "next/web-vitals";

export function WebVitals() {
  useReportWebVitals((metric) => {
    window.gtag?.("event", metric.name, {
      value: Math.round(metric.name === "CLS" ? metric.value * 1000 : metric.value),
      event_label: metric.id, // unique to this page load
      non_interaction: true,
    });
  });
  return null;
}

Now a slow release and a drop in conversion show up on the same chart, and the argument for performance work makes itself.

Make it a habit

  • A weekly 20-minute review of one dashboard, with the same questions every time.
  • Explorations in GA4 for funnels and cohorts, and Looker Studio for the dashboard people actually open.
  • Consent mode configured from day one, and no personal data in any event, ever.

Good analytics doesn't come from more tags. It comes from a few well-named events tied to questions someone is actually asking.