Block Third-Party Cookies With Google Tag Manager (GTM) and CookieYes

CookieYes automatically blocks third-party cookies on your website prior to obtaining user consent. But if you use Google Tag Manager to add third-party scripts to your website, you will need to ensure that your site blocks all cookies that are set via GTM but have not been consented to use.

If, for instance, you have added a Functional cookie script to your GTM account, and if a user visiting your website gives consent to only Analytics cookies, ideally your site should block all the other types of cookies.

But in this case, the Functional cookies will also possibly get triggered. The reason behind this is that when you enable Analytics cookies, it will trigger the Google Tag Manager, which would, in turn, trigger all the other cookie scripts set via GTM.

To avoid this issue, you are required to create an appropriate custom event trigger in your GTM account. Here’s an example of how this can be done!

Creating a custom event trigger in GTM

The following are the steps to create a custom event trigger in Google Tag Manager that helps CookieYes block third-party cookie scripts on your website (set via GTM) prior to receiving user consent.

Note
For better understanding, throughout this guide, let’s consider the case of blocking a Functional cookie script (added to your GTM account) until user consent is obtained.

Step 1: Create a user-defined variable

  1. Sign in to your Google Tag Manager account > Click the required Container Name from your Accounts. GTM-Container Name
  2. From the left panel, select Variables > In the User-Defined Variables section, click New. GTM-Variables-User-defined Variables
  3. Name the variable > Click Variable Configuration > Choose variable type as 1st Party Cookie from under Page Variables. GTM-Variable Configuration

    Note
    1st Party Cookie variable allows Google Tag Manager to access the first-party cookies created by your website. 1st Party Cookies

    Cookies installed by the site — displayed under the Application tab of the Developer console

  4. In the Cookie Name text box, enter the name of that cookie for which you need to retrieve the value.
    e.g. cookieyes-functional
  5. Click Save to save the variable. GTM-Saving Trigger Variable

Step 2: Create a custom event trigger

  1. From the left panel of your GTM account, select Triggers > In the Triggers section, click New. GTM-New Trigger
  2. Name the Trigger > Click Trigger Configuration > Choose trigger type as Custom Event. GTM-Custom Event Trigger
  3. In the Event Name text box, enter the .* regular expression and check the Use regex matching option.
  4. Under This trigger fires on, choose Some Custom Events.
  5. Under Fire this trigger when an Event occurs and all of these conditions are true, set up a trigger condition.
    e.g. Tawk.to trigger variable (i.e. the name of the user-defined variable that you have created in Step 1), equals, and yes.
  6. Click Save to save the custom event trigger. GTM-Custom Event Trigger

Step 3: Add the custom event trigger to the required custom HTML tag

  1. From the left panel of your GTM account, select Tags > Click the Tag name that needs to be fired. GTM-Tags
  2. Click Triggering. GTM-Tags-Triggering
  3. Choose the custom event trigger that you have created in Step 2. GTM-Tags-Choose a Trigger
  4. Click Save. Adding custom event trigger to custom HTML tag in GTM

Step 4: Submit changes

  1. Click the Submit button at the top right corner of your GTM account. GTM-Submit Tag
  2. Finally, click Publish to make this tag live on your website. GTM-Publish Tag

Once you have done these steps, your site visitors will not have Functional cookies running in the background after enabling Analytics cookies and disabling Functional cookies.

Now, take a look at how a website’s cookie installation differs before and after adding the custom event trigger in GTM.

Website before adding the custom event trigger in GTM

Before adding the custom event trigger in GTM

Website after adding the custom event trigger in GTM

After adding the custom event trigger in GTM