Sub topic 7
Respecting Consent and Privacy
Assume you know basic HTML, URL query strings, and JavaScript event listeners. You also need access to your site's consent banner and GA4 property. This chapter focuses on the browser-side decision that controls analytics and advertising storage.
Consent is permission to process a visitor's data for a stated purpose. Privacy tracking starts by separating purposes instead of treating all tracking as one switch:
- Necessary storage supports security, login sessions, or the consent banner itself.
- Analytics storage supports measuring visits and behavior.
- Advertising storage supports ad measurement, remarketing, and personalized ads.
- Ad user data and ad personalization control how advertising data is used.
Your legal requirements depend on the visitor's location, your business, and the data you collect. Confirm the design with your privacy or legal adviser. Technical settings cannot replace a valid notice and consent process.
Set a denied default
Load the Google tag library, set consent defaults, and only then configure analytics. The default must run before events can be sent.
<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('consent', 'default', {
ad_storage: 'denied',
analytics_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
wait_for_update: 500
});
gtag('config', 'G-XXXXXXXXXX');
</script>wait_for_update gives the consent banner time to restore a visitor's saved choice. Choose a delay that fits your banner. A long delay can reduce measurement quality, while a short delay can allow tags to run before the choice is known.
In this example, analytics and advertising storage start as denied. Consent Mode can still send limited, cookieless signals in some configurations. That behavior is different from blocking every request, so document it in your privacy review.
Update consent after a choice
The consent banner should call an update function after the visitor accepts or rejects each purpose. Do not grant all purposes when the visitor agrees to only analytics.
function onConsentAccepted() {
gtag('consent', 'update', {
analytics_storage: 'granted',
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied'
});
}
function onAdvertisingAccepted() {
gtag('consent', 'update', {
analytics_storage: 'granted',
ad_storage: 'granted',
ad_user_data: 'granted',
ad_personalization: 'granted'
});
}Store the visitor's choice through your consent management platform. On later page loads, restore that choice before the first analytics configuration. Provide a way to reopen the settings and withdraw consent. Withdrawal should stop future optional storage and tags; decide how your system handles existing cookies according to your policy.
Connect consent to other tags
GA4 consent settings do not automatically govern every script on your site. Meta Pixel, TikTok Pixel, LinkedIn Insight Tag, heat maps, and custom marketing scripts need their own consent checks or tag-manager consent requirements.
| Tag purpose | Required decision | Example behavior |
|---|---|---|
| Analytics | Analytics consent | Send GA4 measurement only according to the selected mode |
| Advertising | Advertising consent | Load an ad pixel and allow advertising cookies |
| Necessary operation | No optional consent | Keep the consent banner and security controls working |
Avoid placing email addresses, phone numbers, or raw form data in URLs or analytics event parameters. Use approved identifiers and follow your retention policy. Consent does not make sensitive data safe to expose.
Exercise
A visitor accepts analytics but rejects advertising. Which consent values should be granted?
Show answer
Grant analyticsstorage only. Keep adstorage, aduserdata, and ad_personalization denied unless the visitor separately accepts those purposes.