# JavaScript API

Source: https://cookiemark.com/docs/javascript-api

Most sites never need this page. Read it when your own code has to know what a visitor allowed, or when you want buttons and links of your own.

## Start here: wait for the state

The Cookiemark line sits first in your head and runs straight away, so `window.Cookiemark` exists for every script below it. The visitor's choice is known a little later, once the page has been read. Use the `ready` event to learn the state when the page loads, and `change` to follow what happens next.

```
Cookiemark.on('ready', function (detail) {
  if (detail.consent.statistics) {
    startMyAnalytics();
  }
});

Cookiemark.on('change', function (detail) {
  if (detail.consent.marketing) {
    loadChatWidget();
  }
});
```

Before your first publish the script holds nothing, and `window.Cookiemark` does not exist. Check for it before you call it, so your code also works on a domain that is still a draft.

A function you add with `on('ready')` after the state is known runs at once. The other events are not repeated for late listeners. Together, `ready` and `change` never miss anything.

Each event is also sent on `window` as a `CustomEvent` with the prefix `cookiemark:`. The data is in `event.detail`.

```
window.addEventListener('cookiemark:change', function (event) {
  console.log(event.detail.action, event.detail.consent);
});
```

## Properties

All properties are read-only and always show the current state.

| Property | Value |
| --- | --- |
| `Cookiemark.consent` | An object with `necessary`, `preferences`, `statistics` and `marketing`, each true or false. `necessary` is always true. |
| `Cookiemark.hasResponse` | True when the visitor made a choice that is still valid. |
| `Cookiemark.consented` | True when at least one optional category is allowed. |
| `Cookiemark.declined` | True when no optional category is allowed. |
| `Cookiemark.consentId` | The random ID of this visitor's consent, as in the consent log. Empty until they choose. |
| `Cookiemark.consentTime` | When the stored choice was made, in milliseconds since 1970. Zero without a choice. |
| `Cookiemark.allowsVendor(id)` | Whether a provider such as `meta-pixel` or `vimeo` may run: its category allowed and, with the choice per provider (paid), the provider not switched off. YouTube and Vimeo may when videos load without consent. |
| `Cookiemark.vendorsOff` | The providers the visitor switched off one by one, by id. |

`declined` is also true for a new visitor who has not answered yet, because nothing is allowed at that point. Check `hasResponse` as well when you need to know that the visitor said no.

```
if (Cookiemark.hasResponse && Cookiemark.declined) {
  // The visitor chose to reject everything.
}
```

## Methods

| Method | What it does |
| --- | --- |
| `show()` | Shows the banner. |
| `hide()` | Hides the banner without storing a choice. |
| `showPreferences()` | Opens the preferences window. `renew()` does the same. |
| `acceptAll()` | Allows every category, as the accept button does. |
| `denyAll()` | Refuses every optional category, as the reject button does. |
| `withdraw()` | Withdraws consent. The log records it as a withdrawal. |
| `submitConsent(choice)` | Stores a choice of your own. Categories you leave out count as refused. |
| `runScripts()` | Runs held scripts that are now allowed. Use it after you add marked scripts to the page yourself. |
| `googleConsentState()` | Returns what Cookiemark tells Google, signal by signal. |
| `on(name, fn)` and `off(name, fn)` | Adds or removes a function for an event. The name works with or without `cookiemark:`. |

```
Cookiemark.submitConsent({ preferences: true, statistics: true, marketing: false });
```

Every choice you store through the API is a real choice. It sets the cookie, goes into the consent log and releases or withdraws scripts, exactly like a click in the banner.

## Events

| Event | When |
| --- | --- |
| `ready` | Once per page view, when Cookiemark knows the state. |
| `change` | After every choice the visitor makes. |
| `accept` | After a choice that allows at least one category, and when the page loads for a visitor who did so before. |
| `decline` | After a choice that allows nothing, and when the page loads for a visitor who refused before. |
| `dialog-open` | When the banner or the preferences window appears. |
| `dialog-close` | When the preferences window closes. |
| `scripts-executed` | When Cookiemark has run the held scripts, on page load and after every choice. |

Every listener receives the same object. `consent` holds the four categories. `action` is `accept_all`, `deny_all`, `custom`, `withdraw` or `signal` after a choice, and empty otherwise. `first` is true when this is the visitor's first choice.

## The listener queue

`window.cookiemarkListeners` is a queue for code that may run before the Cookiemark script, such as a Tag Manager template. Push a function onto it. Cookiemark calls it once the choice is known, and again after every change.

```
window.cookiemarkListeners = window.cookiemarkListeners || [];
window.cookiemarkListeners.push(function (googleState, categories) {
  // googleState: { analytics_storage: 'granted', ad_storage: 'denied', ... }
  // categories:  { necessary: true, preferences: false, statistics: true, marketing: false }
});
```

