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
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:
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
For example, a sample code snippet with three categories (as specified above) will look like this: