Embed scripts (consent.js)
- Written for
- + Written for
- Deprecated
- + Deprecated
- Applies to
- + Applies to
Embed scripts (consent.js)
Every site is protected by a single <script> tag. It loads consent.js — a small, dependency-free IIFE — which installs prior-blocking, fetches your site's config, and renders the banner.
The install tag
<script id="CookieMunch"
src="https://cdn.cookiemunch.net/consent.js"
data-api="https://api.cookiemunch.net"
data-cbid="your-site-id"
data-blockingmode="auto"></script>Place it as early as possible in <head> — before any analytics/marketing tags — so prior-blocking can intercept them. The generated snippet for a given site is also available programmatically:
curl https://api.cookiemunch.net/v1/sites/YOUR_CBID/snippet \
-H "Authorization: Bearer fck_…"{
"snippet": "<script id=\"CookieMunch\"\n src=\"https://cdn.cookiemunch.net/consent.js\"\n data-cbid=\"your-site-id\"\n data-blockingmode=\"auto\"></script>",
"src": "https://cdn.cookiemunch.net/consent.js",
"api": "https://api.cookiemunch.net",
"cbid": "your-site-id",
"blockingMode": "auto"
}Note: The element
idmay beCookieMunchorCookiebot— the embed looks for either (in that order), then falls back todocument.currentScript. This is what makes drop-in migration from Cookiebot possible: existingid="Cookiebot"tags keep working unmodified.
Attributes
Attribute | Required | Values | Description |
|---|---|---|---|
| yes | URL | The |
| yes | string | The site identifier issued when you create the site. Without it the embed refuses to start. |
| no |
| Auto-blocking strategy. |
| no | URL | Overrides the API origin used for config fetch + beacons + the lazy |
| no | BCP-47 tag, e.g. | Force the initial banner language, overriding auto-detection from |
All three functional settings (cbid, blockingmode, culture) can also be supplied via window.CookieMunchConfig = { cbid, blockingmode, culture } (useful when a tag manager can't set data-* attributes) or as query parameters on the script src (?cbid=…&blockingmode=…). Precedence is data-* attribute > window.CookieMunchConfig
srcquery string.
What consent.js does on load
Installs prior-blocking synchronously, before fetching anything — so no tracker script that loads later in the page can slip through the window between page-load and config-fetch. See script blocking & data-attributes.
Fetches the live config from
GET <api-origin>/config/:cbidand re-normalizes the blocker/banner/framework settings against it. If the fetch fails (offline, CORS, outage), it falls back to the attribute-only base config rather than leaving the page unprotected.Detects the visitor's region (server-provided) and resolves the banner mode (opt-in / opt-out / off) per your geo rules, honoring Global Privacy Control (GPC) as a forced decline where applicable.
Renders the banner (or applies implied consent silently, depending on mode) and exposes
window.CookieMunch— see the JavaScript API.Beacons anonymous impression + consent-decision events back to the API origin. These calls are unauthenticated by design (the embed runs in an untrusted browser); authenticated read access to the resulting data goes through consent log, stats & export.
consent-tcf.js (lazy IAB bundle)
When a site has framework: "iab" (TCF), GPP, or US-Privacy enabled, consent.js installs synchronous __tcfapi / __gpp / __uspapi stubs immediately (so any ad tech checking for them on page-load sees a valid, if pending, API), then lazily loads consent-tcf.js in the background. That bundle carries the IAB TC-string encoder/GVL lookup logic (@iabtcf/*-derived), which would otherwise bloat the base embed for the majority of sites that don't need IAB compliance. Once loaded, it re-encodes the TC string on every explicit accept/decline and dispatches it through the stub to any listener (addEventListener('addEventListener', …)) already registered by ad tags.
You never load consent-tcf.js yourself — it is fetched automatically, from the same API origin as the beacons, only when needed.
Cookiebot compatibility
Cookie Munch's embed is designed as a drop-in replacement for Cookiebot's script:
The install tag accepts
id="Cookiebot"in addition toid="CookieMunch".window.Cookiebotis exposed as an alias ofwindow.CookieMunch(same object).Lifecycle events are dispatched under both
CookieMunch<Event>andCookiebot<Event>names, and bothCookieMunchCallback_<Event>andCookiebotCallback_<Event>globals are invoked if defined.The legacy
data-cookieconsentscript-blocking attribute is recognized (see script blocking & data-attributes).
This means most Cookiebot integrations (ad-network wiring, custom onConsentReady handlers) work by swapping the <script> tag with no other code changes.
Next: JavaScript API for window.CookieMunch, or script blocking & data-attributes for prior-blocking markup.