Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Cookies are a super important part of the web. Imagine the world without them: you wouldn’t be able to use your Facebook or Twitter. They help retain data throughout a user’s visit on a webpage, so naturally they’re an important component of web analytics, as well.

By default, Google Analytics and other analytics tools use cookies in order to track users’ behavior on your website. Unfortunately, I still see many marketers (working with GTM) suffering over not being able to fire Tags for returning visitors, or after 4 page views, etc.

But this shouldn’t be a problem, since you can use Google Tag Manager to set and retrieve data from cookies in fairly easy way. In this blog post, I’ll show you how to easily utilize cookies with Google Tag Manager and take your web tracking to the next level.

I remember when I was just starting to learn GTM. It was a giant leap for me, when I realized I could set and read cookies without developer’s help. It added much more flexibility and freedom to my tag management.

What if I need to fire a specific Pop-up if a visitor originally landed on my website from Adwords AND has already viewed 5 pages? Not a problem!

Why Should you Even Bother? Few Examples.

By setting and reading cookies with Google Tag Manager you can start creating much sophisticated triggers, such as:

  • A visitor has viewed 5 pages. Let’s show him a popup with special offer!
  • A visitor is actually a user in your SaaS (Software as a service) but is currently browsing without logging in. Nevertheless, he/she still has a cookie with value “registeredUser=true” which can be used as a condition for your trigger.
  • A visitor has landed on your website via affiliate link. With each page view, you can preserve affiliate ID in visitor’s cookie.
  • Do you have a pricing page with price calculator? Let’s attach calculator’s results (price) to that particular visitor. This way you’ll see how visitors with different price ranges are behaving on your site.

Pretty neat, huh?

 

So What’s the Plan?

  1. First, someone has to set the cookie. That might be either you (with help of Google Tag Manager), or a developer.
  2. Then, you need to create a 1st party cookie variable in GTM. It will look for cookie with particular name and make its value available in data layer. You will also be able to see it in Google Tag Manager’s Preview and Debug mode.
  3. Create a trigger where condition involves aforementioned 1st party cookie variable.
  4. Fire a tag based on a trigger from bullet point No. 3.
  5. Profit.

 

Set cookies with Google Tag Manager

In order to set a cookie, you’ll need to create a custom HTML tag. It contains Javascript code which defines cookie’s name, expiration time, etc.

In the script above, there are three variables that you should edit:

  • cookieName – you should give it a meaningful title, e.g. visitorFromAdwords, affiliateID, pageviewCount, etc.
  • cookieValue – In case of visitorFromAdwords possible values ca be true or false. affiliateID cookie’s value can be 2385437, and pageviewCount cookie’s – 5.
  • expirationTime defines when the cookie expires. 2592000 is 30 days converted to seconds (30 days * 24 hours * 60 minutes * 60 seconds * 1000 seconds). If you want the cookie to expire after the session ends (when browser window is closed), then remove expires=”+expirationTime+”; from the end of the script. Keep in mind, that Google Chrome still runs in the background when you close the browser window, thus the cookie will “live” longer.

Now, you need to decide when you want to set that cookie. Should this “set cookie” script fire when URL contains ?gclid= (meaning that a visitor landed via Adwords campaign)?

Say, we want to set cookies for those visitors, who have found our website via Google Adwords. In that case, we need to create a trigger with the following settings.

Trigger - Visitor from Adwords

When a person clicks any Google ad, a destination URL contains ?gclid=XXXXXX. That’s the reason behind this trigger’s condition.

 

Test The Cookie

After you created custom HTML tag with cookie-setting script and its trigger, next thing you should do is test the implementation with Preview and Debug mode. Make sure that the tag fires ONLY when URL contains ?gclid=. If you’re new to Preview and Debug mode, read this comprehensive guide.

Now, let’s check whether cookie is set correctly. There are two main ways to check cookies – browser’s built-in feature (developer tools) or a plugin.

Personally, I am using Chrome plugin called EditThisCookie (also available for Opera users). This really simple and intuitive extension enables me to quickly check which cookies are currently in use and what data do they contain.

If you haven’t already installed, download it here. If you’re using a different browser, here are some similar plugins:

