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.
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…
<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.
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. =)