How to Set Up GTM Cookie Tracking (and Better Understand Content Engagement)

The extra you perceive the behaviour of your customers, the higher you may market your services or products — which is why Google Tag Manager (GTM) is a marketer’s finest pal. With built-in tag templates, resembling scroll depth and click on monitoring, GTM is a strong device to measure the engagement and success of your content material. 

If you’re solely counting on tag templates in GTM, or the sometimes limiting out-of-box Google Analytics, then you might be lacking out on insights that transcend regular engagement metrics. Which means you might be getting an incomplete story out of your knowledge.

This put up will educate you ways to get much more perception by organising cookies in GTM. You’ll find out how to tag and monitor a number of web page views in a single session, monitor a particular set variety of pages, primarily based on particular on-page content material parts, and perceive how customers are partaking together with your content material so you may make data-based choices to higher drive conversions.

Example use case

I just lately labored with a shopper that wished to higher perceive the habits of customers that landed on their weblog content material. The major barrier they confronted was their URL construction. Their content material didn’t reside on logical URL buildings — they positioned their goal key phrase straight after the basis. So, as an alternative of, their URL construction appeared like

You can use superior segments in Google Analytics (GA) to monitor any variety of metrics, however in the event you don’t have a logically outlined URL, then monitoring and measuring these metrics turns into a handbook and time-consuming apply — particularly when there’s numerous pages to monitor.

Fortunately, leveraging a customized cookie code, which I present under, helps you to reduce by way of that point, requires little implementation effort, and might floor highly effective insights:

  1. It can point out that customers are engaged together with your content material and your model.
  2. The saved knowledge could possibly be used for content material scoring — if a web page is included within the three pages of an occasion it could be extra precious than others. You might want to goal these pages with extra upsell or cross-sell alternatives, in that case.
  3. The identical scoring logic might apply to authors. If blogs written by sure authors have extra web page views in a session, then their writing type/matters could possibly be extra partaking and you might have considered trying to additional leverage their content material writing abilities.
  4. You can construct remarketing viewers lists to goal these seemingly engaged customers to align with your enterprise targets — people who find themselves extra engaged together with your content material could possibly be extra doubtless to convert.

So, let’s briefly talk about the anatomy of the customized code that you’ll want to add to set cookies earlier than we stroll by way of a step-by-step implementation information.

Custom cookie code

Cookies, as everyone knows, are a small textual content file that’s saved in your browser — it helps servers keep in mind who you’re and its code is comprised of three parts:

  • a name-value pair containing knowledge
  • an expiry date after which it’s now not legitimate
  • the area and path of the server it needs to be despatched to.

You can create a customized code to add to cookies to show you how to monitor and retailer quite a few web page views in a session throughout a set of pages.

The code under kinds the inspiration in organising your cookies. It defines particular guidelines, such because the occasions required to set off the cookie and the expiration of the cookie. I am going to present the code, then break it up into two elements to clarify every phase.

The code

