Displaying an appropriate cookie dialog is one thing. Giving it a real meaning is another. Because asking permission alone is not enough. We also need to evaluate the responses: We must only store those cookies on our reader’s computers they — or the law — have consented to. A JavaScript function that implements this requirement sets the semantics of the cookie dialog. Based on such a function, we use properly managed cookies.
The plugin bs-cookie-settings itself only provides us with the cookie query. How to activate this, I had already described in a previous post. However, the bootScore developers leave the implementation of the corresponding semantics to the respective web designer. Here is a variant that can be freely reused:
Solution
- Download the JS cookie library from cdnpkg.com (or wherever) and place it (unpacked) under the name
js/js.cookie.min.js
into your child-theme folder. - In your file
functions.php
extend the functionbootscore_child_enqueue_styles()
by the line
wp_enqueue_script('js-cookie',get_stylesheet_directory_uri().'/js/js-cookie-min.js', false, '', true););
- Expand the file
js/custom.js
of your child theme in the following manner:
jQuery(function ($) {
$(document).ready(function(){
const bsCookieSettings='bs_cookie_settings';
const analytics = 'analytics';
const advertising = 'advertising';
const analyticDemoCookie='bsAnalyticCookie';
const advertisingDemoCookie='bsAdvertisingCookie';
const necessaryDemoCookie='bsNecessaryCookie';
const demoCookieValue='demo-cookie';
// alert("adding cookie writing algorithm");
const bsv=Cookies.get(bsCookieSettings);
if (bsv) {
const allowedCookies=JSON.parse(bsv);
// alert(allowedCookies.level);
if (allowedCookies.level.includes(analytics)) {
// alert("writing analytic cookies");
if (!(Cookies.get(analyticDemoCookie))) {
Cookies.set(analyticDemoCookie, demoCookieValue, { expires: 100, path: '/' });
};
};
if (allowedCookies.level.includes(advertising)) {
// alert("writing advertising cookies");
if (!(Cookies.get(advertisingDemoCookie))) {
Cookies.set(advertisingDemoCookie, demoCookieValue, { expires: 10, path: '/' });
};
};
// alert("writing necessary cookies");
if (!(Cookies.get(necessaryDemoCookie))) {
Cookies.set(necessaryDemoCookie, demoCookieValue, { expires: 14, path: '/' });
};
};
});
// Do your other stuff here
}); // jQuery End
Background
We could set and evaluate cookies with native JavaScript. Nevertheless, it’s easier with ready-made libraries. WordPress already brings jQuery with it. For using that, bootScore offers us a way to add custom JavaScript/jQuery functions to our bootScore child theme.
There used to be a real jQuery-Cookie-Plugin for cookie management. This has since been archived and migrated to an independent js-cookie-JavaScript library. To use that, we must download it and place it into the JavaScript folder of our child theme — under the name js/js.cookie.min.js
. As described above, we also must enforce the function bootscore_child_enqueue_styles()
of our file functions.php
to load that library.
Eventually, we should implement an algorithm for evaluating the cookie settings by expanding the file js/custom.js
. That algorithm should work like this:
- First, we try to read the cookie bootScore-Cookie-plugin stores under the name
bs_cookie_settings
. - If it doesn’t exist yet, our reader hasn’t agreed to use cookies. So we are not allowed to write any yet.1
- Once our reader has ‘confirmed’ the cookie dialog to whatever extent, the bs cookie plugin stores the cookie
bs_cookie_settings
. Its value contains a JSON object:
{ "level":
[ "necessary",
"analytics",
"advertising"
],
"revision":0,
"data":null,
"rfc_cookie":false
}
- Thus, we must parse that JSON object before we can — on the JavaScript level — access the list of allowed cookie groups via
allowedCookies.level
and use the methodincludes
of a list object to query which of the cookie groups necessary, analytics, and /or advertising our reader has allowed us to write. - And for each allowed group we now may write the corresponding cookies.2
And a last hint: JavaScript modifies pages dynamically. But the cache stores the respective results. Thus, sometimes it’s helpful if we delete the cache for getting the results of our modifications run.
And how does this …
… support our migration to bootScore? Well, besides her normal design work, the web-designer must deal with some legal requirements, as — for example — those of the DSGVO privacy, of having a cookie consent dialog and the respective semantic, of having a data privacy page, an imprint, an image reference page, and a FOSS compliance page. This post shall support you to manage your legal issues.
- Yes, formally we may write the technically necessary cookies without our reader’s consent. But before we do that, we must inform her that we are going to do so. And the only way to convince ourselves that she has indeed read it is to wait for the written cookie. [↩]
- whereby we refer to the legal permission for the technically necessary cookies [↩]