How to know if a user closes a page

I was able to figure out when a user launches my app by creating SessionState object. But how can I know if that user closes the webpage and ends the session?

Hi @swswsw, welcome to the Streamlit community!

This is an open question for the digital analytics community, and in the 10 years or so Iโ€™ve paid attention to that industry, there really isnโ€™t a good question. The issue is that itโ€™s difficult to tell the difference between someone not doing something on a page vs. they left the page.

You can try to implement some fancy javascript to either ping a โ€œheartbeatโ€ back to a listener of some sort, or you can try and intercept the back button or link clicking, but in the end thatโ€™s a pretty aggressive tactic towards your users.

What is your use case? Maybe thereโ€™s an easier way to achieve what you are looking for.