How To Implement Cookie Exceptions and Triggers in GTM

Oeuyown Kim, Analytics Strategist

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.

Oeuyown Kim, Analytics Strategist

Oeuyown Kim

Senior Analytics Strategist
Senior Analytics Strategist

Oeuyown is a senior analytics strategist with a passion for providing outside-the-box solutions to unique data challenges. With a background in SEO, she applies this knowledge to help client teams draw valuable connections between analytics data and actionable optimization strategies. Outside of analytics, Oeuyown practices yoga, attends music and art shows, and has a growing affinity for Seattle's food scene.

Start call to action

See how Portent can help you own your piece of the web.

End call to action
0

Leave a Reply

Your email address will not be published. Required fields are marked *

Close search overlay