The first argument follows the mapping on the **Google and GTM** tab. For a new visitor, Cookiemark calls the function once they choose.

## Markup the script understands

You can build your own buttons and links without writing JavaScript. Cookiemark handles a click on any element with these attributes.

| Markup | Effect |
| --- | --- |
| `data-cookiemark-action="accept"` | Allows everything. |
| `data-cookiemark-action="deny"` | Refuses everything. |
| `data-cookiemark-action="withdraw"` | Withdraws consent. |
| `data-cookiemark-action="preferences"` | Opens the preferences window. |
| `data-cmk-allow="marketing"` | Allows the named category and keeps the rest of the choice as it is. |
| A link to `#cookiemark-preferences` | Opens the preferences window. |

### A link to the preferences

A link opens the preferences window when its address ends in `#cookiemark-preferences`. That can be the bare `#cookiemark-preferences`, or a page address with it at the end, such as `/privacy#cookiemark-preferences`. Cookiemark catches the click, so the page does not reload.

In a website builder or CMS, add it to a menu as a link of your own. In WordPress that is a **Custom Link** in the menu, with `#cookiemark-preferences` as the URL and **Cookie settings** as the text.

The window opens on a click on the page. An address with `#cookiemark-preferences` opened straight from an email or another site shows the page, not the window.

Cookiemark also fills in elements with the visitor's own data. Because the browser does this, the page itself stays the same for everyone and is safe to cache.

| Markup | Cookiemark fills in |
| --- | --- |
| `data-cookiemark-consent-id` | The consent ID, as text. |
| `data-cookiemark-consent-date` | The date and time of the choice, as text. |
| `data-cookiemark-consent-state="marketing"` | The attribute `data-granted`, set to true or false. Style it with CSS. |

```
<a href="#cookiemark-preferences">Cookie settings</a>
<button type="button" data-cookiemark-action="withdraw">Withdraw my consent</button>
<button type="button" data-cmk-allow="marketing">Allow marketing cookies</button>

<p>Your consent ID: <span data-cookiemark-consent-id></span></p>
<p>Chosen on: <span data-cookiemark-consent-date></span></p>
```

A cookie policy page is a good place for the consent ID. A visitor who asks what they agreed to can quote it, and you look it up in the [consent log](https://cookiemark.com/docs/consent-log).

## The consent cookie

Cookiemark stores the choice in a first-party cookie called `cookiemark_consent`. Its value is URL-encoded JSON like this.

```
{"necessary":true,"preferences":false,"statistics":true,"marketing":false,
 "v":1,"id":"0b0f6c1e-3d0a-4c54-9f0e-2f1c8a4e7b11","ts":1791974400}
```

`v` is the consent version and `ts` the time of the choice in seconds. A cookie with an older consent version counts as no choice, so the banner shows again. Read the choice through the API rather than the cookie where you can, because the API also knows about regions and privacy signals.

## The Cookiebot API

Theme and plugin code written for Cookiebot often asks `Cookiebot.consent.marketing`, or listens for `CookiebotOnAccept`. Switch on **Answer to the Cookiebot JavaScript API** on the **Google and GTM** tab, and Cookiemark answers that code. It stays away when the real Cookiebot script is still on the page.

| Cookiebot | Answered by Cookiemark |
| --- | --- |
| `Cookiebot.consent.necessary`, `.preferences`, `.statistics`, `.marketing` | The current choice. |
| `Cookiebot.consent.stamp` | The Cookiemark consent ID. |
| `Cookiebot.consent.utc` | The time of the choice, in milliseconds. |
| `Cookiebot.consent.method` | `explicit` after a choice, `implied` outside your regions. |
| `Cookiebot.consented`, `.declined`, `.hasResponse` | As in Cookiemark, except that `declined` stays false until the visitor answers. |
| `show()`, `hide()`, `renew()`, `withdraw()`, `runScripts()` | The Cookiemark methods of the same name. |
| `submitCustomConsent(preferences, statistics, marketing)` | Stores that choice. |

The events `CookiebotOnLoad`, `CookiebotOnConsentReady`, `CookiebotOnAccept`, `CookiebotOnDecline`, `CookiebotOnDialogInit`, `CookiebotOnDialogDisplay` and `CookiebotOnTagsExecuted` fire on `window`. The older callback functions such as `CookiebotCallback_OnAccept` are called too.

> The Cookiebot API stores nothing and sets no cookie of its own. Cookiebot's `CookieConsent` cookie does not exist on a Cookiemark site, so code that reads that cookie directly needs changing.
