On this page
Overview
The JavaScript API lets your own code check what a visitor has agreed to. Use it to load a script, video or widget only once the visitor accepts its category. It works on every plan.
To turn it on, go to Cookie Banner, then Settings, then Javascript API, tick Javascript API and select Save & Publish.
The cookieScan object
With the API on, the CookieScan script creates window.cookieScan as it loads. It holds the visitor's saved choice.
| Property | Type | Value |
|---|---|---|
| cookieScan.consentGiven | true or false | true once the visitor has made a choice on your banner. |
| cookieScan.consent.preference | true or false | true if the visitor accepted Preference. |
| cookieScan.consent.statistics | true or false | true if the visitor accepted Statistics. |
| cookieScan.consent.statisticsExempt | true or false | true if the visitor allows Statistics cookies you have marked Exempt. |
| cookieScan.consent.marketing | true or false | true if the visitor accepted Marketing. |
| cookieScan.consent.unclassified | true or false | true if the visitor accepted Unclassified. |
Until the visitor makes a choice, every value is false. Necessary cookies have no property. They are always allowed.
A short example that checks for Marketing consent:
if (window.cookieScan && cookieScan.consent && cookieScan.consent.marketing) {
// The visitor accepted Marketing: load your marketing script here
}The consent update event
When a visitor saves a choice, CookieScan fires a cookiescan_consent_update event on document. The event's detail holds the same values as cookieScan.consent, plus consentGiven.
document.addEventListener('cookiescan_consent_update', function (e) {
if (e.detail.marketing) {
// The visitor has just accepted Marketing
}
});The event fires only when Seamless consent (no page reload) is on. Otherwise the page reloads after the choice, and cookieScan holds the new values on the reloaded page. The event also fires when a visitor turns categories off, with those values set to false.
When the values are ready
When your code can read the values depends on how you install CookieScan.
| Install method | When cookieScan is ready |
|---|---|
| Script tag | As soon as the CookieScan script has run. Put your code after the CookieScan script tag. |
| Google Tag Manager | After Tag Manager loads the CookieScan script, which can be after your page's own scripts run. Check that window.cookieScan exists first. |
If you install with Google Tag Manager, you can use Tag Manager events instead of reading cookieScan.
Related settings
- Load your own scripts after consent, with a full example
- Fire tags on consent in Tag Manager
- Banner settings, including Seamless consent (no page reload)
- Cookie categories