Script Blocking
How Zest blocks tracking scripts until user consent
How It Works
Zest uses a sophisticated script blocking system that:
- Monitors the DOM using
MutationObserverto detect new scripts - Checks each script against blocking rules based on the configured mode
- Blocks matching scripts by changing their type to
text/plain - Queues blocked scripts for later execution
- Replays scripts when the user grants consent
Blocking Modes
Manual Mode
Only blocks scripts explicitly tagged with data-consent-category:
<!-- This will be blocked -->
<script data-consent-category="analytics" src="https://example.com/tracker.js"></script>
<!-- This will NOT be blocked -->
<script src="https://www.googletagmanager.com/gtag/js"></script>Safe Mode (Default)
Blocks manually tagged scripts plus known major trackers:
Blocked Analytics:
google-analytics.comanalytics.google.comgoogletagmanager.complausible.iocloudflareinsights.com
Blocked Marketing:
connect.facebook.netads.google.comgoogleads.g.doubleclick.netpagead2.googlesyndication.com
Strict Mode
Everything in Safe mode plus extended tracking services:
- Hotjar, Microsoft Clarity, Heap
- Mixpanel, Segment, FullStory, Amplitude
- LinkedIn, Twitter/X, TikTok, Snapchat, Pinterest
- Bing, Yahoo, Amazon, Criteo, Taboola, Outbrain
Doomsday Mode
Blocks all third-party scripts. Use with caution—this may break legitimate functionality.
Tagging Scripts
Basic Tagging
Use data-consent-category to specify which consent category a script requires:
<!-- Requires analytics consent -->
<script data-consent-category="analytics" src="https://example.com/analytics.js"></script>
<!-- Requires marketing consent -->
<script data-consent-category="marketing" src="https://example.com/pixel.js"></script>
<!-- Inline scripts work too -->
<script data-consent-category="analytics">
console.log('This runs after analytics consent');
</script>Categories
| Category | Purpose |
|---|---|
essential | Always allowed (cannot be blocked) |
functional | Site personalization features |
analytics | Usage tracking and statistics |
marketing | Advertising and remarketing |
Allow-listing Scripts
Prevent a script from being blocked (even in strict/doomsday mode):
<script data-zest-allow src="https://trusted-cdn.com/library.js"></script>Custom Blocked Domains
Add your own domains to the blocklist:
window.ZestConfig = {
mode: 'safe',
blockedDomains: [
// Simple domain (defaults to marketing category)
'custom-tracker.com',
// With explicit category
{ domain: 'my-analytics.com', category: 'analytics' },
{ domain: 'my-cdn.com', category: 'functional' }
]
};Cookie & Storage Interception
Zest also intercepts cookie and storage operations:
How It Works
- Intercepts
document.cookiesetter operations - Proxies
localStorage.setItem()andsessionStorage.setItem() - Categorizes operations based on key name patterns
- Queues blocked operations
- Replays when consent is granted
Automatic Categorization
Keys are categorized by matching patterns:
Analytics patterns:
_ga, _gid, _gat, _utm, __utm, plausible, _pk_, matomo, _hj, ajs_Marketing patterns:
_fbp, _fbc, _gcl, _ttp, ads, doubleclick, __gads, __gpi, _pin_, li_Functional patterns:
lang, locale, theme, preferences, ui_Essential patterns (always allowed):
zest_, csrf, xsrf, session, __host-, __secure-Unknown keys default to the marketing category (strictest).
Custom Patterns
Add your own categorization patterns:
window.ZestConfig = {
patterns: {
analytics: [/^my_analytics_/, /^custom_ga/],
marketing: [/^my_ad_/, /^campaign_/],
functional: [/^user_pref_/]
}
};Script Execution Order
When consent is granted:
- External scripts are loaded in their original order
- Inline scripts execute after their dependencies load
- The
zest:consentevent fires after all scripts execute
Debugging
Check which scripts were blocked:
// In browser console
document.querySelectorAll('script[type="text/plain"]').forEach(s => {
console.log('Blocked:', s.src || 'inline script');
});Best Practices
- Load Zest first - Before any tracking scripts in your HTML
- Use safe mode - Good balance of protection and compatibility
- Tag unknown scripts - Use
data-consent-categoryfor scripts Zest doesn't recognize - Test thoroughly - Check that your site works after consent is granted
- Allow essential scripts - Use
data-zest-allowfor critical third-party libraries
