Avg Time on Site: how is the last page visit handled? #39
Replies: 3 comments
-
|
Hi! Yes, there are no custom events yet (every event is treated as a pageview for now) and time spent on the last page of a session isn't counted right now. I'd like to eventually revisit this, maybe by just adding support for custom events and/or a specific unload event that updates that duration. |
Beta Was this translation helpful? Give feedback.
-
|
It does exist, unfortunately it's quite unknown :) It's been widely available since 2018 — The basic usage looks like this: document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "hidden") {
navigator.sendBeacon("/api/event", analyticsData);
}
});As an alternative, Following up on your interest in improving avg time — I'd like to propose an approach. The idea is to send an event on I'd call it I'll leave the query side to you since you know the data model best, but the intuition is On a related note: with custom events on the horizon, I'd suggest introducing a whitelist Would love to hear your thoughts! |
Beta Was this translation helpful? Give feedback.
-
|
Quick heads up while implementing the
Sending The fix that works:
function sendEvent(name: string) {
fetch(ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name, entity_id: ENTITY, url: location.href, referrer: document.referrer }),
keepalive: true,
}).catch(() => {});
}
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
sendEvent('pagelastview');
}
});So for the tracker implementation, I'm currently testing this approach on one of my sites by manually sending You can see it in action at https://www.vanessafemia.it/ — open the network tab in devtools |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Looking at the source,
avg_time_on_siteis calculated fromtime_to_next_event, filtered to exclude values greater than 30 minutes orNULL. This means the last event in a session (wheretime_to_next_eventisNULL) is excluded from the average.The tracker script also doesn't seem to send any outgoing events (
sendBeacon,visibilitychange,pagehide, etc.), so we were wondering: is the time spent on the last visited page intentionally not considered in the avg time calculation?We tested sending a custom event via the API:
This gets recorded and correctly populates
time_to_next_eventon the previous event. We could usesetIntervalwith the exportedevent()function as a workaround.Two questions:
"heartbeat"affect the hits count in the dashboard, or are only"pageview"events counted?Thanks!
Beta Was this translation helpful? Give feedback.
All reactions