Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if a user has left my website

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.

like image 309
user7289 Avatar asked Jul 09 '10 18:07

user7289


People also ask

How do you check if the user leaves the page?

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.

Which event triggers when the user leaves the page?

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.

Which of the following body tag handler is triggered when a visitor is about 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.


3 Answers

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.

like image 179
SLaks Avatar answered Nov 14 '22 03:11

SLaks


Nope.

Http = connectionless

like image 35
P.Brian.Mackey Avatar answered Nov 14 '22 03:11

P.Brian.Mackey


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.

like image 33
Gacha Avatar answered Nov 14 '22 01:11

Gacha