Kookee ships a cookie consent banner: a small widget that asks the visitor which cookie categories they accept, remembers the answer, and switches your tracking scripts on only after they say yes.
Everything the visitor sees — categories, services, texts, position, theme, accent color — is configured per project on the Cookie banner page in your dashboard and published from there. The widget fetches that configuration with your API key, so changing the wording never means a code deploy.
Add it with a script tag
For any site with an HTML head, use the script build. It initializes itself from the tag's data-api-key and exposes the API as the KookeeConsent global:
<!-- Google Consent Mode v2 defaults: must run before any gtag/analytics script -->
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('consent', 'default', {
analytics_storage: 'denied',
ad_storage: 'denied',
ad_user_data: 'denied',
ad_personalization: 'denied',
wait_for_update: 500
});
</script>
<!-- The stub queues API calls made before the script loads -->
<script>
window.KookeeConsent = window.KookeeConsent || {_q:[],
on: function(){this._q.push(['on',[].slice.call(arguments)])},
onChange: function(){this._q.push(['onChange',[].slice.call(arguments)])},
show: function(){this._q.push(['show',[]])}};
</script>
<script async src="https://kookee.dev/consent/latest.js" data-api-key="YOUR_API_KEY"></script>
The same snippet with your key already filled in is on the project's Cookie banner page.
Three things to know about the global:
KookeeConsentis the API object. There is noinit()— the script initializes itself on load fromdata-api-key, anddata-base-urloverrides the API host if you need it to.The stub only defines
on(),onChange()andshow(). Those three are safe to call before the bundle arrives and are replayed in order the moment it loads.isGranted()andget()are not on the stub, so calling them that early throws — read state after the bundle is there, or react toon()/onChange()instead.The banner renders inside a shadow root appended to
<body>, so your stylesheets cannot reach it and it cannot leak into your page.
The stub is the same pattern the chat widget uses — see Control the chat widget from JavaScript if you want the reasoning behind it in full.
Add it from npm
The widget ships on its own subpath, so it is only bundled if you import it:
import { initKookeeConsent } from '@kookee/sdk/consent';
const consent = initKookeeConsent({ apiKey: 'YOUR_API_KEY' });
initKookeeConsent({ apiKey, baseUrl }) takes the API key (required) and an optional baseUrl, and returns the same API object the global exposes. Call it as your app boots; it waits for DOMContentLoaded on its own before touching the page. Every call returns the page's one instance and ignores the options of later calls, so React StrictMode, a remount or the script tag included as well never show a second banner.
@kookee/sdk/consent is the package's only code subpath. The full export map is @kookee/sdk, @kookee/sdk/consent, @kookee/sdk/styles/content.css and @kookee/sdk/styles/typography.css — nothing else resolves.
The API
on(category, callback)— run the callback once the category is granted and the tags gated on it have loaded, now or later.onChange(callback)— run the callback every time choices are applied.isGranted(category)— synchronoustrue/falsefor the current state.get()— the whole choices map, as a copy.show()— reopen the preferences dialog.ready— a promise that resolves once the config is loaded and any stored consent applied, its gated tags included.
Everything is category-based, and the category keys come from your project's configuration. A key that does not exist there is simply never granted.
The categories
You define the categories in the dashboard: each has a key, a name, an optional description, a required flag and a list of services shown to the visitor. A new project starts with three:
necessary— required for the site to work. Always granted.analytics— optional.marketing— optional.
Keys are lowercase letters, digits and hyphens, up to 20 categories per project. Categories marked required are always granted and their switch is disabled in the dialog.
Two consequences worth knowing:
If every category is required, there is nothing to consent to: no banner appears, no cookie is written and no event is recorded.
analyticsandmarketinghave a fixed meaning for Google Consent Mode. Rename their labels freely, but keep those keys if you use Consent Mode.
Gate your tracker script tags
Change a tracker's type to text/plain and tag it with a category key:
<script type="text/plain" data-kookee-consent="analytics"
src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXX"></script>
<script type="text/plain" data-kookee-consent="analytics">
gtag('js', new Date());
gtag('config', 'G-XXXXXXX');
</script>
Browsers do not execute text/plain, so nothing runs. Once consent is applied, the widget replaces each tag whose category was granted with a real script element, copying every other attribute across, the CSP nonce included. Tags run in document order: after a tag with a src and no async, the next one waits for it to load, for at most 10 seconds, so the inline half of a snippet like gtag's can call the library above it.
Gated tags are activated once, when consent is applied on page load. Markup your app inserts later — a client-side route change, a tag manager — is not scanned again. Gate those from JavaScript.
Gate from JavaScript
on() runs the callback once the category is granted and the tags gated on it have loaded: on page load if the visitor consented on an earlier visit, or right after they accept. So the callback can use a library one of those tags loads.
consent.on('analytics', () => {
loadAnalytics();
});
It fires at most once per page load, never fires for a category the visitor declined, and there is nothing to unsubscribe. This is the reliable way to load a script that your bundler owns rather than your HTML.
Wait for it with ready
ready resolves once the widget has fetched its config and applied any stored consent, gated tags included:
await consent.ready;
console.log(consent.get()); // { necessary: true, analytics: true, marketing: false }
Until it resolves, isGranted() returns false and get() returns {} for everybody — including a visitor who accepted months ago, because the stored cookie has not been read yet. It also resolves when the configuration could not be loaded at all, so read it as "the widget finished starting", not "there is consent".
Read the current state
if (consent.isGranted('marketing')) {
renderRetargetingPixel();
}
isGranted() is synchronous and returns false until the visitor has decided. get() hands back a copy of the map, so mutating it changes nothing:
const choices = consent.get(); // { necessary: true, analytics: false, marketing: false }
React to changes
consent.onChange((choices) => {
console.log(choices); // { necessary: true, analytics: true, marketing: false }
});
onChange also fires once on page load, when a returning visitor's stored choice is applied — so a listener registered at startup sees the current state without a separate read.
One case behaves differently. When a visitor switches off a category they had already granted in this page's session, the widget stores the new choice, records a withdrawal, updates Consent Mode and then reloads the page. A script that already executed cannot be unloaded, so a clean load is the only honest way to stop it. Do not write teardown logic in onChange and expect it to run — assume the page is about to reload.
Reopen the banner
Most cookie rules require a way to change your mind. Point a footer link at show():
<button id="cookie-settings">Cookie settings</button>
<script>
document.getElementById('cookie-settings').addEventListener('click', function () {
window.KookeeConsent.show();
});
</script>
From npm it is the same call on the object you got back:
document.querySelector('#cookie-settings')?.addEventListener('click', () => consent.show());
show() opens the preferences dialog with the visitor's current choices already selected. It waits for ready internally, so an early click is safe; if the configuration could not be loaded, it does nothing.
Google Consent Mode
When Consent Mode is enabled for your project, the widget fires a consent update every time choices are applied, with a fixed mapping:
analyticsdrivesanalytics_storagemarketingdrivesad_storage,ad_user_dataandad_personalization
The default denied call is yours to make. An async widget cannot reliably run before gtag.js, so those defaults belong inline in your head — that is the first block of the embed snippet above.
Sites running Google Ads or AdSense in the EEA or UK need a Google-certified consent platform. This banner does not cover that case.
What gets stored
The decision lives in a first-party cookie named kk_consent (path /, SameSite=Lax, Secure on https pages, one year). It holds the choices, an anonymous visitor id and the id of the configuration version the visitor was shown. Each decision is also sent to Kookee as an event — accept_all, reject_all, custom or withdraw — which is what the records list on the Cookie banner page shows.
Publishing a new configuration invalidates stored consent: the version id no longer matches, so returning visitors are asked again. That is deliberate. Consent given to three categories does not cover a fourth you added later.
TypeScript
With the package installed, take the type from the same subpath and declare the global once:
import type { KookeeConsentApi } from '@kookee/sdk/consent';
declare global {
var KookeeConsent: KookeeConsentApi;
}
Without npm — a plain HTML site with a tsconfig — download https://kookee.dev/consent/latest.d.ts into your types folder instead. It declares both the KookeeConsent global and Window.KookeeConsent, with every referenced type inlined so the file imports nothing:
// types/kookee-consent.d.ts — downloaded, not hand-written
KookeeConsent.on('analytics', () => loadAnalytics()); // now typed
That copy is a snapshot: it does not update when the script does, so prefer the package wherever you already have a build step.
When no banner appears
The configuration is not published. The widget asks for the published version; a project with only unsaved edits gets nothing back and stays silent. Publish it from the Cookie banner page.
Every category is required. There is nothing to ask, so nothing is shown.
The tag has no
data-api-key. You get[kookee-consent] Missing data-api-key attribute on the script tagin the console, and an inert API where every method is a no-op andisGranted()is alwaysfalse.The project does not allow your domain. If the project's allowed origins do not include your domain, the config request fails and the widget stays silent. See API keys and security.
The visitor already decided. That is the normal path: stored consent is applied without showing anything. Clear the
kk_consentcookie, or callshow(), to see the dialog again.