Digital Marketing Solutions Web Analytics Solutions

Pageview count based tag: Fire on 3rd or 4th pageview (GTM)

In a previous article, I’ve explained how to fire a tag only once per session. .i.e., only on the first page view. Let’s extend the use case to see how to fire a tag on 2nd, 3rd or 4th pageview. Or simply put, on Nth pageview. The idea is to create a ‘count based tag trigger’ condition. Demonstrated on GTM but works on other platforms too.

Note: This technique can be used to fire a tag on first pageview too!

There are plenty of use-cases why you would need this. It could be a Personalization effort ,a behavior based pop-up or even if you want to wait for 3rd party API calls without using time-delays.

Cookie Vs sessionStorage for pageview count based tag

Of-course there are plenty of ‘Cookie-based’ solution for this. In fact, this is the first thing data collection engineers think of. However, I tend to avoid using cookies.

Again, why use cookies when there is much simpler, quicker and easier way to fire tag once per session or visit? 🤔

This approach also stores the number of pageviews locally on your client browser. This could come in handy for subscriptions, security etc.

Fire a tag after certain page views
Fire a tag on 2nd, 3rd or Nth pageview.

Logic Explained:

  • Create a counter variable key-value in the Session-Storage Object on first pageview.
  • Keep incrementing it on all subsequent pageviews.
  • Check the value of session storage.
    • If it is > (N-1), then and only then fire the tag.

Remember: This only creates a ‘Trigger/Condition’. You need to attach this to your own tag.

Step 0: Understanding the code: Store pageview count >> setup count based tag trigger >> fire tag on 2nd or 3rd pageview.

Let’s first understand the code to set, check and increment sessionStorage item that helps to fire tag after any number of pageviews.

//Check if pageViewCount already exisits, if yes, then +1 it.
	let pvc = parseInt(sessionStorage.getItem("pageViewCount")) +1;
//if no, then just create it and set as '1'. (first pageview)
else if(!sessionStorage.pageViewCount)

Step 1: Create a GTM tag to fire this on all pageview.

Tag Type: Custom HTML.

count based tag fire based on pageview count

Step 2: Create a variable to read and return this value from sessionStorage.

Note: If you’re upgrading from previous article, be careful on variable naming here. pv Vs pageViewCount.

tag execute after 2nd pageview

Step 3: Create the trigger/condition to fire a tag after 2nd or 4th (or Nth) pageview .

pageview count based tag

Note that you can use this technique on any tag management platform for any number of count based tag triggers.

Step 4: Test it.

Execution after 4th pageview

If you’re new to Tag management and Google Tag Manager, read this post to quickly get an idea of implementing Google analytics.


Like Reading Smart Content?

Join hundreds of Smart Readers.

* Only new post notifications.
* No promotions, No spams whatsoever.

We don’t spam! Read our privacy policy for more info.

By Shivanandana Hegde

A digital marketing professional, blogger, automation enthusiast, fitness fancier, occasional hiker and a 'mobile' photographer. 😀 ➤ ( Checkout my instagram)

My interests and expertise are in web analytics, gadgets, technology, psychology and general well being.

Leave a Reply

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