Skip to content

JavaScript API

window.Kookiok methods

Programmatic banner control: showBanner(), revokeConsent(), getConsent(), hasConsent('marketing'), setLanguage('de'), getAvailableLanguages().

Consent events

Subscribe to the consent lifecycle: Kookiok:consentUpdate (also through Kookiok.on('consentUpdate', fn)), KookiokOnConsentReady, KookiokOnAccept, KookiokOnDecline, KookiokOnScriptsUnblocked, KookiokOnDialogDisplay, KookiokOnLoad, KookiokOnStorageBlocked, KookiokOnStorageUnblocked.

GPC integration

How the Global Privacy Control signal is applied, where (the banner’s setting), and how a visitor gives consent over it. Below.

Embedding & lifecycle

Script gating with data-ag-consent is documented in Script Blocking, Google Consent Mode v2 below. Still to come: custom DOM hooks and the IAB TCF v2.2 (window.__tcfapi) reference.

The JavaScript API is fully implemented and available in production. You can experiment with it in the browser console once the consent script is loaded:

// Read current consent
Kookiok.getConsent();
// { necessary: true, preferences: false, statistics: false, marketing: false }
// null when their decision no longer counts, and under GDPR before they decide.
// Under the opt-out models an undecided visitor reads as not opted out - see below.
// Check a single category
Kookiok.hasConsent('marketing'); // false
// Programmatically open the banner. It opens on the visitor's current choice while that
// choice is still valid, so a "Cookie settings" link lets them review and re-save it.
Kookiok.showBanner();
// Revoke all consent
Kookiok.revokeConsent();
// Switch language
Kookiok.setLanguage('cs');
// List available languages
Kookiok.getAvailableLanguages();

Both readers report the decision that is currently valid

Section titled “Both readers report the decision that is currently valid”

getConsent() and hasConsent() answer with the decision the runtime is acting on, not with whatever is in the cookie. That cuts both ways.

A stored decision stops counting when a Global Privacy Control signal starts to apply to the visitor after they decided (their browser starts sending it, or the banner’s setting starts applying it to them), or when a re-consent threshold has moved past the configuration they agreed to. The signal then refuses automatically, or the banner is shown again; the runtime blocks its own annotated tags, and these two readers return null / false to match. A decision the visitor made themselves while the signal applied keeps counting.

And where the visitor has made no decision at all, the answer follows the model that applies to them. Under GDPR that is null - nothing may run yet. Under the opt-out model (CCPA) the visitor has not opted out, so the runtime is already running annotated tags for them and these readers say so, unless a Global Privacy Control signal reverses it. Gating your own script with hasConsent() therefore gives you the same behaviour as annotating it with data-ag-consent, for every visitor.

This matters most for code that runs early. The consent script is a head script, so a signal that arrives with the page is applied when the document is ready - a footer script that asked first would otherwise act on a decision the visitor had already overridden.

  • Kookiok:consentUpdate (also Kookiok.on('consentUpdate', fn)) fires for every decision made on the banner, in the privacy popup or through the API, and for every withdrawal. Its detail is the decision now in force; after a withdrawal every optional category in it is false.
  • KookiokOnAccept fires for a decision that grants at least one optional category. Its detail is { consent, method }.
  • KookiokOnDecline fires for a decision that grants none: Reject all, “Accept selected” with nothing selected (how a visitor refuses on a banner without a Reject all button), the automatic refusal of a Global Privacy Control signal, or a Kookiok.acceptAll() the signal refused. It also fires for every withdrawal. Its detail carries consent, and method for a withdrawal.

If you load tags on KookiokOnAccept, check detail.consent for the category each tag needs. The event says that something was granted, not what.

KookiokOnLoad fires once the consent script has started. Its detail carries:

  • regulation - 'gdpr', 'ccpa' or 'none', the regime applied to this visitor.
  • unavailableLayers - the blocking layers the page would not let the runtime install, by API name: 'window.fetch', 'XMLHttpRequest', 'navigator.sendBeacon', 'document.cookie', 'Storage', 'document.write', 'document.writeln', 'Element.setAttribute', 'HTMLIFrameElement.setAttribute', 'HTMLScriptElement.src', 'HTMLIFrameElement.src', 'HTMLImageElement.src' or 'MutationObserver'. It is an empty array when every layer is in place, and when none was attempted - visitors under the US opt-out model who have not opted out, and manual blocking mode.

