Creating page-count-based triggers in Google Tag Manager

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;
        var dn = ".juliencoquet.com"; // prefix with dot to include all subdomains
  	document.cookie="jcoPageCount="+cc+";domain="+dn+";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!

Author: Julien Coquet

Expert de la mesure d’audience sur Internet depuis plus de 15 ans, Julien Coquet est consultant senior digital analytics et responsable produit et évangélisation pour Hub’Scan, une solution d’assurance qualité du marquage analytics. > A propos de Julien Coquet

25 thoughts on “Creating page-count-based triggers in Google Tag Manager”

  1. Hey Julien, great article!

    Two remarks:

    – Un-necessary trolling: I don’t know about other TMS but Tealium already includes that information within its main cookie (“_pn” parameter)
    – I don’t think this method would work for the landing page (page count = 1); I understand that’s not its main purpose though. The cookie is not yet set when GTM triggers are evaluated. Maybe checking its “absence” would do the trick 🙂

  2. Hi QB, long time no see 🙂

    Thanks for your comment!
    You are totally right, Tealium has a built-in variable for that.
    As far as the landing page goes, like I said you can trigger the page count increment in different situations. Arguably you could use a trigger based on Number of Page views = 0, much like you suggest.

  3. Coucou Julien,

    Happy to read your new article 🙂

    Just 2 small remarks, about your custom HTML tag:

    1) You don’t need to subtract 0 from the cookie value to cast the string to integer because your incrementation already does this job, do it?
    var cc = {{Cookie – Page Count}}+1;

    2) You have to precise the expiration of the cookie because some browsers don’t handle automatic session expiration

    no no no, I’m not trolling 😉

    1. Bonjour Mehdi,

      I don’t trust JavaScript so I always sanitize my inputs ;^)
      As far as cookie expiration, by default it expires when your browser closes FOR THE BROWSERS THAT MATTER 😀

          1. I experienced the same issue here as Thomas. Weirdly I setup the configuration on one site and it worked perfectly, I set up exactly the same and to the letter of your instructions on a second site and NaN returns in the variable each time.

  4. thank you.
    I tried your method, but all time get this message –
    Unknown variable “Cookie – Page count” found in a tag. Edit the tag and remove the reference to the unknown variable.

    What can be the reason? It seems that the variable is string type…

    1. Hi Ben, in GTM you need to create a custom variable called exactly “Cookie – Page Count”
      Depending on how you cut/paste from this site, make sure the hyphens (‘-‘) match your system. Feel free to call it something else but reflect the label everywhere per this tutorial.

  5. Hi Julien,
    Thanks for your share.
    Before, I used sessionStorage for this job but it did not consider multi tabs navigation.
    However, I think there is a little improvement you could realise (that I am not able to do…). It would be perfect if the cookie would be readable by subdomains. Indeed, lots of website have areas hosted on different subdomains.
    What is your point of view ?

    1. Bonjour Christian,

      you can set a cookie domain value that includes your domain name, prefixed with a dot, which covers all subdomains.

      See updated post.

      Hope that helps!

  6. Hi Julien,

    Thanks for the share, i note that this is really good at tracking page count across multiple sessions but if i wanted to have a page count at a session only basis and have it reset each time the user comes back whats the best way to modify your code to do this?


        1. var d = new Date();
          d.setTime(d.getTime() + (30*24*60*60*1000));
          var expires = "expires="+ d.toUTCString();
          document.cookie="jcoPageCount="+cc+";domain="+dn+";path=/;expires="+expires;

          1. Hi Julien
            Could you also send me the entire custom HTML with the expiration time of 30 min.?
            I don’t really get where to put the part of the code that you added here in your last comment.

            Thanks!

      1. Hi,

        Thank you for your contribution.

        Unfortunately, I can’t get any further to set the pagecount cookie to 30 minutes.

        In this case, can you please tell me exactly what I have to store as custom html in the Google Tag Manager?

        I would be very grateful!

        Best regards,
        Stella

  7. Hello Julian,

    Thank for the code and this article.

    I came across it looking for a duplicate of GA’s version of “custom goal, record session/page view if greater than…”

    I set my page count to be great than 4 and changed the naming of variables. However when I enter debug mode to test I don’t see any triggers being fired.

    Should it be setup instead of just using preview for it to work?

      1. Thank you for the response JC,

        I don’t have a popup on the site so perhaps that’s why I’m not triggering the tag?

        I’m converting from GA to GTM (I know it’s bit late) but I’ve been resistant and now I’m getting into it.

        Do you perhaps know of a simple rule I can use to create a trigger for GTM to fire this page view count?

  8. Easy to follow article! Works great. Thanks for the help.

    Might just be worth updating the instructions on the page to mention the cookie needs the name ‘Cookie – Page Count’. I have the same problem as another user in the comments, it came up with a publishing error because I named it something different.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.