Cookiebot callbacks and event listeners
In our Developer documentation we have listed a number of events which you can hook into that are fired during the execution of the uc.js script:
- CookiebotOnConsentReady
- CookiebotOnLoad
- CookiebotOnAccept
- CookiebotOnDecline
- CookiebotOnDialogInit
- CookiebotOnDialogDisplay
- CookiebotOnTagsExecuted
You can use these events in an event listener similar to this:
window.addEventListener('CookiebotOnLoad', function () {
// Do something
})
Similarly, we also have a list of callback functions which are called when the corresponding events are fired:
- CookiebotCallback_OnLoad
- CookiebotCallback_OnAccept
- CookiebotCallback_OnDecline
- CookiebotCallback_OnDialogInit
- CookiebotCallback_OnDialogDisplay
- CookiebotCallback_OnTagsExecuted
You can define a callback function similar to this:
function CookiebotCallback_OnAccept() {
// Do something
}
As the Cookiebot script is loading, it will check if these functions have been defined, and call them at the appropriate time if this is the case.
Confusion surrouding the CookiebotOnLoad event and the CookiebotCallback_OnLoad callback
The purpose of both the CookiebotOnLoad event and the CookiebotCallback_OnLoad callback is often misunderstood. The "load" implied here doesn't refer to the loading of the Cookiebot script. It refers to the loading of consent.
Therefore, these events occur when a visitor submits consent on the banner, or when the Cookiebot script determines the visitor's consent from the CookieConsent cookie during the loading of the Cookiebot script.
It is completely unrelated to when the Cookiebot script (uc.js) finishes loading and none of the events or callbacks listed above tell you when this occurs.
Determining when the Cookiebot (uc.js) script is finished loading
There may be situations where you want to know when the uc.js script has finished loading, and as we determined above, there are no Cookiebot events or callback that help us in that regard.
To be able to determine when the uc.js script has finished loading, we can create an event and a callback of our own though. You can do this using the script below.
Enter your own Domain Group ID here to update the snippet:
<script>(function(c,oo,k,ie,b,o,t){b=oo.scripts[0],o=oo.createElement(k);
o.src='https://consent.cookiebot.com/uc.js',o.id=c;o.dataset.cbid=ie;o.async=1;
o.addEventListener('load',function(){var a=new CustomEvent('CookiebotLoaded',
{bubbles:!0});oo.dispatchEvent(a);});
typeof CookiebotCallback_Loaded==="function"&&CookiebotCallback_Loaded();
b.parentNode.insertBefore(o,b);})('Cookiebot',document,'script',
'00000000-0000-0000-0000-000000000000')</script>
This will fire a new event "CookiebotLoaded" and also call the callback function "CookiebotCallback_Loaded".
Keep in mind that if you want to use the callback, then you need to have defined it before the above script loads, otherwise the function won't exist at the time the script wants to call it.
Comments
0 comments
Please sign in to leave a comment.