First 100 spots 50% lifetime discount. Start free.
Checking cookie consent from your own code
When a script of your own should only run once a visitor has agreed to cookies, ask ClickWhale what that visitor has allowed. The same calls let you build your own consent buttons and links.
| Where | Your own code, in Manage → Custom code or in a Custom code element |
|---|---|
| Needs | The cookie bar switched on |
Wrap it in DOMContentLoaded. Your code runs before ClickWhale's own script, so ClickWhale doesn't exist yet at the moment your code first runs. The listener in the example below waits until it does.
Waiting for consent
Ask, and run your code only when the answer includes the category you need:
<script>
document.addEventListener("DOMContentLoaded", function () {
ClickWhale.consent.on(function (granted) {
if (granted.indexOf("analytics") !== -1) {
// the visitor has agreed to analytics
}
});
});
</script>ClickWhale.consent.on calls your function straight away with what the visitor has already agreed to, and again each time they change their mind, so you don't have to worry about which happened first.
For code on the site-wide Functional, Analytics or Marketing pages you don't need any of this: those pages already wait for their category. See Site-wide custom code.
The calls
| Call | Does |
|---|---|
ClickWhale.consent.has("analytics") | true if that category is currently allowed |
ClickWhale.consent.granted() | The list of categories the visitor allows right now |
ClickWhale.consent.on(fn) | Runs fn immediately with the current list, and again every time the visitor changes it |
ClickWhale.consent.grant(["functional"]) | Records a decision, exactly this list, plus essential |
ClickWhale.consent.withdraw() | Forgets the decision, so the bar asks again |
ClickWhale.consent.open() | Opens the cookie settings dialog |
Two attributes do the same without a script:
data-cw-open-consenton a link or button reopens the cookie dialog, for a "Cookie settings" link in your footer.data-cw-allow="functional"on a button grants that one category. It's the same mechanism behind the Allow and play placeholder on a blocked video.
Timing
- Code in a category page runs late. It's held inert until the visitor accepts, so it can start seconds after the page did, or on a later page, or never. Don't assume it runs during page load.
- Withdrawing doesn't unload anything. A script that already ran keeps running until the visitor navigates. Narrowing consent takes full effect on the next page.
Common issues
My script says ClickWhale is not defined.
keyboard_arrow_down
remove
ClickWhale is not defined.
It ran too early. Wrap it in the DOMContentLoaded listener above.
The widget loads for me but not for a colleague.
keyboard_arrow_down
remove
You accepted that cookie category and they didn't.
My code never runs, even for me.
keyboard_arrow_down
remove
Check that the cookie bar is switched on and that you accepted the category your code asks for on your own site. Click the cookie button in the corner to change your choice. See The cookie bar.
What will you create today?
ClickWhale is a website management system created by Ikabus