Consent-Gated Analytics on Cloudflare Workers: A Reproducible Engineering Walkthrough
A concrete look at consent-first GA4 integration, privacy-safe referral attribution, and one-page-view-per-navigation testing from the VIS Labs website.
By VIS Labs · Published
A first-party tag is not a consent policy
VIS Labs publishes a small static website for its software products and applied-AI collaborations. The stack is Next.js exported to Cloudflare Workers, with optional Google Analytics behind a user-controlled preference. During an October 2026 release, a browser check found an automatically installed Google tag before consent. This was a real release acceptance problem: using a first-party endpoint alone did not prevent early script execution.
The resulting correction placed optional tag startup in the application consent component and required checking Cloudflare-managed automatic installation separately. This article explains that released engineering pattern, not a legal-compliance certification or an assertion that all third-party scripts everywhere behave the same way.
Separate consent, page transitions, and tag transport
The released integration initializes the GA4 configuration with send_page_view disabled, then emits explicit page_view events when an allowed route transition occurs after consent. This avoids double counting caused by both automatic SDK configuration and application navigation observing the same transition.
The implementation's createMeasurement(send) state machine is intentionally testable without the Google SDK or network access. A route outside the fixed public path allowlist cannot emit a page event; repeated visits to the same route do not emit extra events. A withdrawal makes later page and click events unavailable until consent is granted again.
const sent = [];
const tracker = createMeasurement((name, fields) => sent.push([name, fields]));
tracker.page('/'); // false, no consent
tracker.consent(true);
tracker.page('/'); // true, first page view
tracker.page('/'); // false, duplicate route
tracker.consent(false);
tracker.page('/contact'); // false after withdrawal
// Exactly one page_view and no private URL parameters in sent.Preserve acquisition without preserving private URLs
Incoming browser referrers may contain individual search terms, email addresses, query strings, or unusual URL paths. The published attribution module accepts known public search-engine origins and a tightly allowlisted LinkedIn organic campaign taxonomy. It rejects paid-click markers and unknown manual campaign values rather than relabeling them as organic.
Only fixed public attribution labels survive into consenting analytics events. The collector deliberately excludes full private referrer paths, arbitrary UTM values and user-supplied search strings. This protects sensitive context, but it also means unattributed traffic must remain unattributed rather than being guessed into a source.
Test the negative behavior, not only a successful page view
The October release testing covered missing consent, saved rejection, Do Not Track and Global Privacy Control signals, withdrawal, duplicate navigation, constrained campaign labels, and distinct product-link intent events. The most useful regressions are negative tests: before permission there must be no optional Google tag request; after revocation there must be no new optional events; after a repeated route change there must not be an extra page_view.
Unit tests prove the deterministic state machine. An intercepted browser session shows what the configured SDK attempted to send. GA4 Realtime receipt of deliberately labeled QA events confirms ingestion for the tested path. These are three different layers of evidence, and none demonstrates a human audience, conversion, legal compliance, or sustained attribution accuracy.
What the October release did and did not establish
After the reviewed correction, production browser checks and GA4 Realtime independently observed labeled QA page_view, owner_alliance_click and portfolio_link_click events. The same evidence did not establish downstream partner leads, new customers, or that all pages were free from duplicate events under every possible browser condition.
Cloudflare Web Analytics/RUM and GA4 also have different visit and session definitions. Cloudflare can change sampling granularity for older periods, so those measurements must not be simply added to GA4 users or interpreted as unique people. Reporting reliability requires provenance and compatible collection windows, not just a working tag.
An engineering release checklist
Inventory every optional analytics installation point: application code, tag manager, hosted proxy, automatic provider injection, and edge tools. Verify browser behavior before consent, on rejection, after withdrawal, and after re-consent. Disable automatic page views when routing emits explicit events. Sanitize sources and campaign parameters before event creation. Ensure each intended action emits one event at most for a tested interaction. Verify actual provider ingestion with labeled QA data and keep those events out of customer-acquisition claims.
For software and AI product work, the same approach helps separate an implementation claim from a measured result: build a reproducible test, record the exact source and limitations, and avoid treating missing evidence as success. VIS Labs applies that principle across its product and collaboration work; individual products may use different analytics architectures.