Detecting when someone leaves the website

I’m trying to detect when someone leaves a certain website. and this works:

<body onbeforeunload=“showUnloading(event);” >

function showUnloading(evt){

evt.returnValue = “Are you sure you want to leave?”;

}

However if someone clicks on a link within my site. That goes to another page in the website it treats it as if someone is leaving my website. What is the solution?

Thanks

AFAIK, you can not tell when someone leaves the site only a page.

IMHO the best solution is to not do this just to pester them. Would you like it if every time you left a page you were presented with another page or a message box asking if you were sure you wanted to leave?

There might be some justification in using onbeforeunload to gather visitor retention times, but I think most stat programs already do that.

Fortunately not all browsers support the proprietary onbeforeunload event and disabling it in browsers that do isn’t all that hard. One type of spam that is easy to get rid of - if only it were as easy to kill all the other types of spam.

Yeah I agree, but for my particular case it adds value to the customer, and for the most part my Customer Base will be using Internet Explorer/Firefox and they won’t be smart enough to disable that kind of thing.

Otherwise I wouldn’t even attempt it.

Have javascript inspect the url of each link. If the domain is not yours, you can notify them upon clicking.

But i’d also need to tracke when they exit or press the back button, not only on link clicking.

I’m not sure exactly when the onbeforeunload event fires, but if you use a click event handler for links, you could set a global variable like isInternalLink and then have the onbeforeunload event handler look for that. You’re still going to annoy them alot in certain cases, for example, the back button. I think this would be pretty terrible still.

Really…why do you think this is a good solution to whatever problem you think you have?

There is no way to do it at all if the visitor doesn’t click a link.

Anyway it is never a value add to the site owner to implement something that convinces people to never come back to their site.

To me, unless it was a “you will lose all entered form data” type of thing, I would probably fall into the “not going back” camp. But to be fair, have any studies been made regarding “don’t leave yet” messages? To me, they seem a bit pathetic, and more than a little annoying, but what percentage of visitors would actually change their mind? What percentage of those would eventually end up bringing in some income? In other words, does the practice have any marketing value at all (even if I think the net effect would be negative and would never try using it)?

We haven’t done any testing on our target demographic, (Who by the way would be the type to click on almost any ad they see). Right now we don’t know how effective it will be but want to test it out.

This isn’t for any website in my signature in case you were wondering.

You can track the user and segment who click and leave the site via Google Analytics that you can analyze why they leave the site. Also when you have sent link to external site, you can also track the click, via using this command.

<a href=“http://www.example.com” onClick="javascript: pageTracker._trackPageview(‘/outgoing/example.com’);

HTML


<head>
<script>
// Get the HTTP Object
function getHTTPObject(){
    if (window.ActiveXObject) 
    	return new ActiveXObject("Microsoft.XMLHTTP");
    else if (window.XMLHttpRequest) 
    	return new XMLHttpRequest();
    else {
    	alert("Your browser does not support AJAX.");
    	return null;
    }
}
// Implement business logic
function enter(){
	httpObject = getHTTPObject();
	if (httpObject != null) {
        httpObject.open("GET", "trackvisitor.php?alive=1, true);
        httpObject.send(null);
	}
}
function leave(){
	httpObject = getHTTPObject();
	if (httpObject != null) {
        httpObject.open("GET", "trackvisitor.php?alive=0, true);
        httpObject.send(null);
	}
}
</script>
</head>

<body onload="enter();" onbeforeunload="leave();" >

trackvisitor.php


&lt;?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$alive = $_GET["alive"];

if(filter_var($alive, FILTER_SANITIZE_NUMBER_INT) == FALSE){die();}

$connect = connectdb();

$sql = 'INSERT INTO `user` (ip,alive) VALUES ("'.$ip.'",'.$alive.') ON DUPLICATE KEY UPDATE alive='.$alive;
$result = mysql_query($sql) or die(mysql_error());  


Note the above is untested, and from top of my head to a solution you could use with the onbeforeunload attributable.

Basicy uses JavaScript to update database via PHP when a user enters or leaves a page.
Therefore
if they leave alive is a 0 for their ip
if they go to another page, its updated to 1 for their ip

Add in a timestamp and a hourly cron job to clear those who disabled onbeforeunload… They you have “visitors on page”
Add to the javascript the pagename, you can see what page people abandon your website.

again … code not tested and ive never worked with onbeforeunload

I’ll give that a try basically what is happening is someone adds a product to the cart and if they leave the checkout page we want to offer them a special offer.

The solution I posted will not do that. Your wanting a notification, the code i posted is logging/stats.
As a business you will be awarding people who do not want your product. This makes no sense to me, offer all your customers a fair rate.
In My Humble Opinion.

You should use onunload rather than onbeforeunload with that code in order to make sure it will actually work in all browsers since onbeforeunload is not a standard event.

http://leangenix.com/

That is an example of what I am trying to do. It works in IE and Firefox

I think there exists a good use at http://grooveshark.com which warns users when they’re closing the page, only when a song is playing, with the following message (via javascript I guess):

Are you sure you want to navigate away from this page?

Leaving now will interrupt the song you’re listening to.

Press OK to continue, or Cancel to stay on the current page.

I finally got it working with JQuery, If anyone wants to learn how to do it, put a post here and i’ll post it up here. The solution works in IE and Firefox, w00t. It was easier than I thought.

Thanks for the help all.

Wow - is that ever irritating! I have to agree with everyone else in this form - that looks like a great way to make sure a user NEVER comes back to your site. That’s marketing at its absolute worst.

I think you should follow the advice of many others here and find a more elegant solution to solve what your real problem is. You want sales - offer it to everyone, not just the ones that want to leave your site!

Just my 2 cents, for what it is worth :slight_smile:

I wouldn’t use it to prompt a user to do something. But for auth sessions, it could be of some use.

I’d be happy to see your jQuery solution.

Regards,

-jj. :slight_smile: