Skip to main content

Events

Subscribe to consent lifecycle events. A listener registered after an event has fired still runs, receiving the last emitted value automatically.

OptSens.on('event_name', function (data) {
// handle the event
});

OptSens.off('event_name', handler); // unsubscribe

Available events

EventDataWhen
ready-Script initialized, consent state resolved
consent_updateConsentStateThe visitor set or withdrew consent (accept, reject, custom or withdraw)
banner_show-The banner became visible
banner_hide-The banner was closed
scripts_executed-Blocked scripts were unblocked after consent
consent_expiring{ days_remaining: number }Consent expires within 30 days
route_change-SPA route change detected (see SPA support)
errorErrorAn OptSens error occurred, for example the configuration failed to load

Example: wait for ready

OptSens.on('ready', function () {
if (OptSens.hasConsent('analytics')) {
// initialize analytics
}
});
OptSens.on('consent_update', function (consent) {
if (consent.analytics) {
// analytics was granted: initialize tracking
gtag('config', 'GA_ID');
} else {
// analytics was revoked
}
});