Link to home
Start Free TrialLog in
Avatar of countrymeister
countrymeister

asked on

How to detect a user has left a website

I need to now when a user leaves my webiste, lets says I have a website www.xyz.com
When the user logs in to my website I record the login time, when they leave the website say by typing www.google.com.
I need to know if they hit the browser back button, they had left my website, gone somewhere else and now trying to get back.
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

That is not trivial.

There are no events triggered on back or close.

onbeforeunload can be used to ask the user if they really want to leave, but you cannot register their answer.

You can add onclicks to links to see when they leave your site by clicking an external link
Avatar of countrymeister
countrymeister

ASKER

mplungjan:

onbeforeunload is triggered when the user navigates from  page a to page b within the website and also when the user types in a new URL and also when he closes the browser

so is there any way to track the new URL typed?
web is working in a disconnected mode and there is no way (in my knowledge) to identify if the user after getting result from your website because what ever happens is controlled through the browser and the browser will only navigate to the new url that is typed and will not send back any information to the previous URL
ragi0017:

i have seen quite a few websites where the user on navigating from the  URL of interest and comes back, it lets the user know that the session has been terminated.
How is this done then
if you are using asp,asp.net or other languages use
Session_OnStart and sub Session_OnEnd functions see details here
http://www.w3schools.com/ASP/asp_globalasa.asp

or simple you can follow this as well
http://www.siteexperts.com/forums/viewConverse.asp?d_id=20684
In classic ASP this is made through session variables. I supose that ASP.NET would have something similar.

When a user access to your webpage a session environment is created for him. This session environment stores variables where you can assign values relative to each unique user.

Sessions and session variables has a defined life time, when a user leaves a webpage, or becomes inactive, it's session starts to die, being totally death upon reaching it's maximum duration.

On classic ASP the death of a session triggers the execution of a function in Global.asa file (session_onUnload I think... it's been a long time since last time I used it), once more I supose that should be an equivalent for ASP.NET

The time you define for your user sessions is very important, as a session doesn't dies until users pass this determinated time without navigating or interacting with your page... so if you give 20 seconds to force quick disconnects after leaving page you risk that users get disconnected when taking brief seconds reading data in one of your pages (as they become inactive for more than 20 seconds).
mplungjan:

" You cannot track if they load a url in the browser except if it is another of your urls "

Do you have any code samples in doing the above

I tried to get the location.href when the type in the new URL, say I am on www.xyz.com and then I navigate to www.google.com, the onunload trigger still shows me xyz.com
You cannot track any url typed into the location bar - not even your own

What you CAN track is that you end on your site again using for example cookies.

referrer will be empty if you come from a typed url or a bookmark
mplungjan:
can you please give me some code sample to do what you mentioned above
I was thinking to set a cookie with a time stamp onbeforeunload and read this cookie when loading. In both cases tell the server. In the load part tell the server to ignore the previous unload if the time stamp is less than x seconds.
tis is not going to work, because the user could be on the main site for say five mins and then type in google.com and then the very next min hit the back button
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How about a B, let me know if that is OK