Sub topic 6
Collecting and honoring consent
Consent management is the system that asks visitors for permission to track them, records the answer, and makes sure your tags and data flows obey it. This chapter assumes you know what a cookie is, can work with a tag manager, and understand the basic legal ideas from the previous chapter on GDPR, ePrivacy and US state laws.
What valid consent looks like
Under GDPR and ePrivacy, consent for non-essential tracking must be:
- Prior. Tags that set cookies or read identifiers must not fire before the visitor agrees.
- Specific. Analytics, advertising and personalization are separate purposes, so each gets its own choice.
- Informed. The visitor can see who the vendors are and what each purpose does.
- Freely given and easy to withdraw. Declining must be as easy as accepting, and changing your mind must be simple.
Many US state laws work differently. They often let you collect by default but require an opt-out, including honoring browser signals such as Global Privacy Control. Check which rules apply to the regions you serve.
How the pieces fit together
A consent management platform (CMP) is the tool that shows the banner, stores the choice and exposes it to other code. Your tag manager reads that choice and decides which tags may run.
Defaults first, updates second
Consent-aware tags need a safe starting state. Set everything to denied before any tag loads, then update when the visitor chooses. This is the pattern used by Google's consent mode.
// Set the defaults to denied before any tags load,
// then update them when the visitor makes a choice.
window.dataLayer = window.dataLayer || [];
function gtag() { dataLayer.push(arguments); }
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied'
});
// Called by your consent banner when the visitor accepts.
function onConsentGranted() {
gtag('consent', 'update', {
ad_storage: 'granted',
analytics_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted'
});
}If the default snippet loads after other tags, those tags may already have run with full permissions. Load order is the most common implementation bug.
Consent mode also lets tags send limited, cookieless signals when consent is denied. Platforms use these to model conversions, which a later chapter covers.
Honoring consent in first-party code
Your own code must obey the same rules. A first-party cookie is still a cookie, so setting an identifier without consent is a violation.
function setFirstPartyId(consented) {
if (!consented) return;
const id = crypto.randomUUID();
const maxAge = 60 * 60 * 24 * 90; // 90 days
document.cookie = `visitor_id=${id}; Max-Age=${maxAge}; Path=/; Secure; SameSite=Lax`;
}Browsers may also cap the lifetime of cookies set by script, so a 90-day request can end up shorter.
Common errors
| Error | Result | Fix |
|---|---|---|
| Tags fire before the banner is answered | Tracking without valid consent | Block tags until the consent state is known |
| One "accept all" button and a buried "reject" | Consent may be invalid | Give accept and reject equal prominence |
| Choice not stored or not re-checked | Visitors are re-asked or ignored | Store the choice with a timestamp and read it on every page |
| Consent not passed to server-side uploads | Data sent for people who declined | Attach a consent flag to each record and filter on it |
| No way to withdraw | Consent is hard to undo | Add a persistent settings link |
Exercise
Your consent banner works, but a regulator audit finds analytics cookies on the page before any choice was made. The default gtag('consent', 'default', ...) call exists in the code. What is the most likely cause, and how do you check it?
Show answer
The default call likely runs after the analytics tag loads, or the analytics tag is not wired to read consent state at all. Check the load order in the tag manager so the default fires first, and use the browser's network and cookie tools on a fresh visit with no stored choice to confirm that no analytics cookies appear before the visitor answers.