JavaScript API

The script exposes the window.Optooki object. Use it to read consent from your code, open the banner from your own buttons, and react to every choice.

Reading consent

Optooki.hasConsent('analytics'); // true or false
Optooki.getConsent();            // null until the visitor has chosen

getConsent() returns the visitor’s latest choice:

{
  id: 'c_8f2…',            // anonymous ID, the one in the consent records
  choices: { necessary: true, preferences: false, analytics: true, marketing: false },
  action: 'custom',         // accept_all, reject_all, custom, or close (closed without accepting)
  consentVersion: 1,
  timestamp: 1767225600000  // time of the choice, in milliseconds
}

The categories are necessary, preferences, analytics and marketing.

Opening the banner, deciding

Optooki.showPreferences();            // open the preference centre
Optooki.show();                       // show the banner again
Optooki.hide();                       // close it without deciding
Optooki.acceptAll();
Optooki.rejectAll();
Optooki.setConsent({ analytics: true }); // change one category
Optooki.reset();                      // forget the choice and ask again

Reacting to a choice

Four events: ready, consent, show and hide.

// With the API: the function receives the consent state
const stop = Optooki.on('consent', (state) => {
  console.log('new choice', state.choices);
});
stop(); // stop listening

// Or with a browser event, useful if your code loads before Optooki
window.addEventListener('optooki:consent', (event) => {
  console.log(event.detail.choices);
});

Full example

Load an interactive map only when the visitor accepts functional cookies, now or later:

function loadMap() {
  if (document.getElementById('map').dataset.ok) return;
  document.getElementById('map').dataset.ok = '1';
  // … load the map library here
}

// The Optooki script is the first of the page: it already knows the stored choice.
if (window.Optooki?.hasConsent('preferences')) loadMap();

// And if the visitor accepts later:
window.addEventListener('optooki:consent', (event) => {
  if (event.detail.choices.preferences) loadMap();
});

In the same section : Developers

Your cookie banner, ready before your next coffee.

Create your account, customise the banner and add one line of code. Free for one site, no credit card required.