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, jut 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

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.

2 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.

Leave a Reply

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