Is there a way to tell if a user on my website has left (i.e. connection dropped, navigated away etc.) in real time?
I am building a RoR website and want to initiate an event as soon as a user leaves my site for whatever reason (e.g. connection, navigating away from domain etc.)
Thanks in advance for any help.
The most reliable way to detect when a user leaves a web page is to use visiblitychange event. This event will fire to even the slightest changes like changing tabs, minimizing the browser, switching from browser to another app on mobile, etc.
The beforeunload event is fired when the window, the document and its resources are about to be unloaded. The document is still visible and the event is still cancelable at this point. This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page.
The Onunload event handler in Javascript is an event handler that is triggered when a user leaves the current web page.
No, there isn't.
The best you can do is send an AJAX request every X seconds (perhaps only if the user moves the mouse). If the server doesn't receive any requests for 2X seconds, assume that the user left.
Nope.
Http = connectionless
You can do some request in this function:
$(window).bind('beforeunload', function(){
// there
});
Send some Ajax or something to tell the server that this user is leaving the page, but this is not a good idea. In Google analytics and in other statistic services you already can see how long the user stays in your page.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With