Skip to main content
AnalyticsLearner

Code Library

Production-correct snippets. De-identified. Module-mapped to the Advanced GTM & Server-Side Tracking course.

Module 3 · GA4 Base Config

GA4 base config through GTM (consent-gated)

Configure a GA4 tag in GTM that fires only when analytics_storage is granted. The correct implementation of GA4 with Consent Mode v2.

ga4gtmconsent-mode-v2
Module 5 · Consent Mode v2

Consent Mode v2 default-deny baseline

Inject a default-deny consent baseline before GTM loads. Covers all regions globally plus an explicit EEA/UK region list.

consent-mode-v2gtmprivacygdpr
Module 5 · Consent Mode v2

Consent Mode v2 update on banner consent

Fire gtag consent update when a visitor accepts or rejects cookies. Covers both the granted and denied paths.

consent-mode-v2gtmcmpprivacy
Module 6 · Server-Side GTM

GA4 Measurement Protocol: server-side event from a Next.js route handler

Send a GA4 event directly from a Next.js route handler using the Measurement Protocol. Sessions stitch correctly when client_id is read from the _ga cookie.

ga4measurement-protocolnextjsserver-side
Module 6 · Server-Side GTM

Self-hosted server-side GTM on a VPS with Docker + Caddy

Deploy the Google server-side GTM tagging container on a VPS using Docker and Caddy as the reverse proxy. The advanced reference for client work, not the architecture this site uses.

sgtmdockercaddyself-hostedvps
Module 7 · Enhanced Conversions

Enhanced Conversions: user-provided data + SHA-256 normalised hashing

Implement Google Ads Enhanced Conversions by hashing user-provided data (email, phone, name) with SHA-256 before sending to Google.

enhanced-conversionsgoogle-adssha256gtm
Module 8 · Meta CAPI

Meta CAPI and browser Pixel deduplication via shared event_id

Prevent double-counting Meta conversions when you run both the browser Pixel and CAPI. The event_id must be identical on both sides.

meta-capimeta-pixeldeduplicationevent_id
Module 8 · Meta CAPI

Meta CAPI: server-side conversion from a Next.js route handler

Send a Meta Conversions API event from a Next.js route handler. Includes SHA-256 hashing of user data and the event_id for browser-Pixel deduplication.

meta-capinextjsserver-sideconversions-api
Module 9 · Cross-Domain

Cross-domain linker configuration

Configure GA4 cross-domain measurement so sessions are not broken when visitors move between domains or subdomains.

ga4cross-domainattributiongtm
Module 10 · Ecommerce

GA4 ecommerce purchase dataLayer (generic)

The complete GA4 purchase event dataLayer push. Production-correct schema with all required and recommended parameters.

ga4ecommercedatalayerpurchase
Module 10 · Ecommerce

Shopify purchase dataLayer via Customer Events

Fire the GA4 purchase event from Shopify's Customer Events API (web pixels). Works with the new checkout extensibility without requiring checkout.liquid access.

shopifyga4ecommercecustomer-events