I need to fire an event once per session on session start. I have googled this and tried some solutions to no avail. For clarity I am using a LAMP stack. I need it to be an event so I can set a custom metric on the event for reports

  • Are you OK using a cookie? Commented Feb 17, 2022 at 17:31

3 Answers 3

  1. Define what a session is. It's extremely unlikely that you want a counter that counts GA's sessions since GA already has a metric for sessions.

  2. If your definition of session is just a time-between-actions story, then just:

    2.1 Make a tag that sends the session event on every pageview

    2.2 Make a tag that sets your session cookie with the expiration time set to the duration of the session.

    2.3 Make CJS (or the cookie variable) that checks if that cookie is present.

    2.4 Now finally make the 2.2 tag to autofire always after 2.1 fires. And add an exception for 2.1 to not fire it whenever your 2.3 CJS (or cookie) variable detects the session cookie.

  3. Boom! Now you have a mechanism that fires the tag only if some time passed in between user interactions.

Well, ok, to be fair, I'm doing it only for pageviews in this example, but you can do it for all interactions by just making the 2.2 fire after all tags that you deem "interactional" for the purpose of your custom session tracking.

  • ' It's extremely unlikely that you want a counter that counts GA's sessions since GA already has a metric for sessions '. No...No that's exactly what I want. I need to associate a custom metric with the event so I can see the number of sessions per tenant. I am unable to associate custom metrics with my sessions so I do it through my events in GTM
    – Gary Kelly
    Commented Feb 18, 2022 at 8:59

I actually figured it out with the help of this tutorial: https://digilitica.com/solutions-and-services/digital-marketing-solutions/fire-a-tag-just-once-per-session-only-on-first-pageviewgtm/

The only thing is their triggers are wrong. You need 2 triggers. Have it trigger on all pages but also add an exception to your tag :enter image description here

Then create a GA4 event with the exact same triggers (The exception in the custom html may not be necessary but I add it to prevent the tag continuously firing)

The outcome will look something like this:enter image description here


Shivanandana's solution linked to in Gary Kelly's answer is nicely done (of course I'd already checked 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. The timer fires after X sec on any page (matches regex ".*") but only if referrer doesn't contain my domain. Only the first page of a new session will trigger it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.