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.

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

Thanks for the post!
I have tried to use your method, but I am encountering a very big problem with it.
The thing is, when you use that trigger (pv<2) with another tag (say tag X) so that tag X is triggered only once, you realize both tags (tag X + the tag adding +1 to pv) run asynchronously.
Since they are asynchronous (there is no way to control the order in which they are triggered WHILE having triggers (pv<2), as far as I know. Not even assigning a priority number works for that purpose) what happens is that the target tag, X, some times loads/runs its trigger condition (pv<2) before the PV tag finishes its execution, and some times afterwards.
The only way to make your method work – as far as I am understanding it – would be to use tag sequencing BUT with triggers
In gtm it is possible to use tag sequencing, but doing so disables the tags' triggers.
Plase, am I incorrect in my reasoning (I am new to this)?
How can we use your method (which I like and I think is extremely witty) to fire the tag X (containing the pv<2 trigger) ONLY when the tag controlling the tag variable has finished its execution?

Thanks!

Hi Jorge, I understand the problem. However, you’re talking about 2 different things.
1) Fire a tag (X in your case) only when pv<2
2) Fire the above tag X, only after another tag {controller tag –> let’s call it P} has already fired first.

The second scenario is purely tag timing and you must use tag sequencing. I am not sure why GTM would disable the tag’s trigger. I am using it without any issue.
Even if so, you can make use of local or session storage within the code itself (custom JavaScript) instead of setting it to trigger.
Refer: https://support.google.com/tagmanager/answer/6238868?hl=en
Assuming I understood the problem, the tag setup would be like below:
1) Create tag X and make it dependent (fire only after controller tag P).
2) Have an AND logic in place to check for pv<2 as well as whatever the other condition for that tag is.
Refer the attached image for second step.
Tag Sequence Example

I hope this helps! Lemme know.

Leave a Reply

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