Docs menu

Events & callbacks

CookieHug SDK events and configuration callbacks — listening to consent changes, with examples.

Events

CookieHug dispatches Custom Events on the window object. Use addEventListener to listen for them.

Available Events

EventDescriptionevent.detail
CookieHugOnConsentReadyConsent state is ready (loaded or newly submitted){ consent: {...} }
CookieHugOnLoadBanner loaded, consent state read{}
CookieHugOnAcceptUser accepted cookies (or had previously accepted){ consent: {...} }
CookieHugOnDeclineUser declined cookies (or had previously declined){ consent: {...} }
CookieHugOnDialogInitBanner initialized (before display){}
CookieHugOnDialogDisplayBanner displayed to user{}
CookieHugOnTagsExecutedScripts with data-cookiehug-consent were executed{ count: N }

Example: Listening for Events

window.addEventListener('CookieHugOnAccept', function(e) {
  console.log('Consent given:', e.detail.consent);

  if (e.detail.consent.statistics) {
    // Initialize analytics
  }
  if (e.detail.consent.marketing) {
    // Initialize marketing pixels
  }
});

window.addEventListener('CookieHugOnDecline', function(e) {
  console.log('User declined cookies');
});

window.addEventListener('CookieHugOnConsentReady', function(e) {
  // Fires on every page load once consent state is determined
  console.log('Consent state:', e.detail.consent);
});

Callbacks

As an alternative to events, you can define global callback functions. Define them before the CookieHug script loads.

Available Callbacks

CallbackCorresponding Event
CookieHugCallback_OnConsentReadyCookieHugOnConsentReady
CookieHugCallback_OnLoadCookieHugOnLoad
CookieHugCallback_OnAcceptCookieHugOnAccept
CookieHugCallback_OnDeclineCookieHugOnDecline
CookieHugCallback_OnDialogInitCookieHugOnDialogInit
CookieHugCallback_OnDialogDisplayCookieHugOnDialogDisplay
CookieHugCallback_OnTagsExecutedCookieHugOnTagsExecuted

Example: Using Callbacks

<script>
  // Define callbacks BEFORE the CookieHug script
  window.CookieHugCallback_OnAccept = function() {
    console.log('Cookies accepted!');
    // Initialize third-party services
  };

  window.CookieHugCallback_OnDecline = function() {
    console.log('Cookies declined');
  };

  window.CookieHugCallback_OnLoad = function() {
    console.log('CookieHug loaded');
  };
</script>

<script src="https://cookiehug.com/api/script/YOUR-LICENSE-KEY.js"></script>