The Secure Privacy script loads a client-side JavaScript object named sp that exposes a set of public methods and events. Use these methods optionally to programmatically control the display of your consent banners, manage language switching, handle plugin consent, and listen for user interactions with Secure Privacy UI elements.
In this reference:
Direct Methods
The following methods can be called directly on the sp object to control banner visibility, manage language, and handle consent programmatically.
Method | Parameters | Description |
|---|
sp.showPrivacyBanner();
| None | Forces the privacy banner to show. |
sp.hidePrivacyBanner();
| None | Forces the privacy banner to hide. |
sp.showPrivacyPolicy();
| None | Forces the privacy policy popup to show. |
sp.hidePrivacyPolicy();
| None | Forces the privacy policy popup to hide. |
sp.showCookieBanner();
| None | Forces the cookie banner to show. |
sp.hideCookieBanner();
| None | Forces the cookie banner to hide. |
sp.showTrustWidget(true);
| true
| Forces the trust widget to show. |
sp.hideTrustWidget();
| None | Forces the trust widget to hide. |
sp.switchLanguage('lang_code');
| 'lang_code' — a 2-character language code, e.g. 'en' or 'sv'. Must be a language code already activated for the domain.
| Changes the language of all banners to the specified language. |
sp.switchLanguage('lang_code', 'cookieBanner');
| 'lang_code', 'cookieBanner'
| Changes the language of the cookie banner specifically. Overrides all default language settings and forces the cookie banner to show. |
sp.cookieBannerVisible();
| None | Returns true if the cookie banner is currently visible on screen; otherwise returns false. |
sp.savePluginConsent(pluginName);
| pluginName — string
| Saves consent for a specific plugin. The pluginName parameter must be a string matching the plugin name exactly. |
sp.checkConsent(pluginName);
| pluginName — string
| Checks whether consent has been given for a specific plugin. Returns a boolean: true if consent has been given, false if not. |
sp.allGivenConsents
| None | A JavaScript object containing all consents given by the current user. This value is available on the window object after the user has given consent. Can be used to check consent status and pass it to your server. |
Event Listeners
The following events can be subscribed to via window.addEventListener. Each event fires at a specific point in the Secure Privacy consent lifecycle and passes relevant data via evt.detail.
Event Name | Example Usage | Description and Returned Data |
|---|
sp_init
| window.addEventListener("sp_init", function(evt) { console.log(evt.detail); }, false);
| Fires when Secure Privacy has fully initialized. Use this event to safely execute any code that depends on the sp object being ready. |
sp_trust_badge_shown
| window.addEventListener("sp_trust_badge_shown", function(evt) { console.log(evt); });
| Fires when the trust badge is shown or hidden. Returns detail: true when the trust badge is shown; detail: false when it is hidden. |
sp_cookie_banner_shown
| window.addEventListener("sp_cookie_banner_shown", function(evt) { console.log(evt); });
| Fires when the cookie banner is shown or hidden. Returns detail: true when shown; detail: false when hidden. |
sp_cookie_banner_click
| window.addEventListener("sp_cookie_banner_click", function(evt) { console.log(evt); });
| Fires when a button on the cookie banner is clicked. Returns detail: "accept" when the user clicks Accept; detail: "reject" when the user clicks Reject. |
sp_cookie_banner_save
| window.addEventListener("sp_cookie_banner_save", function(evt) { console.log(evt); });
| Fires when the user's cookie banner choice is saved. Returns detail: "accept" when the Accept choice is saved; detail: "reject" when the Reject choice is saved. |
sp_privacy_banner_shown
| window.addEventListener("sp_privacy_banner_shown", function(evt) { console.log(evt); });
| Fires when the privacy banner is shown or hidden. Returns detail: true when shown; detail: false when hidden. |
sp_privacy_banner_click
| window.addEventListener("sp_privacy_banner_click", function(evt) { console.log(evt); });
| Fires when a button on the privacy banner is clicked. Returns detail: "accept" when the user clicks Accept; detail: "reject" when the user clicks Reject. |
sp_privacy_banner_save
| window.addEventListener("sp_privacy_banner_save", function(evt) { console.log(evt); });
| Fires when the user's privacy banner choice is saved. Returns detail: "accept" when the Accept choice is saved; detail: "reject" when the Reject choice is saved. |
sp_switch_lang
| window.addEventListener("sp_switch_lang", function(evt) { console.log(evt); });
| Fires when the user changes the language in a privacy banner. Returns an object: detail: { useLang: "en", category: "privacy-banner" }, where useLang is the selected language code and category identifies which banner triggered the language change. |
sp_unblock_[ServiceName]
| window.addEventListener("sp_unblock_Google_Analytics", function(evt) { console.log(evt); }, false);
| Fires when a specific service is unblocked following user consent. Replace [ServiceName] with the exact service name as shown in the Scan Report. Replace any spaces in the service name with underscores (_). For example, to listen for Google Analytics being unblocked, use the event name sp_unblock_Google_Analytics. |