Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Maybe this is the wrong place to ask, but. . . I'm looking for a bit of code that I can use in a cgi file to count the number of seconds each individual viewer has looked at a particular page in a website. (With a statistical running count to be updated automatically.) Is there such a thing out there? I'd appreciate a link to some simple code (Perl is most desirable) that does this, if it exists. Something similar to this: # of viewers caclculated - 100 % of visitor viewing time 0 to 1 seconds - 10% 1+ to 5 seconds - 30% 5+ to 10 seconds - 25% 10+ to 30 seconds - 15% 30+ to 60 seconds - 15% 60+ seconds - 5%

Replies are listed 'Best First'.
Re: viewer eyeball counter?
by talexb (Chancellor) on Dec 08, 2003 at 20:03 UTC

    As you've already heard, this "can't be done due to the statelessness of the HTTP protocol", however another way to approach this is to do web log analysis, and infer page eyeball seconds from that information.

    The beauty of this approach is that you don't need to retrofit any of your CGIs -- just look through the log file that probably already exists.

    --t. alex
    Life is short: get busy!
      The fault with this is that unless you use session unique session ids to track visitors on your website and store these in the log file you have to make assumptions:
      If I see the client IP is a.b.c.d and the next hit is from a.b.c.d then he must be the same visitor. (oops what about proxy servers? AOL? MSN? woopsie.)

      If I see a delay of X minutes between hits then I am going to count this client IP as a new visitor, otherwise it is the same session and the session length is <time of last hit> - <time of first hit in session> long.

      Depending on what level of accuracy you are looking for (and what percentage of your hits come from proxies) looking at the log files for this information just gives you faulty info.


      -Waswas

        Well, I guess I could have put in the standard disclaimer about how none of this makes sense if you're dealing with client IP addresses that change (which they will for anyone behind an AOL firewall), session IDs that change, multiple servers (in which case you have to merge the log files then do your parsing), and so forth. But I presented my (simple) solution based on the trivial situation: stable IPs, a single server, no weirdness with the URL changing.

        I'm guessing (or hoping, anyway) that anyone in a non-trivial situation is going to know about how to work around their situation to get the right answers.

        --t. alex
        Life is short: get busy!
Re: viewer eyeball counter?
by pg (Canon) on Dec 08, 2003 at 19:52 UTC

    This is doable, if we are talking about intranet. Just hook something in your web server. This code shall be made aware of each user request, and has a data structure to store the time of the last request from each client, so it can calculate the intervals between requests. But obviously that interval is not precise, as you cannot say that the user 100% focused on a page before he/she requests the next one. (Internet would also be fine, as the users in an organization, most likely would access internet through some sort of proxy.)

    This could also be a small proxy program on the client side, which captures each user request before it forwards the request to server. In this case, it does not matter whether we are talking about internet or intranet.

Re: viewer eyeball counter?
by Roger (Parson) on Dec 09, 2003 at 02:12 UTC
    You could do this with a bit of Javascript on the webpage you display (generated from Perl CGI script), with the OnLoad and OnExit handlers.

    The basic idea is to let the client side track the time instead of doing it from the server side.

    When the user first load the page, the OnLoad handler is called, which sets a start timestamp. When the user clicks on another URL, the OnExit handler is called, which can submit a request back to your CGI script on the webserver with the time difference as a parameter.

    This method will certainly not work if the user's browser has Javascript disabled. But I think most people have Javascript enabled in their browser, so it should work most of the time.

Re: viewer eyeball counter?
by hardburn (Abbot) on Dec 08, 2003 at 19:37 UTC

    It won't work, due to HTTP's stateless nature.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    : () { :|:& };:

    Note: All code is untested, unless otherwise stated

Re: viewer eyeball counter?
by schweini (Friar) on Dec 08, 2003 at 19:57 UTC
    i agree that this is basically impossible with HTTP being stateless and all that, but you might be able to achieve something similar doing the following:
    every page could have an embedded cgi somewhere (via SSI, or maybe a cgi-generated image), and that CGI-script could check for some cookie you set the first time the user hits your website. using the information in the cookie, you could then log the the visitor's 'tracking number' alongside the url and the time somewhere.

    i'd be very surprised if somebody hasn't done this before, though...maybe something in combination with the various session-management modules?
Re: viewer eyeball counter?
by Anonymous Monk on Nov 27, 2017 at 06:09 UTC
    Any one now the supplier of Eye Ball Counting Camera ?