How to fire GTM events when users change cookie consent?
User consent changed: how to fire triggers in GTM?
Google Tag Manager (GTM) could be used to fire events that depend on user interaction with CookieScript cookie banner. The previous guide explained how to configure GTM Tags to fire depending on the Consent State of Google Consent Mode.
This guide will explain how to set GTM to fire events when users change cookie consent. GTM will fire events when users agree with or reject cookies, change cookie categories, withdraw cookie consent, or make other changes related to cookie consent.
When users make changes to CookieScript cookie consent categories, you can set up other scripts to load.
To fire GTM Tags with CookieScript events, you should have a GTM account with installed CookieScript.
First, create your GTM account and a website container in GTM, and install the GTM container to your website, where you could add your tags. Read the guide on how to create a GTM account and install the GTM container.
Second, install CookieScript with GTM on your website.
There are two methods to fire triggers in GTM when users change their cookie consent choices:
Using CookieScript cookie consent categories, or
Using Google consent mode categories.
I. Firing triggers in GTM using CookieScript cookie consent categories
When you use a CookieScript template on GTM and want to fire events on GTM when CookieScript cookie consent categories are changed, perform the following steps:
Go to the GTM dashboard > CookieScript Container >Triggers, and press New.
Press Trigger Configuration and select Custom Event as a Trigger type.
Enter ^CookieScriptConsentUpdated\[(?:.*?)\]$ as an Event name and check Use regex matching:
Set the trigger to fire on All Custom Events or Some Custom Events.
If you check Some Custom Events, you can select which custom events should be fired.
Done, GTM will fire events, so you can be informed when users make changes to CookieScript cookie consent categories. You can react to these changes and set up other scripts to load. For example, if a user agrees with functionality cookies, you can load a script to show Ads.
Note: It is important to check the Use regex matching checkbox. The firing events functionality will not work if the checkbox is unchecked.
Note: In the example above, the Trigger will fire only if a user accepted both targeting and performance categories after consent change. If you need to fire a trigger when any of the following categories are accepted, use the following setup(separate categories with "|" symbol and use matches RegEx):
The list of supported CookieScript category names:
CookieScript category names |
---|
functionality |
targeting |
performance |
unclassified |
When users make changes to CookieScript cookie consent categories, you can set up other scripts to load.
To set up scripts to load when CookieScript cookie consent categories are changed, perform the following steps:
Go to your GTM account > Tags, and press the Tag you want to fire events.
Press Tag Configuration, select Custom HTML as a Tag type, and enter a script you need as the console.log (instead of Test event) in the HTML field:
In the Triggering field, select CookieScriptConsentUpdated as the Firing trigger.
Done, your preset script will load when CookieScript cookie consent categories are changed.
II. Firing triggers in GTM using Google Consent Mode categories:
To fire events on GTM when Google consent mode categories were changed, perform the following steps:
Go to the GTM dashboard > CookieScript Container >Triggers, and press New.
Press Trigger Configuration and select Custom Event as a Trigger type.
Enter CookieScriptGoogleConsentUpdated as an Event name and leave Use regex matching unchecked:
Done, GTM will fire events, so you can be informed when users make changes to Google consent mode categories. You can react to these changes and set up other scripts to load.
To set up scripts to load when Google consent mode categories are changed, perform the following steps:
Set the trigger to fire on All Custom Events.
Go to Tag Configuration, select Custom HTML as a Tag type, and enter CookieScriptGoogleConsentUpdated as the console.log in the HTML field:
In the Triggering field, select CookieScriptGoogleConsentUpdated as the Firing Triggers.
Done, your preset script will load when Google consent mode categories were changed.
You can also read the following guides:
How to fire GTM Tags with CookieScript events?
How to configure GTM Tags based on the Google Consent State?
If you are not using Google Consent Mode, you can also do the same using CookieScript Consent State variable.
Did this answer your question?