Blocking scripts at the client side(Javascript)

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 too 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 Hotjar script under 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 excecutes the script entailing it and likewise for the if (CLI.consent.analytics).


Note:

  1. 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.
  2. Script blocker enable or disable
    Script blocker enable or disable
  3. 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) 
  4. 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).

Leave a Reply

Your email address will not be published. Required fields are marked *