Consent for cookies JavaScript
As part of our eTrigue best practices series, we have developed a simple JavaScript to enable permission based tracking on your website. This is important if your company is marketing to and has a presence in European countries.
Cookie Consent JavaScript
The following describes how adding some basic JavaScript to a site can enforce the European cookie law, including the basic framework JavaScript to implement this. This assumes that this script is executed prior to any other cookie processing on the site, and this script is added to all pages on the site.
The process checks to see if cookies are enabled on the user's device:
IF cookies are NOT enabled, you may want to display a banner message on the site indicating that:
- Cookies are not enabled
AND
- User experience will be better if cookies are enabled.
In this event, a cookie dialog should NOT be presented to the user.
IF the “cookieconsent” cookie does not exist on the user’s device, the JavaScript will pop a dialog informing the user that the site uses cookies and they need to either:
- 'Accept' to continue
OR
- disable cookies on their device.
Your site must provide the script for the actual dialog, including the text you wish to present. The dialog should be implemented in such a way that it grays out the site, with the dialog being the only accessible element. Below is an example of text that could be presented in the dialog:
|
Company Cookie Policy
In accordance with European cookie laws, we notify all users visiting our site of our cookie policies --please review these. If you disable cookies in your device, please be aware that parts of the site may not function properly. By accepting this message, you consent to our use of cookies on the device you are using in accordance with our cookie policy. |
NOTE: eTrigue does not provide legal advice. Please consult with a lawyer or legal team on the messaging that is used on your own website.
If the user does not 'accept', they should not be able to access the site.
If the user 'accepts', the “cookiesconsent” cookie is added to their computer and the site continues processing as usual. From that point forward, the user will no longer see the prompt.
If the user removes the “cookiesconsent” cookie (or all cookies) in the future, the prompt will return.
IF cookies ARE allowed on the user’s device and the “cookieconsent” cookie already exists on the user’s computer, then the prompt will not be shown, and the site will cookie the user’s device as it would normally.
Example JavaScript
var consent = getCookie("cookieconsent"); if(consent === "") { // first check if cookies are even enabled setCookie("cookieconsent","shown",".yourdomain",1); consent = getCookie("cookieconsent"); if(consent !== "") { // only try to pop dialog if get a value. If we don't get a value that means cookies are disabled tryPopDialog(consent); } }else { tryPopDialog(consent); } functiontryPopDialog(consent) { if(consent !== "yes") { //do whatever you have to do to pop the dialog //when they click accept in your dialog call consentConfirmed() } } functionconsentConfirmed() { setCookie("cookieconsent","yes",".yourdomain",360); } functiongetCookie(c_name) { varc_start, c_end; if (document.cookie.length>0){ c_start=document.cookie.indexOf(c_name + "="); if (c_start!=-1){ c_start=c_start + c_name.length+1; c_end=document.cookie.indexOf(";",c_start); if (c_end==-1) c_end=document.cookie.length; returnunescape(document.cookie.substring(c_start,c_end)); } } return ""; } functionsetCookie( name, value, domain, daysTillExpiration){ varcookie_string = name + '=' + escape ( value ); var expires = new Date (); expires.setDate(expires.getDate() + daysTillExpiration); cookie_string += '; expires=' + expires.toGMTString(); cookie_string += '; path=' + escape ( '/' ); cookie_string += '; domain=' + escape ( domain ); document.cookie = cookie_string; }