You are currently viewing How To Implement Cookie Exceptions and Triggers in GTM

How To Implement Cookie Exceptions and Triggers in GTM

You may have already explored setting up exceptions in Google Tag Manager (GTM), which allows you to prevent tags from firing on specific pages or events. This is particularly useful when you would like to exclude events from firing under a certain condition.

Letโ€™s say, for instance, you need to avoid tracking certain tags in the EU to comply with GDPR. In the example below, weโ€™d like a tag to fire on the โ€˜addToCartโ€™ event, but not if itโ€™s occurring anywhere outside of the US.

You could apply this one exception trigger to every e-commerce tag that fires on an event that should be limited to a specific conditionโ€”US only, in this case.

This post will show you how to set up exceptions and triggers in GTM, specifically based on cookie values, like tracking preferences.

When to Use Cookie Triggers

Cookie values are especially useful to set exceptions because they can persist across pages or sessions. When configured correctly, cookie exceptions are also useful because theyโ€™re dynamic, can be set to expire, and new values can automatically replace the outdated ones.

Exceptions for Cookie Compliance

Setting cookie exceptions is becoming increasingly useful to comply with cookie policiesโ€”namely CCPA and GDPR. Youโ€™ve most likely added banners and updated privacy policies to allow users to opt-in or opt-out of cookie tracking. However, setting the banners and notifying your users is just part one of compliance. The userโ€™s privacy preferences must be respected across all analytics and marketing tracking configurations.

There are services out there that set banners for cookie compliance and even handle necessary tracking updates based on your usersโ€™ preferences. If youโ€™re using one of these services, you may not need to set additional GTM cookie exceptions based on tracking preferences. However, thereโ€™s undoubtedly a use-case for you to configure your own triggers and exceptions based on cookies.

Cookies for Session-Based Tag Firing

Although technically the same functionality, a cookie value can be set and conditionally used to fire tags within or across sessions. This is particularly useful when you want to fire a tag for an attribute that youโ€™d like to persist across pages or sessions.

The following are a few examples of when Iโ€™ve set session-based cookie triggers and exceptions:

  • X # of pages visited in one session
  • Viewed a certain page or product within that session
  • Viewed a pop-up
  • Saw a version of an A/B test
  • Interacted with a specific element
  • Converted during a previous session
  • Persist a dataLayer or DOM value across pages

Capturing Cookie Values in GTM

The method of capturing cookie values is the same regardless of whether you want to set cookie exceptions for compliance or employ cookie values to set conditional triggers. The difference might be how that cookie value is set.

1. Locate or Set Your Cookie Value

If youโ€™re using a platform for cookie compliance, you may be able to locate cookie values that are already being pushed. You can find existing cookie names and values by inspecting the page and navigation to Application > Cookies > Your Domain.

""

If you need to set a cookie based on an action, you can push a cookie script through a Custom HTML tag.

Copy the script below and set the cookieName, cookieValue, and number of days based on how long youโ€™d like your cookie to exist. You only need to edit this part in the last line of the script: "cookieName", "cookieValue", 30

You can set dynamic variables as the cookieValue to return conditionally based on the userโ€™s action. For example, you could create a lookup variable based on click text to return โ€œallowโ€ if a user accepts cookies or โ€œdenyโ€ if a user opts out through your banner.

You should check for that cookie name and value in the application panel once youโ€™ve pushed your script.

2. Create a Cookie Variable

Youโ€™ll create the reference variable once youโ€™ve pushed or located your cookie value in the application panel.

Create a new โ€˜1st Party Cookieโ€™ variable in GTM and set the cookie name as it appears in your cookie storage. Once this is created, you should be able to preview and see your cookie variable in GTM Preview Mode return the expected value.

""

3. Set Your Cookies in Triggers or Exceptions

For most trigger types, youโ€™re able to use one exception for each cookie value and apply it to every trigger that uses that same trigger type. For example, to add an exception to a tag that fires on a custom event, the exception needs to be based on a custom event; tags that fire on pageviews need to have exceptions based on pageviews.

If all of your marketing and analytics tags are based on event triggers, you could technically set up the one trigger below as the exception across all of your tags (using relevant correct cookie variable and values):

""

You can add your cookie to any trigger as you would with any other condition. The example below is based on a cookie named โ€œPagesPerSessionโ€ that returns the number of pages visited in that session as the cookie value. You could apply this condition as an โ€œandโ€ condition to any page view or even a specified action.

""

To Recap

You can get creative with how you refer to cookie values to fire certain tags. Fire remarketing pixels for users who visited a specific combination of pages within a session. Push a pop-up onto the page for users who almost purchased a product but navigated back. Track Google Analytics events to see when users submit a form after viewing particular pages and identify correlations.

Although these all could be deployed through most sites with development support, using cookies in GTM allows you to autonomously test and set up conditional tracking.

The post How To Implement Cookie Exceptions and Triggers in GTM appeared first on Portent.