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:
Add Google Analytics GA4 tracking with ID G-XXXXXXXXXX to my site. 
Put the snippet in the root layout so it's on every page.
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):
<script async src="https://www.googletagmanager.com/gtag/js?id={{ NEXT_PUBLIC_GA_ID }}"></script>
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.
Example consent gate with the Hiveku consent helper:
<script>
  // Only load GA if the user has consented to analytics cookies
  if (window.hiveku?.consent?.analytics) {
    const gaScript = document.createElement('script');
    gaScript.async = true;
    gaScript.src = 'https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX';
    document.head.appendChild(gaScript);
    // ... configure gtag as usual
  }
</script>

Google Tag Manager

If you prefer Tag Manager, the install flow is identical — swap the gtag.js snippet for your GTM container snippet:
<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){ /* GTM snippet */ })(window,document,'script','dataLayer','GTM-XXXXXXX');</script>
<!-- End Google Tag Manager -->
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