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

Last Updated on December 13, 2021 by Shivanandana Hegde

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)

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

Note:– This only satisfies one use case. However, if you want to keep a count of the actual pageviews of every visitor, you would simply avoid resetting pv=1. 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.

Like Reading Smart Content?

Join hundreds of Sharp Readers.


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

Read our privacy policy.

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

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

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

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

    Reply
    • Hey all,
      I’m having the same issue, as the trigger and tags fires asynchronously the trigger will always have null in the 1st page_view. I don’t know how other people got this to work, but copy-pasting gives always null result in first page-view.

      Reply
  3. 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!

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

      Reply
      • Looking to understand this more. I need this exactly. What two variables are needed and how does the setup of this work? thanks!

        Reply
  4. 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!

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

      Reply
  5. HI! I was able to implement this without any trouble in my standard GA setup. When trying to use this trigger in G4 – I do not get matching data between total users and the number of events fired. Although, again, this is correct in GA. Did you do anything differently for a GA4 setup? Have you noticed a discrepancy in your own data?

    Reply
  6. Hello,

    Does the pageview count reset every30 minutes of inactivity or every time user closes the browser and start a new session in a new browser?

    Reply
    • Sensible question. A: It depends on where you’re storing it. If you store it in cookie, it’ll expire based on your setting and will remain even when user comes back. If it’s on session storage, it’ll expire after the session. If it’s local storage it won’t expire. Of course, all of these will be erased if users clear their browser or history.

      Reply
  7. Hi
    I found your tutorial here. https://webmasters.stackexchange.com/questions/138665/have-google-tag-manager-fire-event-once-per-session
    (BTW, he says you forgot to add the exclusion for PV > 1 on the tag)
    Nicely done, and thanks also for the references to the cookie solutions (of course I already had checked in with Simo and Julius : -)
    However, in my case I wanted to make session timers – i.e., fire once x sec after session start, never again, once per session.
    Since the timer is itself a trigger, this procedure with the session storage variable becomes more complicated.
    My easy solution was to use the built-in Referrer variable. So the timer fires after X sec on any page (matches regex “.*”) but only if referrer doesn’t contain my domain. So only the first page of a session will trigger it.

    Reply

Leave a Comment

X

Like Reading Smart Content?

Join hundreds of Sharp Readers.


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

Read our privacy policy.