Skip to main content
Add Google Analytics 4 to track traffic, conversions, and user behavior on your site.
Hiveku already includes privacy-friendly visitor analytics — see Track Site Analytics. Use Google Analytics when you need GA-specific integrations (Google Ads conversions, advanced audiences, BigQuery export) or when your team is already on GA.

Get Your Measurement ID

Before installing, grab your GA4 Measurement ID from analytics.google.com:
  1. Admin > Property > Data streams
  2. Click your web stream
  3. Copy the Measurement ID — it looks like G-XXXXXXXXXX

Three Ways to Install

Ask the AI in your project:
The AI picks the right file for your stack (Next.js root layout, static site <head>, etc.), adds the snippet, and deploys when you confirm.

Using an Env Var

Whichever path you choose, it’s a good idea to read the Measurement ID from an environment variable rather than hardcoding it. This way, production uses one GA property and staging uses another (or none):
See Configure Environment Variables for how to set per-environment values. GA4 uses cookies to track users across sessions. Depending on your visitors’ location, you may need a consent banner before loading GA:
  • EU (GDPR) — explicit opt-in required
  • California (CCPA) — opt-out required, at minimum
  • Other regions — varies, check local law
Don’t load GA before the user consents. The easiest path is to enable the cookie consent banner in Site Enhancements and gate the GA snippet on it — the AI can wire this up for you.
Hiveku’s consent banner exposes exactly these globals on your site:
There is no window.hiveku.consent. If you gate on that, the condition is permanently undefined — permanently falsy — and your analytics never initialises. Use window.__hivekuConsent (two leading underscores).

Gating the GA snippet

The consent runtime is loaded asynchronously, so reading the global once at page load usually runs before consent resolves and you get a false negative. Check the global once — the inline bootstrap sets it synchronously for visitors who already have a stored decision — then listen for the event to cover everyone else:
You only need this hand-written gate when you paste the GA snippet into your own code. If you register GA4 as a tracker in Site Enhancements instead, the consent banner injects the tag for you once the Analytics category is granted, and sets Google Consent Mode v2 defaults before any Google tag can run.

Google Tag Manager

If you prefer Tag Manager, the install flow is identical — swap the gtag.js snippet for your GTM container snippet:
Everything else in this guide applies the same way.

Verify It’s Working

1

Deploy your changes

Any of the three install paths above requires a deploy.
2

Open the Realtime report in GA4

analytics.google.com > your property > Reports > Realtime.
3

Visit your site in a new tab

Load any page. Within ~30 seconds, you should see your session appear in the Realtime report.
4

Check the script loaded

Open DevTools > Network > filter for gtag — you should see gtag/js?id=G-XXXXXXXXXX returning 200.

Troubleshooting

Check three things: (1) Measurement ID format is G- followed by 10 characters — older UA-prefixed IDs are discontinued; (2) the gtag/js script loads successfully in DevTools Network — if it 404s, the ID is wrong; (3) the snippet is in the rendered HTML, not just the source — a client-rendered React app may need special placement.
Check your env var — NEXT_PUBLIC_GA_ID has to be set in the production environment, not just locally. Vars without the NEXT_PUBLIC_ prefix aren’t exposed to the browser.
Expected for a non-trivial slice of users. Mitigations include server-side Measurement Protocol for important events (conversions) and using Google Tag Manager’s server-side container. Don’t try to defeat ad blockers for general pageviews — you’ll erode user trust.
The GA snippet was added twice — commonly in both the root layout and a page template. Search your codebase for G- and remove duplicates.
GA4 processes data in batches — full reports can lag 24–48 hours after first install. Realtime is the best quick-check for “is it working.”

What’s Next?

Track Site Analytics

Use Hiveku’s built-in privacy-friendly analytics

Environment Variables

Store your Measurement ID per environment