GDPR Cookie consent plugin is a go-to solution to achieve GDPR compliance for your site. The script blocker services incorporated in this plugin allows users to optionally block the script rendered from pre-defined third-party services as per the user’s consent. This can be done either via the automatic script blocker or by manually adding scripts. Both these solutions are server-based, where the page request is expected to hit the server first for script blocking/unblocking services to take effect.
However, in certain cases, these server based solutions may not serve the purpose due to various reasons like extreme cache or conflict with third party plugins and so on. Here the pages will be served based on the cached content and the request will not actually reach the server. Therefore the blocking/unblocking may not behave as expected with the above solution. This can now be overcome by using a client-side script blocking/unblocking service which will do the job irrespective of where it is being served from (server or cache).
The example below will show you exactly how the scripts can be controlled at the client-side (via Javascript). However you need to know that the corresponding services should be disabled from the automatic script blocker; for e.g, if you plan to use a client-side script blocking service for the Facebook pixel you must ensure that this service is disabled under the Script blocker (automatic or extended via filters).
Javascript helper class function can be inserted into the active theme’s header.php. The code snippet below assumes that you have configured the Hotjar script under the Non-necessary and GA script under the Analytics category. Let us see how to disable these scripts via the Javascript code.
The function that primarily achieves this is CookieLawInfo_Accept_Callback()
, which is triggered when the user registers their consent. The if (CLI.consent.non_necessary)
checks if the consent has been received for the non-necessary category and executes the script entailing it and likewise for the if (CLI.consent.analytics)
.
Note:
- The scripts entered as a javascript helper class must be explicitly disabled from the script blocker screen. You can enable or disable the entire script blocker third party services all at once or disable individually using the toggle from the front end.
- While using the javascript code, insert the cookie/script category slug in the if condition; e.g, category analytics should be used in the following manner
if(CLI.consent.analytics)
- If the category slug is a hyphenated word, then it has to be replaced by an underscore; for e.g, if you have a category slug: non-necessary, you should replace the hyphen by an underscore and use as follows:
if(CLI.consent.non_necessary)
.
Vasco
February 8, 2022
The function CookieLawInfo_Accept_Callback is not called without a page reload. How can we configure it in such a way?
Thanks.
Mark
February 16, 2022
Hi Vasco,
Greetings from Webtoffee!
This function will run upon clicking ACCEPT button without a page reload. Feel free to revert if any issues.
Mauricio Galetto
September 5, 2022
According to my tests the first time is not called. Looking at your code I think that before the call to CookieLawInfo_Accept_Callback it checks “viewed_cookie_policy”, but this cookie is set afterwards:
this.cookieLawInfoRunCallBacks();
CLI_Cookie.set(CLI_ACCEPT_COOKIE_NAME, ‘yes’, CLI_ACCEPT_COOKIE_EXPIRE);
In cookieLawInfoRunCallBacks:
if (CLI_Cookie.read(CLI_ACCEPT_COOKIE_NAME) == ‘yes’) {
if (“function” == typeof CookieLawInfo_Accept_Callback) {
CookieLawInfo_Accept_Callback();
}
}
So the first time it didn’t fire.
Mark
October 10, 2022
Hi Mauricio,
Greetings from Webtoffee!
The script will not render during the initial load, and consent will not be received by the plugin, so there will be no Viewed cookie policy cookie.
The script will only be activated if the site visitor agrees to that category. When consent is granted, the viewed cookie policy cookie is set, and the function checks its value and based on it the script will fire.
Philip Myhill
August 13, 2021
“The code snippet below” suggests there should be a snippet of code from the header.php file but I can’t see it. Is there something missing from this page?
Shesna
August 14, 2021
Hi Philip,
We regret for the inconveniences caused. Due to a technical glitch, the code was not rendered on the page. We have updated the same. Thank you.
Olivier
May 18, 2021
Hi, is it possible to block other plugins through a PHP wp webhook? (The plugin is not shown in the blockable plugins in your plugin)
E.g. I use this chat-plugin which should be disabled unless the visitor gives his cookie consent.
If so, where can I find the documentation to do this?
Thanks! Olivier
Mark
May 21, 2021
Hi Oliver,
Our premium version plugin supports a filter to add more script to script blocker. Please find the filter and sample code here.
ElsaV
May 7, 2021
Hello,
Is it possible to prevent the deactivation of the necessary cookies category?
Thank you for your return,
E.
Mark
May 8, 2021
Hi,
By default, the necessary category is strictly enabled and therefore it cannot be deactivated. Make sure that the category slug of the necessary category is set to ‘necessary’.
Coding Nut
August 13, 2019
Can you please explain what a “Category” is? What’s the difference between “Non Necessary” and “Necessary”
Mark
August 14, 2019
Hi,
Refer the article link here to know more about the default categories.