Recipes
Short, copy-paste guides for common consent tasks. Each recipe leads with the goal, gives one complete code block, then shows how to verify it.
Every recipe uses the public API documented under Developers. The methods, events and data attributes here are the same ones described there.
The recipes
| Recipe | Goal |
|---|---|
| Gate GA4 on consent | Load Google Analytics 4 only after analytics consent |
| Block YouTube embeds | Keep YouTube iframes blocked until consent, with a placeholder |
| React and Next.js | Wire the banner into a single-page app with route changes |
| CSP nonce | Run OptSens under a strict Content Security Policy |
| Server-side snippets | Read the consent cookie from your backend |
Before you start
The snippet must be installed first-party in the page head:
<script src="https://cdn.optsens.com/optsens.min.js/YOUR_DOMAIN_ID"></script>
Replace YOUR_DOMAIN_ID with the value from your dashboard Integration
page. See Direct snippet for placement
rules.
The five categories
Recipes refer to consent categories by these names:
| Category | What it covers |
|---|---|
necessary | Always allowed: session, security, the CMP itself |
functional | Chat widgets, language preferences, user settings |
analytics | Google Analytics, Hotjar, Clarity, Matomo |
advertising | Facebook Pixel, Google Ads, TikTok, LinkedIn |
performance | Speed monitoring, A/B testing tools, CDN optimization |