Consent events
One Privacy fires a single custom event whenever the visitor's consent state changes. This page covers when it fires, what it carries, and how to use it.
The event
Name: one-privacy-consent-updated
Target: window
Type: CustomEvent
When it fires
On widget initialization (after the consent cookie is read or initialized).
When the visitor clicks Accept All or Reject All.
When the visitor clicks Confirm My Choices in the Cookie Settings popup.
When the visitor reopens preferences via the floating button and changes a setting.
In short: any time window.onePrivacyCookieGroups could have changed.
Listening
window.addEventListener('one-privacy-consent-updated', (event) => {
// Read the latest accepted categories from the global variable.
const accepted = window.onePrivacyCookieGroups || '';
console.log('Consent updated:', accepted);
// The event also carries any custom detail data set by the widget.
console.log('Event detail:', event.detail);
});
The handler is called every time the event fires. There's no de-duplication, so if you only want to act once, track that yourself.
The detail payload
event.detail is an object. Most of the time it's empty, because the cookie state already lives on window.onePrivacyCookieGroups.
Future versions may carry richer data (the previous state, the visitor's region, etc.). Treat any new keys as additive.
A practical pattern
A common pattern: gate downstream scripts behind consent.
let analyticsLoaded = false;
function loadAnalyticsIfAllowed() {
if (analyticsLoaded) return;
const accepted = window.onePrivacyCookieGroups || '';
if (accepted.includes('C0003')) {
analyticsLoaded = true;
// Load your analytics tag
const s = document.createElement('script');
s.src = 'https://example.com/analytics.js';
document.head.appendChild(s);
}
}
// Run at first opportunity
loadAnalyticsIfAllowed();
// And again whenever consent changes
window.addEventListener('one-privacy-consent-updated', loadAnalyticsIfAllowed);