SDK Overview
The CookieBoss consent script exposes a global window.__cookieboss object you can use to programmatically interact with the consent banner.
Global API
After the consent script loads, window.__cookieboss is available with these properties and methods:
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
config | ConsentScriptConfig | No | - | The full configuration object for this site |
version | number | No | - | Current script version number |
getConsent() | () => ConsentCategories | No | - | Returns the current consent categories |
showBanner() | () => void | No | - | Re-shows the consent banner (triggers geo detection) |
acceptAll() | () => void | No | - | Accepts all cookie categories and dismisses the banner |
rejectAll() | () => void | No | - | Rejects all optional categories and dismisses the banner |
Reading consent state
Use getConsent() to check which categories the visitor has consented to:
const consent = window.__cookieboss.getConsent();
console.log(consent);
// { necessary: true, analytics: true, marketing: false, functional: true }
if (consent.analytics) {
// Safe to load analytics scripts
}
if (consent.marketing) {
// Safe to load marketing pixels
} The necessary category is always true — essential cookies cannot be declined.
Showing the banner programmatically
If you want to let users change their preferences (e.g. from a “Cookie Settings” link in your footer), call showBanner():
<a href="#" onclick="window.__cookieboss.showBanner(); return false;">
Cookie Settings
</a> This triggers geo detection and shows the appropriate banner for the visitor’s location.
Accepting or rejecting programmatically
You can call acceptAll() or rejectAll() directly — useful for custom UIs:
// Accept all categories
window.__cookieboss.acceptAll();
// Reject all optional categories (necessary stays true)
window.__cookieboss.rejectAll(); Both methods persist the consent choice (cookie + localStorage), dispatch the cookieboss:update event, update Google Consent Mode (if enabled), push to GTM dataLayer (if enabled), and unblock/block scripts accordingly.
Waiting for the script to load
The consent script loads asynchronously. To safely access the API, wait for it:
function onCookieBossReady(callback) {
if (window.__cookieboss) {
callback(window.__cookieboss);
} else {
// Poll until available
const interval = setInterval(() => {
if (window.__cookieboss) {
clearInterval(interval);
callback(window.__cookieboss);
}
}, 50);
}
}
onCookieBossReady((cb) => {
console.log('Consent state:', cb.getConsent());
}); Event-driven alternative
Instead of polling, listen for the cookieboss:update event — it fires whenever consent changes. See Events.
ConsentCategories type
interface ConsentCategories {
necessary: true; // Always true — cannot be declined
analytics: boolean; // Analytics cookies (GA, etc.)
marketing: boolean; // Marketing/advertising cookies
functional: boolean; // Functional cookies (preferences, chat widgets)
} Next steps
- Events — listen for consent changes
- Consent State — how consent is stored and persisted
- Script Blocking — automatically block third-party scripts