How much time the user has spent on the site?

Question:Is it possible to record the duration of time(like minutes) a user has spent on my site?

i want to record the time users spend on my site and store it in a database. So that if users leave quickly, i know my site ain’t worth their time and i have to do some serious updating to do.

i know how to store stuff onto a database(using mysql) but i want to know how can i know the duration ?

my idea was to do the following:

1.when a user clicks on the “ENTER SITE” i would start a timer.
2.when the user clicks the close button of the IE window . i have to stop the timer and store it in a database.

while the first step is a bit easy to do, i want to know how i could start the timer. more importantly, the second step i guess should be done using JAVASCRIPT.

please reply if:
1.you have already done something like this.
2.you can help me out with this idea(i am a bit of novice in javascript)
3.more importantly, if you have a better way in which one can do this kind of thing.

thanks.

ok… i didnt read your whole post… but im gonna post what i know.

i know if you get counters at different providers (cant remember name right now) they have a thing that says how long has someone been there for.

if you want me to give the url, lemme know.

I am interested to know how the timer is activated when the user start accessing a site for instance. :slight_smile:

you would record the time. save it into a database and then comapre that to the time that they leave the site.

Yes, and the problem with that is that unfortunetaly, Javascript records the user’s time clock, and not the server’s, thus if the user’s clock is inaccurate, well then…

One way you can do it is to use the Date object:


var newTime = new Date()
var curTime = newTime.getTime() // this will record the milliseconds since 1/1/70 00:00:00 GMT
//Do stuff with the curTime here, such as pass to Flash

you could just pull it off the server too because it doen’t matter if its accurate or even if it is their time because you are just comparing when they enter and leave. You just compare them…

Javascript, not php:

In your body tag, add


onLoad="ChkStrtTme()" onUnload="ChkEndTme()"

In your head add


<script language="javascript">
var strtTme

function ChkStrtTme() {
    var CurDate = new Date()
    strtTme = CurDate.getTime()
}

function ChkEndTme() {
    var CurDate = new Date()
    endTme = CurDate.getTime()
    var totalTime = (endTme - strtTme) / 100 // Number of seconds
}
</script>

Then do what you want with totalTime.

Unfortunately a server-side solution will not work here, as the server will have no idea when a user leaves the site unless you use the Application object, and that is complex business.

Maybe a better solution would be to record how many times each user has been to your site. That would help too, because then you could actually record the information on a database.

I you want I’ll type up the ASP for you.

thank you guys! i never really expected such response to this somewhat puzzling question.

anyway, i want to extend this idea like this:

when a user logs onto the site i will send him a cookie with a random name to identify him. i would also enter the name that the cookie sent into the database. to record the times the user has visited the site i shall check if the random name is in the database and increment a count variable.

the question is:

how do i make flash and cookies communicate with each other?

thanks again 4 the gr8 replies. i’ll work on that when i get home.(i am at a public terminal right now)

I’m not sure if you realized it or not, but this information is readily available in your log files, for EVERY visitor that has come to your site. Simply use a log-analyzer to decipher it. =)

i have free webspace at lycos.co.uk. Is it possible for me to view the log files?

Anyway i don’t think lycos.co.uk allows free webspace users to view log files

ASP sessions look into the server session objects

my free webhost(lycos.co.uk) supports only PHP. So I cannot use the ASP server session objects you are talking about.

Anyway are there any reliable FREE asp web hosts out there?

www.brinkster.com