# Secure Privacy JS API: sp Methods & Event Listeners

> Full reference for the Secure Privacy JavaScript API — including sp object methods to control banners and consent, and window event listeners for tracking user interactions.

- Canonical: https://support.secureprivacy.ai/article/developer-guide-for-javascript-methods
- Product: Consent Management
- Category: Integrations
- Published: 2026-03-06T07:13:00+00:00
- Updated: 2026-03-26T21:32:10.11+00:00
- Reading time: 3 minutes

---

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 —](#direct-methods) `sp.*` [calls](#direct-methods)
    
-   [Event Listeners —](#event-listeners) `window.addEventListener`
    

## Direct Methods

The following methods can be called directly on the `sp` object to control banner visibility, manage language, and handle consent programmatically.

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Method</p></th><th colspan="1" rowspan="1"><p>Parameters</p></th><th colspan="1" rowspan="1"><p>Description</p></th></tr><tr><td colspan="1" rowspan="1"><p><code>sp.showPrivacyBanner();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the privacy banner to show.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.hidePrivacyBanner();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the privacy banner to hide.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.showPrivacyPolicy();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the privacy policy popup to show.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.hidePrivacyPolicy();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the privacy policy popup to hide.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.showCookieBanner();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the cookie banner to show.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.hideCookieBanner();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the cookie banner to hide.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.showTrustWidget(true);</code></p></td><td colspan="1" rowspan="1"><p><code>true</code></p></td><td colspan="1" rowspan="1"><p>Forces the trust widget to show.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.hideTrustWidget();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Forces the trust widget to hide.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.switchLanguage('lang_code');</code></p></td><td colspan="1" rowspan="1"><p><code>'lang_code'</code> — a 2-character language code, e.g. <code>'en'</code> or <code>'sv'</code>. Must be a language code already activated for the domain.</p></td><td colspan="1" rowspan="1"><p>Changes the language of all banners to the specified language.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.switchLanguage('lang_code', 'cookieBanner');</code></p></td><td colspan="1" rowspan="1"><p><code>'lang_code'</code>, <code>'cookieBanner'</code></p></td><td colspan="1" rowspan="1"><p>Changes the language of the cookie banner specifically. Overrides all default language settings and forces the cookie banner to show.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.cookieBannerVisible();</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>Returns <code>true</code> if the cookie banner is currently visible on screen; otherwise returns <code>false</code>.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.savePluginConsent(pluginName);</code></p></td><td colspan="1" rowspan="1"><p><code>pluginName</code> — string</p></td><td colspan="1" rowspan="1"><p>Saves consent for a specific plugin. The <code>pluginName</code> parameter must be a string matching the plugin name exactly.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.checkConsent(pluginName);</code></p></td><td colspan="1" rowspan="1"><p><code>pluginName</code> — string</p></td><td colspan="1" rowspan="1"><p>Checks whether consent has been given for a specific plugin. Returns a boolean: <code>true</code> if consent has been given, <code>false</code> if not.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp.allGivenConsents</code></p></td><td colspan="1" rowspan="1"><p>None</p></td><td colspan="1" rowspan="1"><p>A JavaScript object containing all consents given by the current user. This value is available on the <code>window</code> object after the user has given consent. Can be used to check consent status and pass it to your server.</p></td></tr></tbody></table>

## 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`.

<table style="min-width: 75px;"><colgroup><col style="min-width: 25px;"><col style="min-width: 25px;"><col style="min-width: 25px;"></colgroup><tbody><tr><th colspan="1" rowspan="1"><p>Event Name</p></th><th colspan="1" rowspan="1"><p>Example Usage</p></th><th colspan="1" rowspan="1"><p>Description and Returned Data</p></th></tr><tr><td colspan="1" rowspan="1"><p><code>sp_init</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_init", function(evt) { console.log(evt.detail); }, false);</code></p></td><td colspan="1" rowspan="1"><p>Fires when Secure Privacy has fully initialized. Use this event to safely execute any code that depends on the <code>sp</code> object being ready.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_trust_badge_shown</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_trust_badge_shown", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the trust badge is shown or hidden. Returns <code>detail: true</code> when the trust badge is shown; <code>detail: false</code> when it is hidden.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_cookie_banner_shown</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_cookie_banner_shown", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the cookie banner is shown or hidden. Returns <code>detail: true</code> when shown; <code>detail: false</code> when hidden.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_cookie_banner_click</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_cookie_banner_click", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when a button on the cookie banner is clicked. Returns <code>detail: "accept"</code> when the user clicks Accept; <code>detail: "reject"</code> when the user clicks Reject.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_cookie_banner_save</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_cookie_banner_save", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the user's cookie banner choice is saved. Returns <code>detail: "accept"</code> when the Accept choice is saved; <code>detail: "reject"</code> when the Reject choice is saved.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_privacy_banner_shown</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_privacy_banner_shown", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the privacy banner is shown or hidden. Returns <code>detail: true</code> when shown; <code>detail: false</code> when hidden.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_privacy_banner_click</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_privacy_banner_click", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when a button on the privacy banner is clicked. Returns <code>detail: "accept"</code> when the user clicks Accept; <code>detail: "reject"</code> when the user clicks Reject.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_privacy_banner_save</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_privacy_banner_save", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the user's privacy banner choice is saved. Returns <code>detail: "accept"</code> when the Accept choice is saved; <code>detail: "reject"</code> when the Reject choice is saved.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_switch_lang</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_switch_lang", function(evt) { console.log(evt); });</code></p></td><td colspan="1" rowspan="1"><p>Fires when the user changes the language in a privacy banner. Returns an object: <code>detail: { useLang: "en", category: "privacy-banner" }</code>, where <code>useLang</code> is the selected language code and <code>category</code> identifies which banner triggered the language change.</p></td></tr><tr><td colspan="1" rowspan="1"><p><code>sp_unblock_[ServiceName]</code></p></td><td colspan="1" rowspan="1"><p><code>window.addEventListener("sp_unblock_Google_Analytics", function(evt) { console.log(evt); }, false);</code></p></td><td colspan="1" rowspan="1"><p>Fires when a specific service is unblocked following user consent. Replace <code>[ServiceName]</code> with the exact service name as shown in the Scan Report. Replace any spaces in the service name with underscores (<code>_</code>). For example, to listen for Google Analytics being unblocked, use the event name <code>sp_unblock_Google_Analytics</code>.</p></td></tr></tbody></table>
