-
Notifications
You must be signed in to change notification settings - Fork 4
Control programmatically
The plugin exposes the pressidiumCookieConsent
object on the window
object.
Show the consent modal.
pressidiumCookieConsent.show(optionalDelay, createModal)
-
optionalDelay
— (optional)number
-
createModal
— (optional)boolean
Pass true
to the createModal
parameter to create a modal if it doesn’t exist.
Examples:
// Immediately show the consent modal
pressidiumCookieConsent.show();
// Wait 250 ms, then show the consent modal
pressidiumCookieConsent.show(250);
// Show the consent modal. If it doesn't already exist, create one
pressidiumCookieConsent.show(0, true);
Hide the consent modal.
pressidiumCookieConsent.hide()
Example:
// Hide the consent modal
pressidiumCookieConsent.hide();
Show the settings modal.
pressidiumCookieConsent.showSettings(optionalDelay)
-
optionalDelay
— (optional)number
Examples:
// Immediately show the settings modal
pressidiumCookieConsent.showSettings();
// Wait 250 ms, then show the settings modal
pressidiumCookieConsent.showSettings(250);
Keep in mind that by appending the data-cc="c-settings"
attribute to a button (or link) within the consent modal, users can access the settings modal.
<!-- button with the `data-cc="c-settings"` attribute in the consent modal -->
<button type="button" aria-label="View cookie settings" data-cc="c-settings">Cookie settings</button>
Hide the settings modal.
pressidiumCookieConsent.hideSettings()
Example:
// Hide the settings modal
pressidiumCookieConsent.hideSettings();
Accept one (or more) cookie categories.
pressidiumCookieConsent.accept(acceptedCategories, optionalRejectedCategories)
-
acceptedCategories
—string
orstring[]
-
optionalRejectedCategories
— (optional)string[]
Examples:
// Accept all categories
pressidiumCookieConsent.accept('all');
// Accept none (reject all)
pressidiumCookieConsent.accept([]);
// Accept only the `analytics` category
pressidiumCookieConsent.accept('analytics');
// Accept the `analytics` and `targeting` categories
pressidiumCookieConsent.accept(['analytics', 'targeting']);
// Accept all currently selected categories inside modal
pressidiumCookieConsent.accept();
// Accept all except of the `analytics` category
pressidiumCookieConsent.accept('all', ['analytics']);
// Accept all except of the `analytics` and `targeting` categories
pressidiumCookieConsent.accept('all', ['analytics', 'targeting']);
Check if the specified category is allowed (i.e. the user has given consent for that category).
pressidiumCookieConsent.allowedCategory(categoryName)
-
categoryName
—string
Example:
if (pressidiumCookieConsent.allowedCategory('analytics')) {
// User has accepted the cookie consent with the 'analytics' category enabled
}
Check if the cookie with the specified name exists (and has a non-empty value).
pressidiumCookieConsent.validCookie(cookieName)
-
cookieName
—string
Example:
if (pressidiumCookieConsent.validCookie('_gid')) {
// the `_gid` cookie is set
}
Erase the cookies with the specified name(s).
pressidiumCookieConsent.eraseCookies(cookieNames, optionalPath, optionalDomains)
-
cookieNames
—string[]
-
path
— (optional)string
-
domains
— (optional)string[]
Examples:
// Erase `cc_cookie` if it exists
pressidiumCookieConsent.eraseCookies(['cc_cookie']);
// Erase these 2 cookies
pressidiumCookieConsent.eraseCookies(['cookie1', 'cookie2']);
// Pass the optional `path` parameter
pressidiumCookieConsent.eraseCookies(['cc_cookie'], '/demo');
// Pass the optional `path` and `domains` parameters
pressidiumCookieConsent.eraseCookies(['cc_cookie'], '/demo', [location.hostname]);
Load a previously blocked script.
pressidiumCookieConsent.loadScript(path, callback, optionalCustomAttributes)
-
path
—string
-
callback
—function
-
optionalCustomAttributes
—object[]
Examples:
pressidiumCookieConsent.loadScript('https://www.google-analytics.com/analytics.js', () => {
// Script loaded, do something
});
pressidiumCookieConsent.loadScript('https://www.google-analytics.com/analytics.js', () => {
// Script loaded, do something
}, [
{ name: 'id', value: 'ga_id' },
{ name: 'another-attribute', value: 'value' },
]);
A custom event that gets triggered:
- At the first moment that consent is given
- After every page load, if consent ("accept" or "reject" action) has already been given
window.addEventListener('pressidium-cookie-consent-accepted', (event) => {
// `cookie` contains the current value of the consent cookie
const { cookie } = event.detail;
});
A custom event that gets triggered:
- When the user changes their preferences (i.e. accepts/rejects a cookie category), only if consent has already been given
window.addEventListener('pressidium-cookie-consent-accepted', (event) => {
// `cookie` contains the current value of the consent cookie
// `changedCategories` is an array of the categories whose state (accepted/rejected) just changed
const { cookie, changedCategories } = event.detail;
});