Consent-Aware Analytics and Experiments on Next.js by Dayan Ramly RamadhanConsent-Aware Analytics and Experiments on Next.js by Dayan Ramly Ramadhan

Consent-Aware Analytics and Experiments on Next.js

Dayan Ramly Ramadhan

Dayan Ramly Ramadhan

Overview

Role: Frontend Developer. Stack: Next.js, React, Vercel, a consent management platform, Google Tag Manager, GA4, Meta Pixel, Optimizely.
For a consumer-facing Next.js site, marketing and product teams needed reliable analytics, ad tracking, and A/B testing, while respecting user consent choices. I implemented the frontend layer that connects consent, tags, and experimentation.

The Problem

Tracking scripts firing before consent creates compliance risk.
Blocking everything hurts data quality and experiment validity.
Multiple tools (analytics, ads pixel, experimentation) can conflict or double-count if loaded inconsistently.
Experiments can cause layout flicker or inconsistent experiences if the SDK loads late.

The Approach

1. Consent first

I integrated a consent management platform so that tags load only according to the user's choices, with categories mapped to analytics, advertising, and functional scripts.

2. Controlled tag loading

I routed tags through Google Tag Manager and gated them on consent state, including handling users who change their choice mid-session.

3. Clean event schema

I defined and implemented a consistent event structure for analytics and ad pixels, so the same user action produces the same data across tools.

4. Experiment integration

I integrated the experimentation platform into the Next.js app, handling audience targeting, activation, and tracking so variants load smoothly and results stay measurable.

5. Verification

I tested consent states (accepted, rejected, changed) and confirmed that tags, events, and experiments behave correctly in each case.

The Results

Tags and pixels fire only when consent allows.
A consistent event layer feeding analytics, ads, and experiments.
Experiments running on a production Next.js site with reliable tracking.

Key Takeaways

Treat consent as an architecture concern, not a banner added at the end.
A single event schema prevents mismatched numbers between tools.
Experimentation and consent must be designed together, or one will break the other.
Like this project

Posted Oct 8, 2026

Consent-gated GTM setup for GA4, Meta Pixel, and Optimizely on Next.js, with one shared event schema and every consent state tested.