You are storing the data in Server Session. It has invalidation period (configurable, let's say 10 min for example), so it is cleared only after this period. Closing the window doesn't affect this logic at all.
Handling of window behavior should be done on client side with Javascript. Take a look at window.onload
and window.onbeforeunload
events.
Time spent on the page can also be captured on client side. In general the logic could be:
- When page loaded (
onload
event) you start the timer. - When page is unloaded (
onbeforeunload
event) you save timer value to some local storage of sending it to server withajax
call.