Prompt
How do I block analytics tags until someone opts in?
Latest observation
Oct 29, 2026
OpenAI APIWeb search: off
To block analytics tags until a visitor opts in, you generally want to prevent the tag from loading at all until consent is granted.
Best practice
-
Default to blocked
- Do not load analytics scripts on initial page load.
- Mark analytics consent as
deniedby default.
-
Only load after opt-in
- When the user clicks “Accept analytics” or similar, then:
- update consent state
- inject/load the analytics tag
- initialize tracking
- When the user clicks “Accept analytics” or similar, then:
-
Use a consent manager if possible
- If you use Google Tag Manager, a CMP, or a consent platform, configure analytics as a consent-controlled tag category.
Common implementation patterns
If you use Google Analytics / gtag.js
Set consent to denied before loading tags:
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
'analytics_storage': 'denied'
});
</script>
Then, after the user opts in:
<script>
gtag('consent', 'update', {
'analytics_storage': 'granted'
});
</script>
If you use Google Tag Manager
- Configure Consent Mode.
- Set analytics tags to require analytics consent.
- Ensure the default consent state is denied.
- Fire the tag only after consent is granted.
If you load tags manually
Don’t include the analytics <script> in the HTML initially. Instead, inject it only after consent:
function loadAnalytics() {
const s = document.createElement('script');
s.src = 'https://example.com/analytics.js';
s.async = true;
document.head.appendChild(s);
}
Call loadAnalytics() only after opt-in.
Also remember
- Store the consent choice so you don’t keep asking every page.
- Separate analytics from essential cookies: only essential cookies should load before consent.
- Respect regional laws: in many places, analytics requires opt-in before any non-essential tracking.
If you tell me what platform you’re using (GTM, GA4, Adobe, Shopify, WordPress, etc.), I can give you the exact setup.