Like the title says, I have a React project using react router and want to detect when a user leaves the page (i.e. refresh, close tab, or change url). I don't want to prompt them or anything (like with the Prompt component in react router) because the purpose of this is to track how long they were on the page so I can use it for analytics.
What's the best way to do this so I can emit a metric when the user leaves the page?