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
| Event | Data | When |
|---|---|---|
ready | - | Script initialized, consent state resolved |
consent_update | ConsentState | The 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) |
error | Error | An OptSens error occurred, for example the configuration failed to load |
Example: wait for ready
OptSens.on('ready', function () {
if (OptSens.hasConsent('analytics')) {
// initialize analytics
}
});
Example: react to consent changes
OptSens.on('consent_update', function (consent) {
if (consent.analytics) {
// analytics was granted: initialize tracking
gtag('config', 'GA_ID');
} else {
// analytics was revoked
}
});