By default, the script blocker of the GDPR cookie consent plugin blocks the Google Tag Manager scripts entirely by assigning them to a single category.
That is, it is likely that you have configured different individual services like Google Analytics, Hotjar, Google Ads, etc. in Google Tag Manager. The plugin will block all individual services by assigning them to only a single category. If you are all fine with this default blocking, then you needn’t do anything other than just enabling the Google Tag Manager from the script blocker.
Manage different services configured via GTM
Now that you have different services within Google Tag Manager and need to assign different categories for each individual service, then read through the following to know how to achieve this.
Since the plugin works based on script blocking, you will need to slightly modify the event triggers. Let us see how this can be done.
The pre-requisites for using Google tag manager are:
- An account in Google Tag Manager (GTM),
- Created a website container in GTM
Note:
Disable Google Tag Manager from the GDPR Cookie Consent> Script blocker prior to inserting the code into the theme.
Following will detail the steps involved in assigning different categories for different services in GTM:
Create Triggers
Configure the Triggers in the following manner:
- From the GTM “Triggers” list, click “New”.
- Choose “Custom Event” and enter an event name for the trigger, for example for category analytics you can enter the event name as “cli_cookieconsent_analytics”, in the field “Event name”.
- Click “Save” to create the trigger and assign a name for the trigger, say Analytics.
- This is how the trigger configuration pane will look like:
The category associated in this manner will only set cookies if the visitor has accepted consent.
Insert GTM compatibility code snippet in the active theme
The next step would be to insert the code snippet into the active theme header file (header.php). The syntax of the code snippet for attaining GTM compatibility is as shown below:
In the above syntax, replace cookie_category_slug
with the actual category slug name and replace EventName
with the actual event name given while creating a trigger.
ie: For category analytics, you can insert the snippet in the following manner:
Note: If the category slug is a hyphenated word, then it must be replaced by an underscore. That is, for category non-necessary, the category slug can be entered as non_necessary.
For example, a sample code snippet with three categories (as specified above) will look like this:
Martin
March 9, 2023
Hey webtoffee,
in the meantime, have you managed to get this into the plugin as a built-in option?
Guido
January 4, 2022
Hi! How come the plugin doesn’t have a builtin option to add these events to the dataLayer? It seems quite feasible using wp_enqueue_script. Thanks.
Mark
February 28, 2022
Hi Guido,
Sorry for the delayed response as we missed your comment.
As for the request, we will add it to backlogs and will definitely consider it during future update.
Connagh Smith
May 11, 2021
I’ve added the following to header.php:
function CookieLawInfo_Accept_Callback() {
if (CLI.consent.advertisement) {
dataLayer.push({‘event’: ‘cli_cookieconsent_advertisement’});
}
if (CLI.consent.analytics) {
dataLayer.push({‘event’: ‘cli_cookieconsent_analytics’});
}
if (CLI.consent.functional) {
dataLayer.push({‘event’: ‘cli_cookieconsent_functional’});
}
if (CLI.consent.necessary) {
dataLayer.push({‘event’: ‘cli_cookieconsent_necessary’});
}
if (CLI.consent.others) {
dataLayer.push({‘event’: ‘cli_cookieconsent_others’});
}
if (CLI.consent.performance) {
dataLayer.push({‘event’: ‘cli_cookieconsent_performance’});
}
}
And configured corresponding triggers in Google Tag Manager.
However, when I check console, I’m getting:
jQuery.Deferred exception: Cannot read property ‘advertisement’ of undefined TypeError: Cannot read property ‘advertisement’ of undefined
I’m using the free version if that makes a difference…
Mark
May 15, 2021
Hi Connagh,
This type of implementation only works with the premium version of the plugin. In free version, you can only block the google tag script entirely assigned to a single category.
PS: We will update it in the article itself.
Keesjan
March 12, 2021
Hi, is this on the roadmap to include as default or switch on/off in the wp plugin? I like your cookiewall solution but it lacks integrated GTM datalayer support out of the box.
Mark
March 31, 2021
Hi Keesjan,
Thanks for the feedback. We will try to add it in the plugin.
Agnes
December 4, 2020
I am having troubles to get the info show in the data layer.
It would be great to have this included in the plugin, mapped with the categories we have, interacting directly in WP.
Jeff
October 17, 2020
Could I please know the callback function for Reject?
Jeff
October 16, 2020
I’m also looking for returning a callback for the Reject function. Does anyone have a solution?
Hans Palmers
October 16, 2020
How does the code snippet look like for multiple language sites?
The cookie categories are translated and therefore have different slugs, yet these slugs are needed to load the code snippet (?), to fire the events & trigger the scrips such as Universal Analytics.
Mark
October 17, 2020
As mentioned at the end of the WPML set up guide for the plugin(check in our documents section), please make the slugs of the translated categories the same.
Agnes LESAGE
October 15, 2020
Hi, it would be best if the code could be included in the header in WP plugin, and this way it could also be based on the categories we create.
Graham Carroll
October 7, 2020
Is it essential to add a cli_cookieconsent_necessary GTM trigger to fire the “necessary” cookies? Thanks.
Mark
October 9, 2020
That would depend on the whole configuration. We cannot give a yes or no answer to that.
Please submit a ticket via support for site specific assistance.
Jimena
May 5, 2020
By using this solution with GTM, do I have to purchase the plugin or this also applies for the free version? Or the free version doesn’t allow to integrate with GTM ?
Mark
May 6, 2020
It only works with the premium version. If you need to manage GTM with free version, you can add the script of GTM in head/body area under the Non-Necessary script area. There won’t be any granular control for the scripts inside GTM though.
Maxwell Hogan
January 4, 2020
I have a solution for obtaining reject. All good.
Andrew Wright
January 9, 2020
What is the solution?
Jeff
October 16, 2020
Hi, can you please share your solution?
Maxwell Hogan
January 3, 2020
What is the function for returning a callback of Reject as well?