Namespace cookie_consent
A cookie consent notice manager. The user can be in three states: no consent, implicit consent and explicit consent. No consent is the state the user starts in on first visiting the website. At that point a cookie is set that will indicate implicit consent if the user returns. When the user returns, they are considered to have given implicit consent. If the user then dismisses the cookie notification they move to explicit consent.
The actions to take when the user is in either of the consenting states
are determined by functions push
ed onto two lists:
self.cookie_consent_implicit
and self.cookie_consent_explicit
.
You therefore start by creating these lists as needed, if they don't exist. For example, the following creates the list of actions to take in case of implicit consent:
self.cookie_consent_implicit = self.cookie_consent_implicit || [];
You then push functions onto the lists:
self.cookie_consent_implicit.push(function () {
console.log("Action taken on implicit consent.");
})
Finally, you call cookie_consent.initialize()
to start the manager.
Note: explicit consent implies implicit consent. The function above will also be called for explicit consent.
UI Elements
The notice is assumed to be an initially invisible (display: none
) element
in the page with the id cookie-consent
. In it you should put the notice text
and a button that, when clicked, calls cookie_consent.consent()
.
Defined in: cookie-consent.js.
Constructor Attributes | Constructor Name and Description |
---|---|
A reference to the cookie consent notice manager.
|
Name | Description |
---|
Name | Description |
---|
Field Attributes | Field Name and Description |
---|---|
<static> int |
cookie_consent.COOKIE_EXPIRATION
Expiration of the consent cookie, in days.
|
<static> String |
cookie_consent.COOKIE_NAME
The name of the consent cookie, where we store the user's implicit or explicit consent.
|
<static> String |
cookie_consent.ELEMENT_ID
The ID of the element that shows the cookie consent notice.
|
<static> boolean |
cookie_consent.IMPLICIT_CONSENT
Enable implicit consent.
|
<static> String |
cookie_consent.state
State of the user's consent on page load.
|
Method Attributes | Method Name and Description |
---|---|
<static> |
cookie_consent.consent()
Give explicit consent.
|
<static> |
cookie_consent.initialize()
Call to initialize the cookie consent object.
|
<static> |
cookie_consent.retractConsent(reload)
Retracts consent and optionally reloads the current document.
|
<!-- Load Google Analytics if the user has given implicit consent. --> <head> <script> function manage_cookie_consent() { // Set up what happens in case of implicit consent self.cookie_consent_implicit = self.cookie_consent_implicit || []; self.cookie_consent_implicit.push(function () { // Implicit consent means we load Google Analytics var script = document.createElement ("script"); script.async = true; script.src = "https://www.googletagmanager.com/gtag/js?id=YOUR_ID_HERE"; document.body.appendChild (script); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'YOUR_ID_HERE'); }); // Do the cookie detection cookie_consent.initialize (); } </script> <script async="async" defer="defer" src="cookie-consent.js">/* */</script> </head> <body onload="manage_cookie_consent()"> <div id="cookie-consent" style="display:none;"> <p> example.com uses cookies to make your browsing experience better. By using the site you agree to our use of cookies. <button type="button" onclick="cookie_consent.consent()"> OK </button> </p> </div> . . . </body>
null
- no consent given, implicit or explicit"implicit"
- implicit consent given, by continuing to use the website after being shown the notice."explicit"
- explicit consent given by clicking on the "OK" button in the notice and dismissing it."rejected"
- consent explicitly rejected (via DNT)
onclick
event
handler in the "OK" button in the cookie consent notice.
- Parameters:
- {boolean} reload
- reload the current document