A page that locks one of these APIs costs exactly that layer; the banner and the other layers keep working. Wix always locks document.cookie, and with its security hardening on also window.fetch and setAttribute on the iframes it creates.

The event fires while the consent script itself is running, so only a listener registered before it receives the event - a diagnostic snippet you add while investigating, for example. The same list is written to the browser console as a single warning, which is usually the quicker check.

window.addEventListener('KookiokOnLoad', function (e) {
if (e.detail.unavailableLayers.length) {
console.log('Not intercepted on this page:', e.detail.unavailableLayers);
}
});

KookiOk runs Google Consent Mode v2 itself (Implementation, Google Consent Mode tab); there is nothing to set up in your page, and in Google Tag Manager only what Advanced asks for (below). If your page has its own gtag('consent', 'default', ...) block, remove it, so the consent state has one source.

Defaults. The consent script is the first script in <head>, so it sends them before any Google tag runs: ad_storage, ad_user_data, ad_personalization, analytics_storage, functionality_storage and personalization_storage denied, security_storage granted, with wait_for_update (500 ms unless you changed it). Unless you switched ads data redaction off, gtag('set', 'ads_data_redaction', true) goes with them: Google Ads and Floodlight tags leave ad click identifiers out of their requests while advertising consent is denied (Google Analytics is not covered: the page address it sends keeps them).

Updates. Each decision sends gtag('consent', 'update', ...), and a stored decision is sent again on every page view:

CategoryConsent Mode types
statisticsanalytics_storage
marketingad_storage, ad_user_data, ad_personalization
preferencesfunctionality_storage, personalization_storage

A withdrawal sends everything denied. Each decision also pushes cookie_consent_update to window.dataLayer, and cookie_consent_statistics, cookie_consent_marketing or cookie_consent_preferences for each category granted, for triggers in Google Tag Manager.

Basic and Advanced are Google’s two modes. They differ in what happens before a decision and after a refusal, for visitors who have to opt in:

BasicAdvanced
Before the visitor decidesGoogle’s tags do not runGoogle’s tags load with everything denied and send pings without cookies, for Google’s modeling
After the visitor consentsGoogle’s tags load and measure what was grantedGoogle’s tags measure what was granted
After a refusalGoogle’s tags do not runGoogle’s tags keep sending pings without cookies

With automatic blocking, Basic holds the Google tag (gtag.js) and the Tag Manager container (gtm.js) until the visitor grants statistics. Advanced lets them load at once, along with the requests they send while advertising storage is denied (each says so to Google), and turns on URL passthrough (gtag('set', 'url_passthrough', true)), so ad clicks are still attributed while advertising consent is denied. AdSense and a DoubleClick pixel added by hand stay blocked until the visitor consents, and so does every other vendor the consent script recognizes, also when your Tag Manager container adds its tag. A container tag for a vendor the consent script does not recognize runs as soon as the container does: in Tag Manager, open the tag’s Consent Settings and set “Require additional consent for tag to fire” (for example ad_storage for an advertising tag).

A Google tag reads the consent state from its data layer when it runs, and assumes consent when it finds no default there. In Advanced the consent script keeps its denied state in force against what pages commonly do meanwhile: it follows the defaults with an update (everything denied, so Google’s wait_for_update does not apply in Advanced), which outranks any consent default your page or a Tag Manager template sets later, and it carries its commands to the front of an array your page assigns to window.dataLayer (dataLayer = [...]). Google’s tags still wait for consent, as in Basic, when they read a data layer of another name (the l= parameter) or when window.dataLayer refuses the defaults.

Push your data with dataLayer.push(...) rather than assigning a new array: in Advanced an assigned array gets the consent script’s commands at its front (code that reads dataLayer[0] finds them there), and the cookie_consent_* events pushed before are not carried over. Not covered, so do not do them: sending gtag('consent', 'update', ...) yourself (an update is the visitor’s decision), emptying the array in place (dataLayer.length = 0, dataLayer.splice(0)) or deleting window.dataLayer before Google’s tags have run.

Whether pings may be sent before consent is a legal question for where you operate; Basic runs no Google tag until the visitor agrees. Two cases need the data-ag-consent contract (type="text/plain" with data-ag-consent="statistics") in both modes: a Google tag written into your HTML is fetched by the browser before the consent script can hold it (it does not run), and a Google tag served from your own domain (Google tag gateway, a server-side Tag Manager) is not recognized as Google’s.

