Docs menu
JavaScript API
The CookieHug JavaScript SDK: the CookieHug object, consent and state properties, reading consent and methods (show, hide, renew, withdraw, submit…).
JavaScript SDK Reference
CookieHug exposes a global window.CookieHug object that provides access to consent state, methods, events, and callbacks.
CookieHug.consent
| Property | Type | Description |
|---|---|---|
consent.necessary | boolean | Always true — necessary cookies cannot be declined |
consent.preferences | boolean | Whether the user accepted preference cookies |
consent.statistics | boolean | Whether the user accepted statistics cookies |
consent.marketing | boolean | Whether the user accepted marketing cookies |
consent.method | string | null | "explicit" if user made a choice, null if no response yet |
State Properties
| Property | Type | Description |
|---|---|---|
CookieHug.consented | boolean | true if user accepted any optional cookies |
CookieHug.declined | boolean | true if user rejected all optional cookies |
CookieHug.hasResponse | boolean | true if user has responded (accepted or declined) |
CookieHug.doNotTrack | boolean | true if the browser's Do Not Track setting is enabled |
CookieHug.regulations
Resolved in two steps: (1) timezone fallback runs immediately on script load; when a European timezone is detected, regulations.region is also seeded to the pseudo-region "EEA" so downstream gtag('consent','default',{region}) calls are scoped correctly. (2) Server resolver GET /api/runtime/regulations (Cloudflare CF-IPCountry) runs in parallel with config load — when it returns a region, it is authoritative and fully overwrites the timezone fallback (including all *Applies flags), so VPN / mismatched timezone cases are corrected.
| Property | Type | Description |
|---|---|---|
regulations.gdprApplies | boolean | true if visitor is in Europe (GDPR region) |
regulations.ccpaApplies | boolean | true if visitor is in the USA (CCPA region) |
regulations.lgpdApplies | boolean | true if visitor is in Brazil (LGPD region) |
Example: Reading Consent State
// Check if the user has responded
if (window.CookieHug.hasResponse) {
console.log('User has made a consent choice');
if (window.CookieHug.consent.statistics) {
// Safe to initialize analytics
initAnalytics();
}
if (window.CookieHug.consent.marketing) {
// Safe to load marketing pixels
loadMarketingPixels();
}
}
// Check regulations
if (window.CookieHug.regulations.gdprApplies) {
console.log('GDPR applies to this visitor');
}Methods
CookieHug.show()
Force the consent banner to be displayed.
CookieHug.show();CookieHug.hide()
Hide the consent banner programmatically.
CookieHug.hide();CookieHug.renew()
Show the consent banner so the user can update their preferences. Use this for a "Cookie Settings" link.
// Example: custom cookie settings link
document.getElementById('cookie-settings')
.addEventListener('click', function() {
CookieHug.renew();
});CookieHug.withdraw()
Withdraw the user's consent. This clears all consent data (localStorage and HTTP cookie), resets Google Consent Mode to denied, pushes a consent_update event to dataLayer, resets the ScriptBlocker state, and automatically reloads the page to ensure all scripts respect the new denied state.
CookieHug.withdraw();CookieHug.submitCustomConsent(preferences, statistics, marketing)
Submit a custom consent selection programmatically.
| Parameter | Type | Description |
|---|---|---|
preferences | boolean | Grant or deny preference cookies |
statistics | boolean | Grant or deny statistics cookies |
marketing | boolean | Grant or deny marketing cookies |
// Accept only statistics, deny marketing and preferences
CookieHug.submitCustomConsent(false, true, false);
// Accept all
CookieHug.submitCustomConsent(true, true, true);
// Deny all optional
CookieHug.submitCustomConsent(false, false, false);CookieHug.getScript(url, async, callback)
Dynamically load an external script. Useful for loading scripts after consent is granted.
| Parameter | Type | Description |
|---|---|---|
url | string | Script URL to load |
async | boolean | Whether to load asynchronously |
callback | function | Called after script loads |
if (CookieHug.consent.statistics) {
CookieHug.getScript(
'https://www.google-analytics.com/analytics.js',
true,
function() {
console.log('Analytics loaded');
}
);
}CookieHug.runScripts()
Evaluate and execute all <script> tags marked with data-cookiehug-consent that match the current consent state. Called automatically after consent is given, but can be called manually for SPAs after dynamic content is loaded.
CookieHug.runScripts();