-1

I want to determine when user is leaving the site using javascript. By site I means domain here.

The body unload occur before leaving any page. So it won't do the job. But is there any way to capture where the user is headed? So when the address is on another domain something is alerted by this event.

Another way, attach an onclick or onsubmit to any inner form and inner link. On this events we set a variable so on unload understand that we are going to an inner page. Maybe you suggest a script that do that automatically but without any javascript framework.

Dears, I don't want to use this as a "please, stay on my site". I want to ask those user that go to order page but anyhow can't submit and finalize the order, and leaving our site to contact us on the phone. So please don't judge it.

Ali
  • 21,572
  • 15
  • 83
  • 95
  • 4
    I certainly hope not; that'd have some serious privacy concerns. – vcsjones Aug 27 '11 at 22:45
  • Are you talking about links that originate FROM your site? Or just navigation away (ie, bookmarks, home button, address bar, etc...)? – Jared Farrish Aug 27 '11 at 22:47
  • possible duplicate of [Javascript function, on web page close](http://stackoverflow.com/questions/2858057/javascript-function-on-web-page-close) and many others. – BalusC Aug 27 '11 at 22:49
  • By your edit then, you're wanting a type of "This is an offsite link and realize you will be leaving website.com by continuing" type of link? – Jared Farrish Aug 27 '11 at 22:52
  • I want to determine when they are leaving my site. and I edited my post for those with concerns. – Ali Aug 27 '11 at 22:53
  • Not a real answer, but there's many sites (Facebook, for example) that transform all links from user generated content to a link to one of their own tracking pages, which in turn redirects to the linked-to page. That would be one solution. – cthulhu Aug 27 '11 at 22:54
  • no, ppl goes to order page and I set a cookie. but they leave without finalizing the order. At this point I catch they are leaving but they had been to order page. Now, I ask them to contact us and tell us why they couldn't finish the order. maybe some program problem. so we can repair our code. – Ali Aug 27 '11 at 22:56
  • 1
    Just because they leave without finalizing an order doesn't mean they did so due to anything you can resolve. You should probably have them create an account and email them X number of days after placing an order if that's what you want to do. – Jared Farrish Aug 27 '11 at 23:00
  • Here we have a lot of problematic issues with networks and computers. Don't consider your situation. – Ali Aug 27 '11 at 23:03
  • That doesn't change anything (as far as I can imagine). If the order is made under a specific account, and the account requires a valid email address, you should be able to contact them later to determine any issues the user encountered. Otherwise, they are ONLY random visitors. What you want to do is not possible with the modern internet and how browsers work. – Jared Farrish Aug 27 '11 at 23:06

2 Answers2

1

This question is very similar, and like others I would encourage you not to do this for privacy reasons. Knowing when users leave your site is going to be near impossible; if instead you want to track when people use links on your site to leave your site, you can impliment click tracking via various means: Piwik is one, you can also use some PHP libraries if you have the option.

You cannot however check what URL a user has entered into the search bar; that would be a huge privacy invasion.

Community
  • 1
  • 1
Ben Brocka
  • 2,006
  • 4
  • 34
  • 53
  • I don't want to know where they are going. I just want to know they are leaving by any means, maybe closing. So the second method is how to goes. Can you suggest a javascript that automatically overwrite all onclick function of As and FORMs that are internal so a variable is set. – Ali Aug 27 '11 at 23:00
0

If a user types in the URL into his browser, you have no way of knowing what was requested. If he clicks an external link, you again have no way of detecting AFAIK.

However, you can create a redirect page on your server and rewrite all links on the page to point to that page, for instance www.ExternalSite.com is changed to http://www.example.com/redirect.php?u=www.ExternalSite.com. This is how Facebook used to do it.

Christian Mann
  • 8,030
  • 5
  • 41
  • 51
  • Facebook still does that, doesn't it? – Bojangles Aug 27 '11 at 22:50
  • Not anymore as far as I know. Part of the reason seems to have been security-related. They used to include a "are you sure?" landing page (similar to deviantArt's), but that was vulnerable to XSS attacks. Is it possible to run a function with a link's onClick event to achieve the same purpose? – Christian Mann Aug 27 '11 at 22:53
  • Not that I know of. You could probably work some magic with AJAX, but I'm not sure. – Bojangles Aug 27 '11 at 22:54