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.

State Properties

PropertyTypeDescription
CookieHug.consentedbooleantrue if user accepted any optional cookies
CookieHug.declinedbooleantrue if user rejected all optional cookies
CookieHug.hasResponsebooleantrue if user has responded (accepted or declined)
CookieHug.doNotTrackbooleantrue 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.

PropertyTypeDescription
regulations.gdprAppliesbooleantrue if visitor is in Europe (GDPR region)
regulations.ccpaAppliesbooleantrue if visitor is in the USA (CCPA region)
regulations.lgpdAppliesbooleantrue if visitor is in Brazil (LGPD region)

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.

ParameterTypeDescription
preferencesbooleanGrant or deny preference cookies
statisticsbooleanGrant or deny statistics cookies
marketingbooleanGrant 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.

ParameterTypeDescription
urlstringScript URL to load
asyncbooleanWhether to load asynchronously
callbackfunctionCalled 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();