Add a cookie consent feature to CMS Made Simple
Previous article Next articleAn easy to customize feature to make your CMS Made Simple™ website comply with the EU Cookie Law. In this tutorial I will show you how easy it is to add a cookie consent bar to your CMSMS™ site. A visitor can accept or decline browser cookies. Depending on the visitors choice the, in example advertisement or social media code can be added to the site or not...
I will describe TWO methods:
1 - the "light" version, inform the visitor at the first visit at the website it uses cookies and you assume when the visitor doesn't change his browser settings he doesn't mind you set cookies at his device. Kind of a user friendly cookie wall...
2 - give the user a real choice which he can decline, so you can't set cookies at your website.
METHOD 1
How to use
Add to your HTML template, just after <body>
<div id="cookie_consent">
<p>We use cookies to ensure that we give you the best experience on our website. We also use cookies to ensure we show you advertising that is relevant to you.<br />
If you continue without changing your browser settings, we will assume that you are happy to receive all cookies on this website.<br />
<br />
<a class="accept_cookies">Continue</a><a href="cookies" class="more_info">More info</a></p>
</div>
{/if}
Put this jQuery code just before </body> in your HTML template
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
$(".accept_cookies").click(function () {
$("#cookie_consent").toggle("slow");
location.reload();
} );
$(document).ready(function() {
$.cookie("cookie_consent", "yes", { domain: document.domain, path: "/", expires: 1095 } );
} );
</script>
Note: This was updated in Dec 2023 to use the current domain: document.domain, rather than "yourdomain.com". Alternatively use "yourdomain.com" that needs to be changed for each website to be your domain name, without http, www, etc. ---^
Add to your Stylesheet
#cookie_consent {
background: #000;
position: fixed;
bottom: 0;
float: left;
z-index: 100000;
width: 100%;
padding: 30px 0;
/*filter: alpha(opacity=70);
opacity: 0.7;*/
}
#cookie_consent p {
text-align: center;
color: #fff;
font-size: 14px;
line-height: 20px;
margin: 0;
padding: 0 0 10px 0;
/*filter: alpha(opacity=100);
opacity: 1;*/
}
#cookie_consent a.accept_cookies {
margin: 0 5px;
padding: 5px;
color: #000;
font-weight: bold;
text-decoration: none;
cursor: pointer;
background: #6c0;
}
#cookie_consent a.more_info {
margin: 0 5px;
padding: 5px;
color: #000;
font-weight: bold;
text-decoration: none;
cursor: pointer;
background: #ccc;
}
[[/strip]]
Now you can add to your pages, module or HTML templates
{include file='cms_template:Advertisement'}
{include file='cms_template:Facebook'}
{/if}
METHOD 2
How to use
Add to your HTML template, just after <body>
<div id="cookie_consent">
<p>To be of better service to you this website makes use of cookies <a class="accept_cookies">Accept cookies</a><a href="cookies" class="more_info">More info</a><a class="decline_cookies">Decline cookies</a></p>
</div>
{/if}
Put this jQuery code just before </body> in your HTML template
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script>
$(".accept_cookies").click(function () {
$("#cookie_consent").toggle("slow");
$.cookie("cookie_consent", "yes", { domain: document.domain, path: "/", expires: 1095 } );
location.reload();
} );
$(".decline_cookies").click(function () {
$("#cookie_consent").toggle("slow");
$.cookie("cookie_consent", "no", { domain: document.domain, path: "/", expires: 1095 } );
location.reload();
} );
</script>
Note: This was updated in Dec 2023 to use the current domain: document.domain, rather than "yourdomain.com". Alternatively use "yourdomain.com" that needs to be changed for each website to be your domain name, without http, www, etc. twice ---^
Add to your Stylesheet
#cookie_consent {
background: #000;
position: fixed;
bottom: 0;
float: left;
z-index: 100000;
width: 100%;
padding: 15px;
filter: alpha(opacity=70);
opacity: 0.7;
}
#cookie_consent p {
text-align: center;
color: #fff;
font-size: 14px;
line-height: 20px;
margin: 0;
padding: 0;
filter: alpha(opacity=100);
opacity: 1;
}
#cookie_consent a.accept_cookies {
margin: 0 5px;
padding: 5px;
color: #000;
font-weight: bold;
text-decoration: none;
cursor: pointer;
background: #6c0;
}
#cookie_consent a.decline_cookies {
margin: 0 5px;
padding: 5px;
color: #000;
font-weight: bold;
text-decoration: none;
cursor: pointer;
background: #f33;
}
#cookie_consent a.more_info {
margin: 0 5px;
padding: 5px;
color: #000;
font-weight: bold;
text-decoration: none;
cursor: pointer;
background: #ccc;
}
[[/strip]]
Now you can add to your pages, module or HTML templates
{include file='cms_template:Advertisement'}
{include file='cms_template:Facebook'}
{/if}
{if isset($smarty.cookies.cookie_consent) and ($smarty.cookies.cookie_consent == 'no') }
<p>You can't use this function without cookies...</p>
{/if}
Other information
Do Not Track
Smarty can read the browsers DNT setting like this:
{include file='cms_template:Advertisement'}
{include file='cms_template:Facebook'}
{/if}
You could use this setting in combination with the logic above, like if a person doesn't want to be tracked don't give him the cookie consent question...
Analytics code
When you have analytics software at your website, you don't need to ask permission to set cookies when you don't store personal information.
You can do this by anonymizing the visitors' IP addresses, like 192.168.xxx.xxx or 192.168.100.xxx.
Do you use Matomo/Piwik you can go to Settings >> Privacy Settings and choose the recommended settings.
Using Google Analytics you can do similar by adding the following line to the GA code:
or depending on your Google Analytics version
Change to
The cookie information page
At your website you need to create a page with information about the cookies you use at your website. Link this page in the footer of your website.
More information
For more information about the cookie law requirements visit this website Aboutcookies.org
Also visit the website of your government and other related websites giving information of the Cookie Law in your own language.
DISCLAIMER
The EU Cookie Law is very complex and not all countries use it the same way.
I will not guarantee the methods described and the other information you will find here at this page are adequate for your website!
Working example
- I use method #1 here at cmscanbesimple.org
Comment Form
ReviewManager
ReviewManager
24 Comments
Hello Rolf!
Please help me :-) My cmsms is 1.9.4.3. Is it working with this? Furthermore can you tell me, where to install this? To html template? If yes, where to? The gdpr text appear over the webpage, but I can't click the buttons. THX
{if isset($smarty.cookies.cookie_consent) and ($smarty.cookies.cookie_consent == 'yes') } {include file='cms_template:Advertisement'} {include file='cms_template:Facebook'} {/if} {if isset($smarty.cookies.cookie_consent) and ($smarty.cookies.cookie_consent == 'no') } You can't use this function without cookies... {/if}
He Rolf,
I am stuck with a CMSms 1.x website. I have tried to follow method 2 here for the site but my website breaks when I add the '{if isset($smarty.cookies.cookie_consent) and ($smarty.cookies.cookie_consent == 'yes') }' code to the page.
Does this work for 1.x websites?
Best,
Mike
@Tamara
You can use the described method for free, without a license!
But you can always buy me a cup of coffee if you like it:
https://cmscanbesimple.org/buy-rolf-a-cup-of-coffee
Thanks, Rolf
Hello,
can I use this code for free? Or do I have to pay a licencing fee?
With Regards, Tamara
Sorry, I was working with the exported file. I imported the template, I deleted the cache and now everything works.
Thanks for everything.
Hi! Thanks for your work!.
Sorry by my english. I use de method 1, in localhost everything run perfect, but online the template no read the cookie.
I get Jquery to place the cookie but I can not get it to read immediately after I hit the accept button. But if I do a full refresh, the cookie is read and it does not show me the warning.
Thanks, Rolf ;)
@Johannes
Index.php??
You don't need to change files at the server!
You have to do this in the Admin panel: Layout >> Design Manager.
Look for your (default) Core::Page template, change that one.
@Rolf
I put it into the template's index.php, right below the opening body tag.
Best
Johannes
@Johannes
Where did you enter the Smarty line?
{if empty($smarty.cookies.cookie_consent)} {/if}
In a Core::Page template, module template, page or news/blog content area, or ...?
Grtz. Rolf
Hi there,
I'm completely new to CMSMS and when I follow your tutorial, all I get is
{if empty($smarty.cookies.cookie_consent)} {/if}
rendered on the top left of my page. I followed the instructions for method 2 - do I miss something?
Thanks in advance
Johannes
@Jake Hool
Method 2 described above does that!
You probably used method 1...
Hi,
When you click on another page despite not pressing continue the popup disappears, because of the condition of if cookie empty. Is there a way round this to make sure that the pop up stays on until you press accept? Hopefully it is just me being stupid and missing something obvious ;) Thanks
Hi Rolf,
I use Variant 1 and have found few small improvements:
A)The if-clause can be used also for the script part. Then the script is also only shown when needed and not always:
[Code]
{if empty($smarty.cookies.cookie_consent)} .. script .. {/if}
[/Code]
B)in the script-part you also may use ... domain: "{base_url}" instead of ... domain: "yourdomain.com" when you create a small userdefined tag with the content ...
[Code]
$base_url = $_SERVER['HTTP_HOST'];
return $base_url;
[/Code]
That way the script can also be used for sites with multiple domain name pointing to it ;-)
C) In the CSS part, the "filter/opacity parts" under "cookie_consent/cookie_consent p" should be enabled, I think.
D) Just for information: The whole thing generally is not working/shown if the user has certain plugins active in the browser. I was strugeling at first with that because "uBlock Origin" was active. It seams to disabled the information completely, so the user does not see it at all.
BR Hupi
@pWorker
Did you update the URL in the path line of the script?
domain: "yourdomain.com"
grtz. Rolf
Hi,
i removed the "path"-parameter and the repeatetly loading of cookie consent stops.
Have fun
Be sure the jQuery code doesn't conflict with the present code, like double jQuery library calls...
Does anyone know how or if this works on
BLOG1200
theme?
I've tried, but it seems to be messed up.
I tested both methods in a to 2.0.1.1 upgraded website.
Both work without any problem... You should have something not correct in your template or the way you implemented the tutorial
I just figured out that the problem seems to be that the overlay keeps reopening if I click "Continue". If I reload the page in the browser, though, the overlay disappears.
Since CMSMS 2.0.1
{if empty($smarty.cookies.cookie_consent)}
always is true, even if the cookie is set.
Why could that be?
It's definitely a jQuery problem. In the tutorial above I included the call to the jQuery library to get it working. Do you have this call already present in your website, you don't need to add it twice.
Also the sequence of the jQuery calls might be something to look at...
Hope this helps.
grtz. Rolf
Thanks for a simple solution for something we are forced to....
I placed the code on my site and it works for IE, however FF doesn't respond to the "continue" button. Does this sound familiar?
Thanks to Calguy for the browsers Do Not Track addition.