Skip to main content

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

RecipeGoal
Gate GA4 on consentLoad Google Analytics 4 only after analytics consent
Block YouTube embedsKeep YouTube iframes blocked until consent, with a placeholder
React and Next.jsWire the banner into a single-page app with route changes
CSP nonceRun OptSens under a strict Content Security Policy
Server-side snippetsRead 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:

CategoryWhat it covers
necessaryAlways allowed: session, security, the CMP itself
functionalChat widgets, language preferences, user settings
analyticsGoogle Analytics, Hotjar, Clarity, Matomo
advertisingFacebook Pixel, Google Ads, TikTok, LinkedIn
performanceSpeed monitoring, A/B testing tools, CDN optimization