/    Sign up×
Community /Pin to ProfileBookmark

php/javascript visitor timer

I’m trying to time how long my visitors stay on my salespage.

i’m using php to save a file with their ip like this 255-25-52-32.php,
it simply holds a single line of text, their entry time and their exit
time seperated by a pipe symbol like this “02:08:15|02:09:03”,
of course i could just save the actual timelapsed or add it as a 3rd
variable, but i like knowing the time of day they arrived.

anyways, my problem is how to i do i detect when they are leaving
and update the exit time right before they leave.

i know a lot of javascript and was trying to use onunload=”” in the
body tag, but it seems to be closing before it records the exit time.

any ideas on how i can use php or javascript to grab their exit time
when they close or leave the page?

thanks for your help guys

edit: i’ll probably store the info in a single file or database, but i was
using the ip as the file name just for testing it out. i’ll polish it up
later.

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@ariellOct 20.2008 — when you first file the user (on entry), you set the exit time to the same value. Then, on the next page hit, you probe for the IP - if it is identical, you UPDATE the exit time to "now". If not -and if never again- user probably didn't care any longer for tour page.

Getting the EXACT "exit" time (there is none, since web apps are stateless) is barely realizable, for there's not even a DEFINITION what -in a web app- an exit actually means.

Best from the south.
Copy linkTweet thisAlerts:
@tfk11Oct 20.2008 — look into the onbeforeunload event. you can probably just do an ajax call back to the server triggered by this event to give the server an opportunity to record the time that the user leaves the page you're tracking.

http://www.webreference.com/dhtml/diner/beforeunload/bunload4.html

the php function ip2long() or database function INET_ATON() may also be of interest.
Copy linkTweet thisAlerts:
@bsmbahamasauthorOct 20.2008 — thanks for the responses, i'll dig a lil deeper into it.

What I really want to record for each visitor is ...

  • 1. the entry time

  • 2. the exit time


  • So i can figure out ...

  • 1. what time of the day visitors tend to come by

  • 2. what day of the week they tend to come by

  • 3. how long they stay on the sales/offer page


  • I can do all of the above except record the exit time

    right before they leave, i beleive the onbefore unload will

    help thanks.
    Copy linkTweet thisAlerts:
    @xvszeroOct 20.2008 — I was looking into this before. One of the owners of my company wanted a script that workers could clock into for specific projects and if the page died or they forgot to sign out or whatever it would still get the latest time they were on the page.

    Never quite solved it. And she hasn't asked about it for months but I know she is going to want it eventually. HMM.
    Copy linkTweet thisAlerts:
    @bsmbahamasauthorOct 20.2008 — i haven't figured it out yet either, and think i might have to combine the onbeforeunload function with ajax. i already can log the time they reach the page and the time they leave the page, but i need to make the script execute when they leave the page, trouble is if they click exit it closes before updating, if i provide an exit link then i can force it to execute, but most people are passing by and will simply leave.

    so i need it to freeze the browser for a few seconds and save the exit time befoe it closes.

    i mainly want to track how long my visitors spend reading my salespage, without buying a $300 script or joining a monthly memebrship site or using google.

    i been coding css, html, javascript and php for like 7 years and can do this myself, it actually works, but i need to log the exit time as they leave.

    my only alternative right now would be to probably use a javascript settimeout to

    loop every 3-5 seconds and save the current time, until they exit, obviously not a good choice, especially if javascript is not enabled.
    Copy linkTweet thisAlerts:
    @xvszeroOct 20.2008 — Well if people would actually click an exit link then it'd be simple... write the exit time to a database. But almost no one is going to click the link.

    I tried the JavaScript loop but got caught up in trying to write to PHP or a database in the loop. Probably a solution. You can write to a text file from JavaScript right?
    Copy linkTweet thisAlerts:
    @bsmbahamasauthorOct 20.2008 — as far as i'm aware you can't save anything to a file via javascript. but you can send info from the browser to a php script with ajax in realtime, but i'd have to research it.

    if anybody reading can provide an ajax script that would allow me execute a php file

    using ajax every 5 seconds or right before the browser unloads i could take it from there

    since my script can already record the entry and the exit times.

    i'll look for the ajax code and post the solution later today, if it works.
    Copy linkTweet thisAlerts:
    @tfk11Oct 21.2008 — the complexity of typical ajax requests can be ignored in this case. simply issue a request to a script that updates the exit time of the ip that issued the request. you don't have to send any data to the script or receive anything from it as the request itself contains all the info you need.
    Copy linkTweet thisAlerts:
    @felgallOct 21.2008 — Not all browsers support onbeforeunload since it is non standard.

    To cater for all browsers you could use a simple "heartbeat" ajax script that sends a signal to the server every so many seconds while the person was still on the page. If you send it every 10 seconds then when the server stops receiving them you know the person left the page in the 10 seconds following the last signal.

    Of course they might have just left the page open in another tab in their browser while they spent several hours on other web pages and just didn't notice they'd forgotten to close your page or may have forgotten to close their browser before going to lunch so the time they spend with the page in their browser doesn't actually mean much.
    Copy linkTweet thisAlerts:
    @xvszeroOct 21.2008 — 
    Of course they might have just left the page open in another tab in their browser while they spent several hours on other web pages and just didn't notice they'd forgotten to close your page or may have forgotten to close their browser before going to lunch so the time they spend with the page in their browser doesn't actually mean much.[/QUOTE]


    Perhaps, but you could always make some sort of bell curve out of the results and figure out which ones you consider "accurate."
    ×

    Success!

    Help @bsmbahamas spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.16,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...