In some cases, your website scripts need to know if the user agreed to the cookie policy (for example to enable certain functionality that user agreed to). This is what events are used for: once the user makes/changes his cookie policy choice, an event is fired on the page. You can set up your website to listen to such events and perform necessary actions. Below you can find a list of events as well as some JavaScript code examples of how those events can be used.
List of Cookie-Script events
Those events are assigned to the window , but can as well be listened from a Cookie-Script code instance (more examples below). Events are fired each time user interacts with a banner, so it can fire multiple times on the same page.
CookieScriptAcceptAll
- fires when visitor accepts to all cookies used on the website, no matter if you use cookie categories or not.
CookieScriptAccept
- fires when visitor accepts some cookie categories, only fires when cookie categories are used. Accepted categories are passed together with event (see example below).
CookieScriptReject
- fires when visitor rejects all cookies, no matter if cookie categories are used or not.
CookieScriptClose
- fires when the visitor closes the cookie consent banner.
Cookie category events
In addition to the events above we have included category-specific events, this might be useful if you want to include some third-party functionality for a specific category. Those events are designed to fire once per page, so you can include some third-party code without checking if this code was included before. Events fire once visitor agrees to certain cookie category or on page load if a visitor agreed to cookie category on the previous page.
CookieScriptCategory-functionality
- fires if the user agreed to Functionality cookies
CookieScriptCategory-strict
- fires if the user agreed to Strictly necessary cookies
CookieScriptCategory-targeting
- fires if the user agreed to Targeting cookies
CookieScriptCategory-performance
- fires if the user agreed to Performance cookies
CookieScriptCategory-unclassified
- fires if the user agreed to Unclassified cookies
targeting
category in code, but rename it to Marketing when displaying to visitors.Cookie-Script functions
Cookie-Script also provides a variety of functions to control Cookie Consent popup. We have created a detailed explanation of how custom functions work.
Code examples
1. Listen to AcceptAll global event:
window.addEventListener('CookieScriptAcceptAll', function() {
//your code here
})
or listen to Cookie-Script instance event:
window.addEventListener('DOMContentLoaded', function(event) {
CookieScript.instance.onAcceptAll = function () {
// some code
}
})
2. Listen to Accept global event:
window.addEventListener('CookieScriptAccept', function(e) {
// e.detail will include this data:
// { categories: ['strict', 'functionality'] }
// some code
})
or use Cookie-Script instance event:
window.addEventListener('DOMContentLoaded', function(event) {
CookieScript.instance.onAcceptAll = function (data) {
// data will include this object:
// { categories: ['strict', 'functionality'] }
// some code
}
})