Prompt
How do I block analytics and ad tags until a user opts in?
Latest observation
To block analytics and ad tags until a user opts in, the main rule is:
Do not load or execute any non-essential third-party scripts before consent.
That means no analytics, ads, retargeting pixels, or similar trackers until the user has explicitly opted in.
Recommended approach
1) Categorize scripts by purpose
Separate your tags into:
- Strictly necessary: site security, session, cart, language preference
- Analytics: GA, Matomo, Mixpanel, etc.
- Advertising / marketing: Google Ads, Meta Pixel, TikTok, etc.
- Personalization / social embeds: widgets, embedded video, etc.
Only load the necessary ones by default.
2) Don’t include tracking scripts in the initial HTML
Instead of embedding them directly in the page <head>, keep them disabled until consent. Common ways:
- Server-side rendering / template logic: only render the tag after consent is stored
- Client-side tag loader: check consent first, then inject the script dynamically
- Consent management platform (CMP): use a CMP that blocks tags until category consent is granted
3) Store consent before loading tags
If the user opts in:
- Save the consent choice in a cookie/localStorage/server profile
- Load the relevant scripts afterward
- If consent is denied, keep them blocked
4) Use a tag manager in consent mode
If you use Google Tag Manager or similar, configure it so tags are blocked by default and only fire after consent.
For Google products specifically:
- Implement Consent Mode
- Default analytics/ads storage to denied
- Update consent state after the user chooses
5) Prevent “pre-consent” network requests
Make sure scripts are not just “loaded but inactive.” Some tags send requests as soon as they load.
You want to avoid:
<script src="https://...analytics...">in the initial page- inline snippets that auto-execute on page load
- image beacons or pixel URLs included before consent
6) Provide a clear consent UI
Your banner/modal should:
- Explain what categories you use
- Offer accept/reject or granular choices
- Let users change preferences later
- Record consent timestamp/version if needed for compliance
Simple implementation pattern
Example: only inject analytics after opt-in
<button id="accept-analytics">Accept analytics</button>
<script>
function loadAnalytics() {
const s = document.createElement('script');
s.src = 'https://example.com/analytics.js';
s.async = true;
document.head.appendChild(s);
}
document.getElementById('accept-analytics').addEventListener('click', () => {
localStorage.setItem('analytics_consent', 'yes');
loadAnalytics();
});
if (localStorage.getItem('analytics_consent') === 'yes') {
loadAnalytics();
}
</script>
If you use Google Tag Manager
Use one of these:
- Consent Mode v2
- A CMP integration that automatically blocks categories until consent
- GTM triggers that fire only when consent is granted
Important caveat
If you’re in a regulated environment (for example GDPR/UK GDPR/ePrivacy, or similar), “blocking” usually means:
- no cookies/storage for those tags before consent
- no transmission of personal data before consent, where required
- no loading scripts that may set cookies or send identifiers before approval
Best practice
The safest setup is:
- Default all marketing/analytics consent to denied
- Load only essential scripts on page load
- After opt-in, dynamically load tracking tags
- Persist and honor the user’s choice on future visits
If you want, I can give you:
- a plain HTML/JS example
- a Google Tag Manager setup
- a Next.js / React implementation
- or a server-side blocking approach