Anyway, let’s get back to EditThisCookie. Once you have installed this extension, a cookie icon will appear next to the Menu icon (in Chrome).

EditThisCookie icon

Go to the website you’re currently working on, click EditThisCookie icon and you’ll see an expanded list of all cookies that are currently in use on that website. Look for cookie named visitorFromAdwords. This little piece of data will help us do the magic in further chapters of this blog post.

Edit this cookie expanded

 

Read The COOKIE

Now, we need to “teach” Google Tag Manager read the cookie and make it available as a variable. Go to your GTM account, open list of Variables and create a new user-defined variable with the following settings:

cookie variable - visitorFromAdwords

This variable searches for cookies with title visitorFromAdwords. If it spots one, GTM will read its value and make it available in the Variables tab of Preview and Debug console.

Cookie variable in Preview and Debug mode

This enables you to use visitorFromAdwords variable in any GTM tag or trigger, like in the example below:

trigger with cookie

 

Example – Fire a Tag on 3rd Page View

This is one of the most popular use cases of cookies with Google Tag Manager. For your convenience, I have prepared a GTM Recipe with a ready made script which counts number of page views and fires a trigger after 3. But if you’re eager to understand the details, continue reading.

Let’s Count Page Views

First of all, you’ll need to create a custom HTML tag which counts page views. Every time a page refreshes, this script searches for cookie pageviewCount and increments its value by 1.

The cookie (set by this Javascript) expires after 30 days. You can easily change its duration by editing  setCookie(“pageviewCount”, pageviewCount, 30).

Set this custom HTML to fire on all pages. Refresh Preview and Debug mode, open EditThisCookie plugin and check whether cookie was successfully saved. Now, refresh the page. Cookie’s value should increase to 2.

pageviewCount cookie

 

cookie variable and trigger in Google Tag Manager

Since we have created pageviewCount cookie, now it’s time to “teach” Google Tag Manager read the cookie and make it available as a variable. Go to your GTM account and open list of Variables. Create new user-defined variable with the following settings:

pageviewCount cookie variable in google tag manager

 

Next, go to Triggers and create a new Page view trigger with the following settings:

trigger with cookie

This trigger will fire when cookie’s value is greater or equal to 3.

That’s it! You can now assign this trigger to any tag you want, whether it is a pop-up or Google Analytics event tag, or whatever. It’s up to your imagination.

Spongebob imagination google tag manager

Few More Ideas

If you still don’t know how to leverage cookies in your marketing stack, here’s food for thought. I hope these examples will help you break the ice:

  • If you run an affiliate program, you already know that visitors come to your site via affiliate links (which contain unique affiliate ID). You can fetch that data from URL, set it as cookie and send to Google Analytics as Custom Dimensions. This way you’ll see how well/terrible affiliate traffic is converting. Also, you can identify which affiliate partner is driving the most engaged traffic.
  • If a person made a purchase on your site, save this fact as a cookie. Then, create a pageview trigger which blocks pop-ups (irrelevant to recent buyers).
  • If you run a SaaS business, ask developers to set various data about the user to cookies. Even if the user is logged out, you can still identify him/her visiting your website/blog/etc. and send that data to Google Analytics as custom dimensions. But be aware of PII limitations.

 

Final WOrds

So there you have it. Turns out it’s pretty easy to manage cookies with Google Tag Manager. First you need to fire a custom HTML tag which sets the the cookie with particular value. Then, you need to create 1st party cookie variable within GTM.

This variable’s value can be transferred to another tools (such as Google Analytics) or used as a firing condition within a trigger.

If you’re looking for ways how to fire a particular tag after 3 page views, or for those visitors, who have visited a particular page, then cookies should be your weapon of choice. Another solution is browser storage (localStorage and sessionStorage), but that’s a topic for another time.

Do you know more tricks how to use cookies with Google Tag Manager? Let me know in the comments.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn
Julius Fedorovicius

Product manager at Soundest. I am highly interested in Google Tag Manager, Google Analytics, Adwords, Email marketing, Email Deliverability, Digital Marketing in general. You can follow me on Twitter.