Sub topic 7
Respecting consent and privacy
This chapter covers how to make your GA4 setup respect visitor choices. It assumes you can read basic JavaScript, know how a page loads scripts, and have installed GA4 with the gtag.js snippet from the earlier chapter. It is a technical guide, not legal advice. Your obligations depend on where your visitors live and what data you collect, so check with someone qualified for your situation.
Why consent changes your tracking setup
Analytics tools store identifiers in the browser, usually in cookies, and send data about each visit to a vendor. Privacy laws and platform policies in many regions, including the EU and UK (GDPR and ePrivacy rules), several US states, and Brazil, require you to tell visitors what you collect. In some regions you must also get their permission before you collect it.
For social media tracking this matters because several things you have built are covered:
- GA4 cookies that identify a returning visitor.
- Advertising pixels from social platforms, which are covered in a later chapter.
- UTM values and click IDs that you store and attach to form submissions.
Consent is not only a banner. It is a state your pages hold, and your tags read that state before they store or send anything.
Key terms
- Consent management platform (CMP) is a tool that shows the banner, records the visitor's choices, and tells your tags what was chosen.
- Consent mode is Google's mechanism for telling its tags what the visitor has agreed to. Tags then adjust their behavior.
- Consent signal is one named setting in consent mode, such as
analytics_storage. - Personal data is any information that can identify a person, directly or in combination with other data. Names and emails count. So do online identifiers such as cookie IDs.
The four Google consent signals
Google consent mode v2 uses four signals. Each is either granted or denied.
| Signal | Controls |
|---|---|
| `analytics_storage` | Whether analytics cookies can be read or written |
| `ad_storage` | Whether advertising cookies can be read or written |
| `ad_user_data` | Whether user data can be sent to Google for advertising |
| `ad_personalization` | Whether data can be used for personalized advertising |
For a site that only measures visits and conversions, analytics_storage is the signal that matters most. If you also run Google Ads or share audiences, the other three come into play.
Setting defaults before anything else runs
The most common mistake is setting consent after the page has already sent data. The default must be declared before the config call. Otherwise the first page view goes out under whatever the tag assumes by default.
The code below sets every signal to denied, then grants analytics storage after the visitor accepts.
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied'
});
// Called by your consent banner after the visitor accepts
function onConsentAccepted() {
gtag('consent', 'update', {
analytics_storage: 'granted'
});
}Notice that the update grants only analytics_storage. A visitor who accepts "analytics" has not necessarily accepted advertising. Map each banner category to the signals it covers, and grant only those.
Where the code goes in the page
The order of statements decides what gets sent. A working head section looks like this.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
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'
});
gtag('js', new Date());
gtag('config', 'G-XXXXXXXXXX');
</script>The consent default line comes before config. The library file loads asynchronously, but the commands queue in dataLayer in the order you wrote them, so the default is applied first.
What denied consent does to data
With analytics_storage denied, GA4 does not write or read its analytics cookies. Behavior depends on setup:
- Basic implementation. You do not load the Google tag at all until the visitor consents. No data is sent before consent. Visits from people who decline are missing from your reports.
- Advanced implementation. The tag loads right away with denied defaults. It sends cookieless pings that carry the consent state but no persistent identifier. Google uses these pings for modeled data, which estimates the behavior of visitors who declined. Modeling needs enough traffic to work and is not available on every property.
Each approach has a cost. Basic is simpler to defend, but it creates gaps. Advanced recovers some signal, but you must make sure it matches what your privacy notice and your local rules allow. Decide this with whoever owns compliance, not by default.
The gap matters for social traffic in particular. Campaign visitors from a paid social ad often arrive in an in-app browser and see your banner for the first time. If they ignore it, their session may be unrecorded, so your social conversions look lower than the platform reports.
Preserving UTM values under consent
Earlier you stored UTM values in sessionStorage so a later form submission could carry them. Storage on the visitor's device can itself fall under consent rules, depending on your region and how the data is used. Treat this as a decision, not an assumption.
A defensible pattern is to capture the values only after consent is granted, or to keep them in memory for the current page. The sketch below wraps the earlier capture in a check against your own consent state.
function saveUtmIfAllowed(consentGranted) {
if (!consentGranted) return;
const params = new URLSearchParams(window.location.search);
const keys = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_content', 'utm_term'];
const utm = {};
keys.forEach(function (k) {
if (params.get(k)) utm[k] = params.get(k);
});
if (Object.keys(utm).length > 0) {
sessionStorage.setItem('utm', JSON.stringify(utm));
}
}Your banner calls saveUtmIfAllowed(true) when the visitor accepts. If the visitor accepts on a later page, the query string is gone and the values are lost. That is a real trade-off, and it is one reason the numbers never match exactly.
Remembering returning visitors' choices
A banner that appears on every page load is both annoying and a sign of broken setup. Your CMP stores the choice, usually in a first-party cookie, and replays it on each page. The replay must call gtag('consent', 'update', ...) early, before or soon after the config call, so returning visitors who accepted are tracked from the first page view.
Reducing the personal data you send
Consent is one control. Data minimization is another: do not collect what you do not need.
- Do not put personal data in events or URLs. An email address in a query string or a
methodparameter is sent to Google and may land in reports. Use IDs or categories instead. - Check your UTM values. Never put a person's name or email in
utm_content, for example in an email-to-social workflow. Tags are visible in the URL, logs, and referrer headers. - Review form fields. Send that a
sign_uphappened, not the answers the person typed. - Set data retention. GA4 lets you choose how long event-level data is kept. Pick the shortest period that still supports your reporting.
- Limit access. Give report viewers viewer roles, not admin roles.
GA4 does not store full IP addresses in reports, but your server logs and other tools may. Know which tools in your stack see them.
Consent for ad platform pixels
Advertising pixels from social networks set their own cookies and send data to their own servers. Google's consent mode does not automatically control them. Most platforms offer their own consent controls, and a tag manager can hold a pixel until the visitor consents to advertising. A later chapter covers pixels in detail. For now, remember the rule: every tag that stores identifiers or sends data to a third party needs a consent condition.
Common errors
- Defaults set after config. The first page view goes out before the denied state is applied. Move the
consent defaultcall aboveconfig. - Banner that only hides. The banner closes, but no
updatecall is made, so tags stay denied for accepted visitors, or tags stay granted for refusers. - One toggle for everything. Accepting "analytics" also grants
ad_storage. Map each category to its own signals. - Choice not remembered. The CMP saves the choice but never replays it on later pages, so returning visitors are measured as denied.
- Personal data in parameters. Email addresses end up in event parameters or UTM values.
- No way to withdraw. Visitors must be able to change their mind. Provide a link that reopens the banner and sends
deniedupdates.
Practical checklist
- Choose a CMP that supports Google consent mode and the regions you serve.
- Declare denied defaults before the
configcall. - Map each banner category to the signals it covers.
- Replay saved choices on every page.
- Gate storage of UTM values and any third-party pixels on consent.
- Remove personal data from URLs, events, and parameters.
- Set a retention period and review access roles.
- Document your decisions so you can explain them later.
Exercise
A paid social ad sends a visitor to your landing page. The page loads the GA4 snippet with gtag('config', ...) first and the gtag('consent', 'default', ...) call after it. The visitor has not yet seen the banner. Which problem can occur, and how do you fix it? Also name one reason social conversions may look lower in GA4 than in the ad platform even after the fix.
Show answer
The first page view can be sent before the denied defaults apply, so data is collected without the visitor's permission. Fix it by moving the consent default call above config. Conversions can still look lower in GA4 because visitors who ignore or decline the banner are not recorded (or are only modeled), while the ad platform counts the click or conversion under its own rules.
Exercise
Your consent banner has "Analytics" and "Advertising" toggles. A visitor turns on only "Analytics". Which consent signals should your update call grant?
Show answer
Only analytics_storage. ad_storage, ad_user_data, and ad_personalization stay denied because the visitor did not accept advertising.