Skip to content

Language

Documentation menu

Events

Subscribe to Cookiesh consent and banner events from JavaScript, the DOM or Google Tag Manager, with payloads and examples for each event.

Last updated October 29, 2026

Instead of polling cookies, subscribe to events. Cookiesh emits the same events in three ways, so you can listen wherever your code lives:

  • The JavaScript API: cookiesh.on(name, handler).
  • DOM events on window, prefixed with cookiesh:.
  • dataLayer pushes for Google Tag Manager.

Subscribing

on(event: string, handler: (detail) => void): () => void
off(event: string, handler: (detail) => void): void

on() returns a function that removes the listener:

import { cookiesh } from '@cookiesh/js';

const stop = cookiesh.on('consent:updated', ({ categories, changed }) => {
  if (changed.includes('analytics')) analytics.setEnabled(categories.analytics);
});

// Later, for example when a component unmounts
stop();

If you subscribe to consent:ready after it has already fired, the handler runs immediately with the current state. You do not need to worry about load order.

Event reference

Event When it fires Detail
consent:ready Once per page, when the consent state is known { categories, method, region }
consent:updated Whenever the visitor or your code saves a new choice { categories, changed, method, consentId }
banner:shown The first-layer banner appears { bannerVersion, region }
banner:hidden The banner closes, with or without a choice { reason }
preferences:opened The preference center opens {}
preferences:closed The preference center closes { saved }
script:unblocked Cookiesh runs a script or loads an iframe it was holding back { category, src }

In these payloads:

  • categories is an object with a boolean for each category: { necessary, preferences, analytics, marketing }.
  • changed lists the categories whose value changed, such as ['analytics'].
  • method is one of default, banner, preferences, api or gpc.
  • reason is accepted, rejected, saved or closed.

DOM events

Every event is also dispatched on window as a CustomEvent, which is handy for scripts that do not import the package:

<script>
  window.addEventListener('cookiesh:consent:updated', (event) => {
    console.log('New choice', event.detail.categories);
  });
</script>

Google Tag Manager

Cookiesh pushes two events to the dataLayer, so you can build triggers on them:

{
  event: 'cookiesh_consent_ready',
  cookiesh: { necessary: true, preferences: false, analytics: true, marketing: false, method: 'banner' }
}

{
  event: 'cookiesh_consent_updated',
  cookiesh: { necessary: true, preferences: true, analytics: true, marketing: false, method: 'preferences' }
}

Create a Custom Event trigger on cookiesh_consent_updated and a Data Layer Variable for cookiesh.marketing to fire non-Google tags only after consent. Google tags do not need this: they read the Consent Mode signals that Cookiesh sends automatically.

Server-side events

These events run in the visitor’s browser. To receive consent changes on your server, for example to sync them to a CRM, use webhooks.