In manual blocking mode the consent script blocks nothing itself, so the mode follows how you load Google’s tags: written with type="text/plain" and data-ag-consent="statistics" they wait for consent, loaded normally they run with the denied defaults.

A visitor to whom a Global Privacy Control signal applies is recorded as refusing everything, without a banner, and Google’s tags do not load for them in Advanced either, even after they give consent themselves (they load as in Basic, once statistics is granted). Where the banner’s setting does not apply the signal to the visitor, they are treated like any other visitor who must opt in.

With automatic blocking in Basic, the Google tag and the Tag Manager container wait for statistics consent, so a visitor who accepts only marketing gets no Google Ads tags; in Advanced they load, and Google Ads measures with the advertising consent the visitor gave.

Under the US opt-out model nothing is blocked before a choice, so Google’s tags load in both modes, and Consent Mode is updated to granted at once: the visitor has not opted out, which is also what Kookiok.getConsent() and __uspapi report. An opt-out - Reject all, a choice that refuses a category, a withdrawal, or a Global Privacy Control signal - updates it to what the visitor refused, and from then on the refused trackers are blocked, as under GDPR. Basic and Advanced are for visitors who must opt in: after a US opt-out, Google’s tags are held in both modes.

A browser can send a Global Privacy Control signal (navigator.globalPrivacyControl). The consent script reads it once as the page loads and acts on that answer for the whole page view, which Kookiok.isGpcEnabled() reports; a browser that starts or stops sending it meanwhile (a changed preference, an extension that sets it late) is read again on the next page. Where it applies, the consent script refuses every optional category automatically, without a banner, and records that decision (consentMethod: "gpc_auto"); a visitor who gets no banner at all (outside the banner’s region, or in a country you mapped to none) is not recorded, this refusal included. California, Colorado, Connecticut and other US states require a business to honor the signal as an opt-out; under the GDPR it has no status of its own, and nothing optional runs before consent anyway.

Where it applies is the banner’s setting (Privacy tab, “Global Privacy Control applies”):

  • Everywhere (the default): every visitor whose browser sends it.
  • Only where the law requires it: visitors under the US opt-out model, visitors who get no banner (outside the banner’s region, or in a country you mapped to none), and visitors whose time zone places them in the US or nowhere (UTC, or the Atlantic/Reykjavik that Tor Browser and Firefox’s fingerprinting protection report), who may be in a state where the signal is binding. Everyone else who must opt in sees the banner and chooses.

The visitor can still give consent themselves: the privacy button opens the banner, which shows the notice about the signal (your “GPC notice” text) and offers every category on equal terms. Their choice is stored and recorded as theirs (gpcChoice in the consent object; the consent log shows “Yes, the visitor allowed cookies over it”) and counts on later pages, for __uspapi too. Kookiok.showBanner() opens the same banner, so a “Cookie settings” link in your footer gives the same choice; do not open it unprompted for these visitors (California does not let a business ask an opted-out consumer to opt in for 12 months).

Page code cannot give consent for the visitor: Kookiok.acceptAll() is refused while the signal applies, and the record says so (gpcOverridden). Over a choice the visitor made themselves it changes nothing and records nothing, so a site control wired to it cannot take their consent back either; Kookiok.rejectAll() and Kookiok.revokeConsent() still refuse. Nor is a script clicking the banner’s buttons the visitor’s choice: only a click or key press the browser dispatched (mouse, keyboard, assistive technology; Event.isTrusted) is theirs, and one a script makes (element.click(), dispatchEvent) is refused like Kookiok.acceptAll() while the signal applies, and recorded so. Writing the consent cookie yourself is not their choice either, and the consent script cannot tell it from one: leave the buttons and the cookie to the visitor.

The privacy popup labels the date of the decision on file: “Consent given” (your text) when it allows a category, otherwise a refusal, and a refusal the signal made says that it was made automatically because of Global Privacy Control. A visitor who gets no banner (outside the banner’s region, or in a country you mapped to none) decides nothing: on arrival every category is allowed, or refused if their browser sends the signal. The popup labels that date “Allowed by default” instead of “Consent given” (a refusal there is labelled as the signal’s); for the allowed default it is titled with your “Cookie Settings” text, shows no consent ID (nothing was recorded) and offers your “Customize settings” and “Reject all” buttons, and the cookie declaration shows each category as allowed by default. The consent object the events carry says implied: true. Every other decision, a withdrawal included, says implied: false; one an earlier version of the script stored says neither.