I know I can use onunload to do something when the user exits the page, the problem is that it works whether the user leaves the page, reloads, closes the page, etc.
I need to run a script only when the user leaves my page to another domain, ex:
if the user is in www.exemple.com/here and goes to www.exemple.com/there nothing should happen, but if he goes from www.exemple.com/here to www.anotherdomain.com/ then the script should run.
What´s the best way to do that?
As for why I need that, here is the story:
I work with googleTagManager and I need to fire a tag at the end of a session. The session only ends when the user closes the window or when he leaves the site domain. If he reloads or if the stays within the site, the tag should not fire.