GA4 & GTM Snippet Builder
Generate a ready-to-paste Google Analytics 4 gtag.js or Google Tag Manager snippet — with Consent Mode v2, anonymise IP, debug mode, cookie settings and cross-domain linker built in.
Configuration
Tag type & ID
Pick GA4 (gtag.js) or Google Tag Manager, then paste your ID from admin.
Consent Mode v2
Required by Google for EU/UK/Swiss traffic since March 2024. Default all storage to "denied" and update via your CMP after user consents.
GA4 config options
Advanced options passed to gtag('config', ...). Leave defaults unless you know you need them.
Generated snippet
Paste before </head>
<!-- Google tag (gtag.js) — workspacein.com -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
// Consent Mode v2 — default to denied until user consents
gtag('consent', 'default', {
'ad_storage': 'denied',
'ad_user_data': 'denied',
'ad_personalization': 'denied',
'analytics_storage': 'denied',
'wait_for_update': 500
});
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX', {
'anonymize_ip': true
});
</script>- warningUsing the placeholder ID — replace it with your actual GA4 measurement ID before shipping.
How to Install Your GA4 / GTM Snippet
Four steps that separate a working install from one that silently drops events.
Paste into <head> — high in the document
The snippet should be as close to the opening <head> as possible — only after <meta charset> and basic viewport tags. Late placement delays tag firing and misses early events.
Add the body noscript (GTM only)
GTM needs both a script in <head> and a noscript iframe just inside <body>. The iframe captures sessions with JavaScript disabled — a small slice of users who'd otherwise be invisible.
Wire Consent Mode v2 to your CMP
The defaults in this snippet set all storage to "denied". Your consent management platform (OneTrust, Cookiebot, etc.) needs to fire gtag('consent', 'update', {...}) when the user consents.
Verify with Tag Assistant / DebugView
Turn on debug_mode and open DebugView in GA4 admin — you should see events in real time. For GTM, use Tag Assistant Preview mode. If events don't arrive within 30 seconds, the snippet isn't firing.
Snippet Options Reference
What each option does — and when you actually need it.
Consent Mode v2
Required for EU/UK/Swiss traffic since March 2024. Tells Google how to behave before user consents.
send_page_view: false
Stops the automatic page_view event on load. Useful for SPAs where you fire page_view manually on route change.
debug_mode: true
Routes events to DebugView instead of live reports. Always false in production — noisy reports otherwise.
anonymize_ip
GA4 anonymises IPs by default — this flag is informational only. Universal Analytics used to need it; GA4 doesn't.
cookie_expires
How long the _ga cookie lives. Default 2 years. Shorter values (e.g. 31536000 = 1 year) can be required by some privacy laws.
cookie_domain
"auto" lets GA detect. Override to e.g. ".example.com" for cross-subdomain tracking (blog.example.com ↔ www.example.com).
linker.domains
Preserves the client ID across multiple domains you own. Without it, a user jumping from a.com to b.com shows as two sessions.
Noscript iframe (GTM)
Captures users with JS disabled or blocked. Low volume but not zero — include it for complete coverage.
GA4 / GTM Mistakes That Break Analytics
The install and config errors we see in every audit.
Install errors
Consent breakage
Data quality
Privacy & compliance
GA4 / GTM Snippet FAQ
gtag.js or Google Tag Manager — which one?
GTM if you want marketing to own tracking without dev support. gtag.js if you have only Google Analytics and want the simplest setup. GTM adds ~50KB vs gtag's ~40KB — similar performance cost.
Do I need Consent Mode v2?
Yes, for any traffic from the EU, UK, or Switzerland. Since March 2024, Google requires Consent Mode v2 for ads personalisation and remarketing. Without it, Google Ads features stop working for European users.
Does anonymize_ip still matter in GA4?
No. GA4 anonymises IP addresses by default and never stores them — the old ga('set', 'anonymizeIp', true) flag is a no-op in GA4. We include the setting for informational clarity only.
Is my measurement ID sent anywhere?
No. The snippet is generated entirely in your browser. Nothing is uploaded or stored. Paste the snippet into your own site to start collecting data.
Want a Full GA4 & GTM Audit?
Our Australian growth team audits tag setup, event taxonomy, Consent Mode v2, data-quality filters — then fixes what's broken and builds what's missing.
- GA4 + GTM + CMP audit
- Event taxonomy cleanup
- No lock-in commitment
No long-term commitment. Cancel anytime. 100% satisfaction guaranteed.
