Forum Moderators: coopster

Message Too Old, No Replies

calculate visitor duration on site?

         

CodilX

5:10 am on Oct 15, 2008 (gmt 0)

10+ Year Member



hi there,

i can't seem to find a script that can calculate a visitors duration on a website.

can someone please point me to the right direction

jatar_k

1:20 pm on Oct 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you actually looking for a script?

there are stats packages that do this.

CodilX

1:34 pm on Oct 15, 2008 (gmt 0)

10+ Year Member



yes I'm looking for a script :) or at least something to grab on to

jatar_k

2:20 pm on Oct 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the only thought I have is to check the online script repositories

hotscripts.com
sourceforge.net

the process is pretty straight forward though

on each page
check to see if session exists
if not set the session and add a timestamp
if it exists calculate time from now to the stored timestamp and update it somewhere using the session id as a key

that's pretty much it

CodilX

5:55 pm on Oct 15, 2008 (gmt 0)

10+ Year Member



yeah but i cant figure out how to stop the timestamp if the user leaves the site.

jatar_k

8:31 pm on Oct 15, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



they should get a new session id when they return, as in close the browser or it times out

<added>if you mean how to add how long they spend on the last page, you can't.

CodilX

9:25 pm on Oct 15, 2008 (gmt 0)

10+ Year Member



yes but with the timeout there are problems, for instance, how long should the session be alive? it wouldn't show accurate results. also there are issues with the close browser thing..

CodilX

11:47 pm on Oct 15, 2008 (gmt 0)

10+ Year Member



ok I'm getting somewhere

1. every time the page is loaded, a random token is created
2. a php file checks if the token is in the mysql database, if found, it updates the duration by 1. if not found, it creates a new entry and the cycle begins all over again

now, the only problem is that I need to refresh the site every second to be able to add +1 value to the duration, so I tried making an invisible iframe to do that and added a javascript reload timer, but even though the website doesn't fully refresh, you can still signs of something refreshing. then I tried making a while(0==0) { ... sleep(1); } loop inside the php script, but it somehow overflows the server and the page fails to load.

does anyone have an idea how to refresh the .php file embedded some way in the site without any visible activity?

maybe this could be done with ajax? the problem is that I don't know anything about it :/