r/webdev • u/infinitnet • 13h ago
[Showoff Saturday] What can a page script learn about how the visitor left, with no cookies or storage? (pagehide.persisted, bfcache)
Small experiment: what exit signals does the browser give a page script when you're not allowed to store anything?
- visibilitychange to hidden is the reliable flush point, but it also fires on tab and app switches, so it isn't an exit.
- pagehide always fires on navigate-away or close. e.persisted === true means the page went into bfcache, so the visitor navigated away and the tab stayed open. A tab close gives false.
- On the next document, performance.getEntriesByType("navigation")[0].type === "back_forward" tells you it was reached via Back or Forward.
I send cumulative visible and active time on each hidden period, and always on pagehide, with the persisted flag.
I use it to estimate pogo-sticking for visitors from search: one page view, no events, short stay, left with persisted. A later arrival from a search engine joining the same session is a stronger "went back to the results" signal.
Caveats: persisted fires on any bfcache-eligible exit, not only Back. Ineligible pages never report it, so it's a lower bound. An app switch isn't an exit. Compare within one browser. I looked at the Navigation API, but it only exposes same-origin entries, so activation.from is null for a cross-origin return to the SERP.
It's part of an open-source cookieless analytics plugin (~3 KB script, Apache-2.0): https://github.com/infinitnet/cookieless-analytics
Is there a better exit signal than persisted?
1
u/morenovanessa_94 1h ago
One caveat: `persisted === false` doesn't mean tab close; it also happens on navigations where the page isn't cached. It's a bfcache hint, not a close-vs-navigation classifier.