This is the context of a single-page application, but I believe it would happen as well in other cases. While the GA tracker get's updated with page changes I can see in the realtime report that the user is still alive. But when the user doesn't interact with the page for a while (while playing a video for example) it disconnects from the GA view.
I would like to better understand how GA works in this case, maybe I need send events to keep the user alive.
This is the context of a single-page application, but I believe it would happen as well in other cases. While the GA tracker get's updated with page changes I can see in the realtime report that the user is still alive. But when the user doesn't interact with the page for a while (while playing a video for example) it disconnects from the GA view.
I would like to better understand how GA works in this case, maybe I need send events to keep the user alive.
Share Improve this question asked Dec 19, 2014 at 21:06 a--ma--m 4,7821 gold badge41 silver badges60 bronze badges 1- @canon no, but I thought about send events to GA, would that solve the issue? I would prefere to understand how they implement the user exit. – a--m Commented Dec 19, 2014 at 21:20
3 Answers
Reset to default 18Google Analytics does not detect when somebody leaves the page. There is no reliable way to do that (unload handlers do not fire when somebody just closes the window). Instead Google waits till the session has ended - after 30 minutes without interaction (can be adjusted in Universal Analytics in the property settings) the session is ended and the vsitor has left as far as the reports are concerned (he might have left a lot earlier, but the time after the last interaction is not tracked in GA).
Plus a session can have 500 interactions at maximum, so there is no way to keep it alive indefinitely.
Eike Pierstorff already gave a good answer for my question, just wanted to add some details found on google analytics documentation and a complementary solution to fine tuning the session cookie timeout.
Beside setting the session cookie timeout on the account settings under: admin > property > tracking info > session settings
It can also be set on runtime:
_gaq.push(['_setSessionCookieTimeout', 1800000]);
You can find more details on the google documentation.
_setSessionCookieTimeout(cookieTimeoutMillis)
Sets the new session cookie timeout in milliseconds. By default, session timeout is set to 30 minutes. Session timeout is used to compute visits, since a visit ends after 30 minutes of browser inactivity or upon browser exit. If you want to change the definition of a "session" for your particular needs, you can pass in the number of milliseconds to define a new value. This will impact the Visits reports in every section where the number of visits are calculated, and where visits are used in computing other values. For example, the number of visits will increase if you shorten the session timeout, and will decrease if you increase the session timeout. You can change the expiration timeout to 0 to indicate that this cookie should be deleted when the browser is closed.
The beforeunload
event handler will run just before the page is closed.
window.addEventListener('beforeunload', function(e) {
//run some code
//OPTIONAL: if you use the following, a navigate confirmation box will appear
return "Are you sure you want to leave";
});
Alternatively, you could use the pagehide
event, which also triggers when the page is unloaded. This is triggered a while after the beforeunload
event though, so if you want to perform some more heavy computations, I'd recommend using beforeunload
instead.
If you want to make the session time out after leaving the page inactive for too long, you could use a setTimeout
that's reset everytime the user interacts with the page:
window.idleTimer = 0;
function resetIdleTimer() {
clearTimeout(window.idleTimer); //reset the previous timer
window.idleTimer = setTimeout(function() { //set another one right away
//kill the connection
}, 30*60*1000); //30 minutes = 3600000ms
}
resetIdletimer(); //initialise the timer
window.addEventListener('focus', resetIdleTimer);
window.addEventListener('mousemove', resetIdleTimer);