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
| Event | Description | event.detail |
|---|---|---|
CookieHugOnConsentReady | Consent state is ready (loaded or newly submitted) | { consent: {...} } |
CookieHugOnLoad | Banner loaded, consent state read | {} |
CookieHugOnAccept | User accepted cookies (or had previously accepted) | { consent: {...} } |
CookieHugOnDecline | User declined cookies (or had previously declined) | { consent: {...} } |
CookieHugOnDialogInit | Banner initialized (before display) | {} |
CookieHugOnDialogDisplay | Banner displayed to user | {} |
CookieHugOnTagsExecuted | Scripts 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
| Callback | Corresponding Event |
|---|---|
CookieHugCallback_OnConsentReady | CookieHugOnConsentReady |
CookieHugCallback_OnLoad | CookieHugOnLoad |
CookieHugCallback_OnAccept | CookieHugOnAccept |
CookieHugCallback_OnDecline | CookieHugOnDecline |
CookieHugCallback_OnDialogInit | CookieHugOnDialogInit |
CookieHugCallback_OnDialogDisplay | CookieHugOnDialogDisplay |
CookieHugCallback_OnTagsExecuted | CookieHugOnTagsExecuted |
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>