Categories
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.
if(sessionStorage.pageViewCount)
	{ 
	var pvc = parseInt(sessionStorage.getItem("pageViewCount")) +1;
	sessionStorage.setItem("pageViewCount",pvc);
	} 
//if no, then just create it and set as '1'. (first pageview)
else if(!sessionStorage.pageViewCount)
	{
		
	sessionStorage.setItem("pageViewCount",1);
		
	}

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.

close

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

Shivanandana Hegde is a data practitioner. On a daily basis, he works with analytics and marketing gurus in his profession to optimize sales and user experience and empower clients across India and North America.

He has keen interest in digital devices, communication applications and networks to access and manage information. He has published a number of blog posts and publications over the last decade.

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

Hi Shivanandana,
thanks a lot – the pageview count is exactly what I am looking for, but unfortunately I am getting an error message from GTM “this language feature is only supportetd for ECMASCRIPT_2015 mode or better: let declaration”. Do you have any idea how to solve this?

Hi Tim, you’re welcome! That error you’re seeing is something GTM has inherently. It sometimes checks for strict syntax of JS. Honestly, I don’t know why exactly but if you try it again after a while, it should work!

Leave a Reply

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