perform createCookie(identify,worth,hours) 
if (doc.questionSelectorAll("CSS SELECTOR GOES HERE"").size > zero) 

Part 1

perform createCookie(identify,worth,hours) 


This perform, because the identify implies, will create a cookie in the event you specify a reputation, a price, and the time a cookie needs to be legitimate for. I’ve specified “hours,” however if you’d like to specify “days,” you’ll want to iterate variables of the code. Take a peek at this nice useful resource on organising cookies.

Part 2

if (doc.questionSelectorAll("CSS SELECTOR GOES HERE").size > zero) 
else if (y == 2) 
else if (y == three) 
if (newCount == four) {

Now that we’ve a primary understanding of the script, we are able to use GTM to implement every little thing.

First, you’ll want the arrange the next “Tags,” “Triggers”, and “Variables”:


Custom HTML tag: comprises the cookie script

Event tag: fires the occasion and sends the info to GA after a 3rd pageview is a session.


Page View set off: defines the situations that may fireplace your Custom HTML Tag.

Custom Event set off: defines the situations that may fireplace your occasion.


First Party Cookie variable: This will outline a price set off wants to consider whether or not or not your Custom HTML tag ought to fireplace.

Now, let’s stroll by way of the steps of setting this up in GTM.

Step 1: Create a customized HTML tag

First, we’ll want to create a Custom HTML Tag that may comprise the cookie script. This time, I’ve added the CSS selector, under:

 #content material > div.put up.type-post.status-publish.format-standard.hentry > div.entry-meta > span > span.writer.vcard > a

This matches authors on Distilled’s weblog pages, so that you’ll need to add your individual distinctive selector.

Navigate to Tags > New > Custom HTML Tag > and paste the script into the customized HTML tag field.

You’ll need to guarantee your tag identify is descriptive and intuitive. Google recommends the next tag naming conference: Tag Type – Detail – Location. This will enable you to simply determine and type associated tags from the overview tag interface. You may also create separate folders for various tasks to preserve issues extra organized.

Following Google’s instance, I’ve referred to as my tag Custom HTML – three Page Views Cookie – Blog.

Once you’ve created your tag, keep in mind to click on save.

Step 2: Create a set off

Creating a set off will outline the situations that may fireplace your customized HTML tag. If you need to study extra about triggers, you may learn up on Simo Ahava’s set off information.

Navigate to Triggers > New > PageView.

Once you’ve clicked the set off configuration field, you’ll need to choose “Page View” as a set off sort. I’ve additionally named my set off Page View – Cookie Trigger – Blog, as I’m going to arrange the tag to fireplace when customers land on weblog content material.

Next, you’ll need to outline the properties of your set off.

Since we’re counting on the CSS selector to set off the cookie throughout the location, choose “All Page Views”.

Once you’ve outlined your set off, click on save.

Step three: Create your variable

Just like how a Custom HTML tag depends on a set off to fireplace, a set off depends on a variable. A variable defines a price set off wants to consider whether or not or not a tag ought to fireplace. If you need to study extra about variables, I like to recommend studying up on Simo Ahava’s variable information.

Head over to Variables > User-Defined Variables > Select 1st Party Cookie. You’ll additionally discover that I’ve named this variable “NumberOfBlogPagesVisited” — you’ll need this variable identify to match what’s in your cookie code.

Having chosen “1st Party Cookie,” you’ll now want to enter your cookie identify. Remember: the cookie identify wants to replicate the identify you’ve given your cookie within the code. I named my cookie BlogPagesVisited, so I’ve replicated that within the Cookie Name area, as seen under.

Step four: Create your occasion tag

When a person triggers a third-page view, we’ll need to have it recorded and despatched to GA. To do that, we’d like to arrange an “Event” tag.

First, navigate to Tags > New > Select Google Analytics – Universal Analytics:

Once you’ve made your tag sort “Google Analytics – Universal Analytics”, make certain monitor sort is an “Event” and also you identify your “Category” and “Action” accordingly. You may also fill in a label and worth if you want. I’ve additionally chosen “True” within the “Non-interaction Hit” area, as I nonetheless need to monitor bounce price metrics.

Finally, you’ll need to choose a GA Setting variable that may cross on saved cookie data to a GA property.

Step 5: Create your set off

This set off will reference your occasion.

Navigate to Trigger > New > Custom Event

Once you’ve chosen Custom Event, you’ll need to make sure the “Event name” area matches the identify you will have given your occasion within the code. In my case, I referred to as the occasion “3 Blog Pages”.

Step 6: Audit your cookie in preview mode

After you’ve chosen the preview mode, you must conduct an audit of your cookie to guarantee every little thing is firing correctly. To do that, navigate to the location you the place you’ve arrange cookies.

Within the debugging interface, head on over to Page View > Variables.

Next, look to a URL that comprises the CSS selector. In the case of the shopper, we used the CSS selector that referenced an on-page writer. All their content material pages used the identical CSS selector for authors. Using the GTM preview device you’ll see that “NumberOfBlogPagesVisited” variable has been executed.

And the precise “BlogPagesVisited” cookie has fired at a price of “1” in Chrome DevTools. To see this, click on Inspect > Application > Cookies.

If we skip the second-page view and execute our third-page view on one other weblog web page, you’ll see that each our GA occasion and our Custom HTML tag fired, because it’s our third-page view.

You’ll additionally see the third-page view triggered our cookie worth of “3” in Chrome DevTools.

Step 7: Set up your superior phase

Now that you simply’ve arrange your cookie, you’ll need to pull the saved cookie knowledge into GA, which is able to enable you to manipulate the info as you see match.

In GA, go to Behaviour > Events > Overview > Add Segment > New Segment > Sequences > Event Action > after which add the occasion identify you laid out in your occasion tag. I specified “three Blog Page Views.”

And there you will have it! 


Now that you understand how to arrange a cookie in GTM, you may get heaps of extra perception into the engagement of your content material.

You additionally know the way additionally to mess around with the code snippet and iterate the variety of web page views required to fireplace the cookie occasion in addition to the expiration of the cookies at every stage to fit your wants.

I’d have an interest to hear what different use instances you may consider for this cookie, or what different forms of cookies you arrange in GTM and what knowledge you get from them.

Source hyperlink Internet Marketing

Be the first to comment

Leave a Reply

Your email address will not be published.