|
/ Documentation /Developers/ Frontend JavaScript API & Events (surecookieManager)

Frontend JavaScript API & Events (surecookieManager)

If you write your own frontend code, a custom widget, a map, a tracking snippet, it should respect your visitors’ consent just like the plugins SureCookie manages. For that, SureCookie exposes a small JavaScript API on every page where the banner is active: the window.surecookieManager object for reading consent state, and a set of events for reacting when it changes.

This reference covers that supported surface, reading state, listening for changes, and opening the preferences UI.

Note: The manager object contains other methods used internally by the banner itself. Those are implementation details, they can change between releases, and they aren’t supported for external use. Stick to what’s on this page.

Before You Start

SureCookie’s script loads in the footer, so guard your code against running too early. The simplest pattern:

window.addEventListener( 'load', () => {
	if ( ! window.surecookieManager ) {
		return; // Banner disabled or SureCookie not active.
	}
	// Your code here.
} );

Reading Consent State

isAllowed( category )

isAllowed(category) returns whether that category is permitted for the current visitor. Before a choice, the resolved consent model sets the default. Global Privacy Control can still withhold categories, and a resource quarantined by Compliance Guard stays blocked until approved.

if ( window.surecookieManager.isAllowed( 'functional' ) ) {
	initMyMapWidget();
}

hasConsent()

Returns true once the visitor has made a choice, and false while they haven’t responded yet.

getConsent()

Returns the stored consent object, with the visitor’s per-category preferences, the action they took, and a timestamp, or null before any choice.

getConsentSummary()

getConsentSummary() returns a summary of the stored choice, including its timestamp, the allowed categories and effectivePreferences after Global Privacy Control is applied. Without a stored choice it returns “No consent given”.

Events

SureCookie dispatches these as CustomEvents on window:

EventFires whenevent.detail
surecookie_changedThe visitor makes or changes their choiceThe consent data
surecookie_appliedThe current consent state has been applied to the page, on load and after every changeThe consent data
surecookie_gcm_updatedConsent signals have been pushed to Google Consent ModeThe updated signals

Use surecookie_changed to react to decisions, and surecookie_applied when you also want to run for returning visitors whose stored consent is applied on page load:

window.addEventListener( 'surecookie_applied', () => {
	if ( window.surecookieManager.isAllowed( 'analytics' ) ) {
		startMyCustomAnalytics();
	} else {
		stopMyCustomAnalytics();
	}
} );

This pattern covers every case with one listener: first-time choices, changed preferences, and returning visitors.

Opening the Preferences UI

To let visitors reopen their cookie preferences from your own element, add the data-surecookie-reconsent attribute to any link or button:

<button data-surecookie-reconsent="true">Cookie Preferences</button>

This is the same mechanism the surecookie_reconsent_button shortcode uses. See Shortcodes Reference (Re-consent, Policy Content).

Don’t Set Consent Programmatically

The manager includes methods the banner itself uses to record, change, and clear consent. Don’t call them from your own code.

Important: Consent is only valid when it comes from the visitor’s own action on the banner. Recording or clearing consent programmatically undermines its validity and your compliance posture. If you need consent recorded differently, that’s a banner-configuration question, not a JavaScript one.

For testing your integration as a fresh visitor, use a private browsing window instead of clearing consent in code.

Good Practices

  • Gate your own scripts with isAllowed() and re-check inside a surecookie_applied listener, so they start and stop as consent changes.
  • For a standardized, plugin-neutral way to check consent, the WP Consent API works alongside this API; SureCookie keeps it in sync. See WP Consent API Compatibility.
  • Server-side customization belongs in hooks, not JS. See Developer Reference: Hooks & Filters.
Was this doc helpful?
What went wrong?

We don't respond to the article feedback, we use it to improve our support content.

Need help? Contact Support
Table of Contents
Scroll to Top