Nothing is more annoying than visiting a website and getting spammed by pop-ups, right? I’m pretty sure your users resent that too so if you’re a digital marketer, you are not always in control of how some partner tags get fired. What if you could set up engagement-based triggers to add another layer of control? What if you could control tags based on page count?

In today’s post we’ll be looking at how to set this up with Google Tag Manager by using a simple rule to store and increment the number of page views in a cookie. Then we’ll build a trigger that fires tags after a set number of pages have been viewed.

Ready? Let’s go 🙂

Page count cookies are delicious

Right off the bat, let avoid un-necessary trolling by mentioning that this method can be broadly adapted to other tag management systems. Now that we put this out of the way, let’s dive in.

First we need to create a cookie-based variable to store the number of page views. We can call it “Cookie – Page count””, which we can later reference as {{Cookie – Page count}}.

If you are GDPR-minded, this coookie will be a first-party cookie and can safely be considered as a “functional” cookie, containing no personal information whatsoever. It will expire after the browser is closed, anyway.

Create a Google Tag Manager cookie-based variable to store the page count

I called my cookie jcoPageCount but you can name it whatever you’d like.
Note that we create a default value of 0 if cookie is null or undefined.

Add page count to our cookie!

Now let’s create a Custom HTML tag that will read the value in variable {{Cookie – Page count}} and increment it (add 1) after every page view.

The Custom HTML tag should contain code similar to the following:

<script>
  (function(){
  	var cc = {{Cookie - Page Count}}-0+1
  	document.cookie="jcoPageCount="+cc+";path=/;";
  }());
</script>

As you can see above, the Custom HTML tag reads the value of the cookie-based variable, containing the page count – hopefully 🙂

Notice how we subtract 0 to force JavaScript to handle the variable as an integer. Then we add 1 and overwrite the cookie’s value.

So we created the Custom HTML tag but we need to tell it when to fire. Depending on when you need the page count information to be available, you can fire it either with the default GTM page view trigger or with more advanced sequencing, e.g. right after your Google Analytics tag fires. OR event a custom event, it’s really up to you.

When you define the advanced options for the tag, it is recommended to only fire the once per page.

Let’s use our page count cookie

Great, so now we’re incrementing a cookie-based value with the page count.

Let’s now use a simple rule to trigger a tag based on page count. For instance, on this site I have a Custom HTML tag that pops up a sign-up form for my Mailchimp newsletter. Without any customization, the pop-up would fire on every page. With my new Google Tag Manager trigger, I can fire this tag when users have seen at least 3 pages. That way I’m only “targeting” my most engaged users.

The trigger looks like the following:

Use the page count cookie as a condition in a Google Tag Manager trigger

In my case, in the screenshot above, the trigger also looks for the presence of a dataLayer variable making sure it only applies to English language pages. As you can also see, it looks for the value of the Cookie – Page Count variable we created earlier and fires tags only after the third page view (page count is greater than 2).

In closing

Sounds simple, right? That’s because it is 🙂

As I am sure you can appreciate, you can now adapt this methodology to other use cases: number of clicks, events, forms submitted, etc.

Have you tried using this methodology based on page count? What did you use it for? Share your ideas in the comments!