Google analytics - Store event when GA session ends?

445 Views Asked by At

Is it possible to tell Google Analytics to do something when the user's session ends? (NOT a local session, but a Google Analytics session).

According to this: https://support.google.com/analytics/answer/2731565?hl=en#zippy=%2Cin-this-article the session has a timeout of 30 minutes of inactivity, which means it's something that happens on Google's server. Can I tell it to track an event after this session has ended using javascript custom events?

I want to track the user's progress after the user has left the website/session has ended. I want to avoid flooding the analytics with an event every time the user progresses to the next part of the quiz.

I want something like

on_analytics_session_end({
 ga('send', {
  hitType: 'event',
  eventCategory: 'Progress',
  eventLabel: 'Quiz: ' + quizNumber,
 });
});

I can't find anything that allows you to hook onto a session ending.

1

There are 1 best solutions below

0
BNazaruk On

You're approaching it backwards.

First, you have to indicate whether you're on GA4 or GA UA. From the usage of the ga() function, I guess that we're using UA here.

UA changes session in all kinds of weird scenarios like IP change (when you're traveling while on a phone, for example) or when your source/medium change. Or sometimes even when your resolution changes. Catching all that with JS would be pretty hard, and certain things won't be caught for sure. Therefore, no sending events with JS.

You could theoretically run timed function that would check the time of inactivity, and you also can override the onbeforeunload() to send a quick event if a tab is closed or navigated from, but that has little to do with session end tracking as UA defines it.

The proper way of doing it is via building funnels in GA that would start from quiz start and end with quiz end, having all the in-between pages listed for analysis. That will give you the drop-off on every page. Or just use the exit pages report.

If you need to conduct even deeper analysis, then we typically go away from GA interface by exporting the data to BQ, and then either solve your analytics tasks with SQL, or ETL the data from BQ, enriching it and generating fake events to indicate every session end. I've seen a lot of custom GA data ETL and data enriching, but I never seen anyone needing a generated event to indicate session end. Usually the session id dimension solves a lot of session-related quesions, especially in SQL, where you can just count unique session ids where/by/having whatever and so on.