Categories
Digital Marketing Solutions Web Analytics Solutions

Fire a tag just once per session: Without using Cookie (GTM)

Google Tag Manager provides three basic, tag firing options i.e., Unlimited, Once per Event and Once per page. However, there isn’t yet an option to fire a tag ‘just once per session‘ or ‘Only on first pageview‘.

When I tried searching for this, I could find few solution. However, the problem is that they all involve creating ‘cookies‘ to fire a tag just once per session.

Find below the best “Cookie” based working solutions to fire a tag only once per session/visit:

First party cookies are not a problem yet. However, I’m avoiding relying on cookies because of all the hype around future ‘cookie-less world‘.

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

Fire Tag "ONLY" on first page view (once per session)
Fire Tag “ONLY” on first page view (once per session)

Session Storage Vs Cookie

Isn’t it better to use ‘sessionStorage‘ than ‘Cookie‘ for this particular purpose? Avoiding Cookie in this particular use-case has multiple pros and we can still fire a tag only on the first page view using google tag manager as shown below.

Remember: There is usually more than one ways to solve a problem. You can even use dataLayer and events to fire a tag just once. However, that requires some development effort too. 😀

Here is a gist of logic to fire a tag only once per session:

  • Create an item in the Session-Storage Object on first pageview.
  • Increment it on subsequent pageviews.
  • Check the value of session storage.
    • If it is < 2, then and only then fire the tag.

An extended version of this use-case. Like keeping a local count of pageviews on client machine — (or) Firing a tag on Nth pageview can be found here. For Example: – Firing a tag on 4th Pageview. (really engaged visitors?)

Step 0: Understanding the code to fire tag once per session

Let’s first understand the code to set and check sessionStorage item that helps to fire tag only once per session.

Since it’s very basic and simple, just the comments in the code block should suffice. Follow the instructions below the code to see it in action via your GTM container.

var pv = 1;
//check if sessionStorage item exisits
//If it does, increment it.
if (sessionStorage.pv) {
    pv = pv + 1;
    sessionStorage.setItem("pv", pv);
}
//If it doesn't, create it!
else if (!sessionStorage.pv) {

    sessionStorage.setItem("pv", pv);

}

That’s it. So simple right? If you observe closely, this basic block, when executed on every pageview won’t increment ‘pv’ beyond 2. If you want to keep count of the actual pageviews of every visitor, you would simply avoid resetting it. Head to this article to see how to create a pageview counter and fire tag anywhere in the user-journey.

Step 1: Create a GTM tag to fire this on page view.

Choose ‘Custom HTML’ type. Set it to fire on all page view.

Create a GTM tag to fire tag once per visit

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

Type: Custom JavaScript.

Code: See Below

function(){

//convert the value to integer for easy comparision.
return parseInt(window.sessionStorage.pv);
}
Create GTM variable to fire a tag only once per session

Step 3: Create the trigger/condition to fire a tag only once per session

Crete GTM condition to fire a tag once per session

That’s it. You’re all set!

Note that you can use this technique on any tag management platform that doesn’t have a built-in way to achieve this.

Does it work? – Test it.

Fire a tag only once per visit without cookie. Tested

Feel free to leave your thoughts, comments or concerns below.

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.

5 replies on “Fire a tag just once per session: Without using Cookie (GTM)”

Hi Shivanandana,

I’ve tried the setup for my company website, but 2 issues popped up:

– How can I get this to function on a specific page of our website ? The tag I create to fire accordingly to step 3 won’t work.

– The snippet of code in Step 1 appears at the bottom of my website… Not ideal.

Hi Quentin,
1) In step 3, you need to add another condition to specify either the page name or page URL along with Session Storage condition for your case.
2) Check to see if you have put the GTM container snippet in the section of your page.

Hi Shivanandana,
thank you for the great post!
I implemented the variable and the tag, but it seems like it always return a null value.
I double checked the code and it’s the same.

Hi Shivanandana,
Thank you for the great article!

I was wondering whether after some adjustments is it possible to use this method in order to fire tag only once per session, not on the first page? For instance I want to trigger tag when user enters product page for the first time and do not fire it when the person is entering next product page (Let’s call it Unique Product Page view)

Thank you in advance!

Hi Adam,
You can certainly do it. You need 2 variables and perform an AND on those two. So, have a logic to store a localStorage/sessionStorage value on product page visit. You can use dataLayer if available or URL path to set the criteria and then fire the tag and set another counter/flag = true. If both conditions are met, then tag should fire.

Leave a Reply

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