How to fire GTM Tags with CookieScript events?

How to fire GTM Tags with CookieScript events?

Nelson
Written by NelsonLast update 3 months ago

This article explains how to implement CookieScript built-in events in Google Tag Manager. You can use those events to fire Tags. 

If you are using Google Tag Manager (GTM) to include external third-party scripts to your website, it's very easy to start complying with cookie regulations using CookieScript build-in integration with the GTM.

You can also adjust Tag behavior based on the current Consent State variable. Or you can use build-in Google Consent Mode to change Tag firing conditions. 

I. How to block third-party scripts with Google Tag Manager?

Why do I have to block third-party scripts?

In short, third-party scripts that you include on your website might track your website, visitors, behavior, and those scripts that might require user consent to be included (unless they are strictly necessary for your website to work). You can read more about:

What are third-party cookies?

How to view, delete or block third-party cookies?

You can use build-in CookieScript GTM events to trigger certain scripts' inclusion. CookieScript also has custom events that can be used without GTM as documented here

Note: GTM events are fired once per page, so you can use them to include third-party scripts without worrying about including them more than once.

CookieScript fires following GTM events:

Event name

Firing condition

CookieScriptCategory-strict

fires if the user agreed to Strictly necessary cookies or declines all cookies (which is technically the same)

CookieScriptCategory-functionality

fires if the user agreed to Functionality cookies

CookieScriptCategory-targeting

fires if the user agreed to Target cookies

CookieScriptCategory-performance

fires if the user agreed to Performance cookies

CookieScriptCategory-unclassified

fires if the user agreed to Unclassified cookies

CookieScriptCategory-all

fires only if the banner is set up to be used without categories and the user just clicks "Accept all" button

Step 1. Add the Cookie-Script Consent Banner Code Snippet on GTM

On the GTM dashboard go to the Tags section and click the New button.

Go to the Choose a tag type to begin set-up..., then select Custom HTML, place your copied Cookie-Script code snippet, then go the Choose a trigger to make this tag fire... and select All Pages, finally click Save.

Step 2. Create a Cookie-Script trigger for your third-party tag.

  1. Go to the Triggers section and click the New button.

  2. Click the Choose a trigger type to begin set-up..., then select Custom Events.

  3. Choose Cookie-Script event, for example, CookieScriptCategory-targeting, which fires if the user selected/checked the "Targeting" checkbox category.

Step 3. Bind Cookie-Script trigger to appropriate Third-party code/library.

Go back to the Tag section and click the Choose a tag type to begin set-up..., then select Custom HTML, place your own or Third-party code snippet, then go the Choose a trigger to make this tag fire... select just created CookieScriptCategory-targeting trigger, and click Save.

Step 4. Test and preview your Website.

Now your Cookie-Script configuration and integration to GTM are done. Your website will from now unblock and load third-party scripts of the corresponding category only when the visitor select that one and gives their consent.

II. User consent change: how to fire Triggers in GTM?

You can 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.

You can set GTM to fire events when you use CookieScript cookie consent categories or Google consent mode categories.

1. Using CookieScript categories:

To fire events on GTM when CookieScript cookie consent categories are changed (you use a CookieScript template on GTM), perform the following steps:

  1. Go to Trigger Configuration and select Custom Event as a Trigger type.

  2. Enter ^CookieScriptConsentUpdated\[(?:.*?)\]$ as an Event name and check Use regex matching:

  3. 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.

    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 Trigger will fire if both targeting and performance categories were accepted 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

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.

To set up scripts to load when CookieScript cookie consent categories are changed, perform the following steps:

  1. Go to 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:

  2. In the Triggering field, select CookieScriptConsentUpdated as the Firing trigger.

Done, your preset script will load when CookieScript cookie consent categories are changed.

2. Using Google Consent Mode categories:

To fire events on GTM when Google consent mode categories are changed, perform the following steps:

  1. Go to Trigger Configuration and select Custom Event as a Trigger type.

  2. 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:

  1. Set the trigger to fire on All Custom Events.

  2. Go to Tag Configuration, select Custom HTML as a Tag type, and enter CookieScriptGoogleConsentUpdated as the console.log in the HTML field:

  3. In the Triggering field, select CookieScriptGoogleConsentUpdated as the Firing trigger.

Done, your preset script will load when Google consent mode categories are changed.

Did this answer your question?