SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jan 2005
    Location
    London
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Measuring item visit time

    Hi,

    I need to measure the time a logged-in member of my website views a specific item for (in seconds) and store the result in a mysql table - I can't see no obvious way to do this.

    I have looked at using javascript onload and unload event handlers, however the only was I can see inserting the result into the db is by calling a pop-up on unload and inserting the result through php - I don't like this idea for obvious reasons.

    Does anyone have any ideas how I can implement such a feature into a php based website? I have considered an applet, however my java coding skills are not great.

    Any help on this matter would be much appreciated. Does anyone have any examples of someone implementing such a feature?

    Cheers in advance

    Robby

  2. #2
    ko pročita magarac :) boccio's Avatar
    Join Date
    Oct 2003
    Location
    belgrade
    Posts
    354
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    function utime (){
            
    $time explode" "microtime());
            
    $usec = (double)$time[0];
            
    $sec = (double)$time[1];
            return 
    $sec $usec;
    }
    // at beginning
    $start utime();

    // view item....

    // at the end
    $view_time = (utime()-$start);

    // store $view_time to db 
    Hope this helped
    Vivvo CMS - Web publishing at your fingertips
    Mile voli disko, a ja belo kolumbijsko

  3. #3
    SitePoint Zealot saurab's Avatar
    Join Date
    Sep 2004
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by yukka
    Hi,

    I need to measure the time a logged-in member of my website views a specific item for (in seconds) and store the result in a mysql table - I can't see no obvious way to do this.

    I have looked at using javascript onload and unload event handlers, however the only was I can see inserting the result into the db is by calling a pop-up on unload and inserting the result through php - I don't like this idea for obvious reasons.

    Does anyone have any ideas how I can implement such a feature into a php based website? I have considered an applet, however my java coding skills are not great.

    Any help on this matter would be much appreciated. Does anyone have any examples of someone implementing such a feature?

    Cheers in advance

    Robby
    Hi, this can definitely be done in PHP. The logic is this :
    Lets say, a visitor clicks on a product image and the product details page loads up. Now you want to check till how long the visitor was on that product details page right ?

    Now, how do you check for valid sessions ? by doing a session_start() on top of each page which if valid will get you the user name or user id etc ....
    Now, what you can do is that whenever the guy goes to some product display page, at that moment check the user id with $_SESSION['userid'], and immediately enter the product id and a start_time in the valid session variable as an asociative array (like array(p_id => 'timestamp') )
    now when he visits a new page, again do the same add another p_id => 'timestamp' into the same array.
    this way when you take the diff of time_stamps between successive elements of the array, you get the time spent on one page.
    so for example $_SESSION[$arr['p_id2']] - $_SESSION[$arr[p_id1']] = time spent on product no 1' page
    this can go on and on.
    Now, only when the guy logs out, put all this p_id and time corresponding to each p_id into the db and only then destroy the session variables and log the guy out.

    Now, the advantage here is that you are not hitting the db for every product page (or every page) loaded. you only hit the db to store the product_id and time spent for each product_id when the guy hits the logout button.
    implement this and tell me how this idea went ..

    --saurab
    Accepting Offers: NicheLabs.com - ExploringVOIP.com
    StableJob.com - AboutUniversities.com - Gemstone.co.in
    Assets.co.in - PropertyDealers.org - MortgageFirms.org
    eLearner.org - TravelResorts.info - TourstoAsia.com



  4. #4
    SitePoint Member
    Join Date
    Jan 2005
    Location
    London
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boccio
    PHP Code:
    function utime (){
            
    $time explode" "microtime());
            
    $usec = (double)$time[0];
            
    $sec = (double)$time[1];
            return 
    $sec $usec;
    }
    // at beginning
    $start utime();

    // view item....

    // at the end
    $view_time = (utime()-$start);

    // store $view_time to db 
    Hope this helped
    Thanks for the response boccio, although your example measures the starting time of user viewing an item it only measures the content load time and not the time a user spent viewing an item/page - or am I wrong here???

    As suggested by saurab the only way to do this in php is by grabbing the start time of the next page and tracking the time difference accordingly. If anyone has any non-php methods that can be implemented on the client side (applet) with a mysql connection then please let me know.

    Cheers for the help ppl.

  5. #5
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that is approximately load time. when user goes from one page to other you will deduct new time from old time. you can only approximate. there's no way knowing exactly how much time user spent.

    think if user closes the browser and never goes to another page then ther's no way knowing how much time she spent.

  6. #6
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    Parry Sound, ON
    Posts
    725
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could use XMLHttpRequest to poll a separate script every x seconds. Just throw the response away.

  7. #7
    SitePoint Zealot saurab's Avatar
    Join Date
    Sep 2004
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    that is approximately load time. when user goes from one page to other you will deduct new time from old time. you can only approximate. there's no way knowing exactly how much time user spent.

    think if user closes the browser and never goes to another page then ther's no way knowing how much time she spent.
    think you are correct there... you can only get estimats there, and as you correctly pointed out if a guy closes the browser there's no way of knowing.
    One solution that comes to mind is to back all this up by some post-statistical analysis.
    So, once the details have already been collected, using statistical methods to detect the "outliers" .. that is to detect the most probable defaulters ...

    so for example if you have data like this :

    page1 : 5 seconds
    page2 : 7 seconds
    page3 : 12 seconds
    page4 : 4 seconds
    page5 : 22 seconds
    page6 : 2230000 seconds


    now here, it's clear that something weird happened to page6 (maybe the guy went to the kitchen to boil some water for the tea or simply closed the browser window without logging out) ..... thats because the value is WAY off the average of the rest of the pages....
    so, if we can detect such outliers then "for the purposes" of calculating page popularity (measured by amount of time a guy spends on a page on average), such outliers can be kicked off .. for calculating useful statistics.
    This is one of several other things that need to be done with the raw statistics before one gets something that can be at least "largely" reliable.

    --saurab
    Accepting Offers: NicheLabs.com - ExploringVOIP.com
    StableJob.com - AboutUniversities.com - Gemstone.co.in
    Assets.co.in - PropertyDealers.org - MortgageFirms.org
    eLearner.org - TravelResorts.info - TourstoAsia.com



  8. #8
    SitePoint Member
    Join Date
    Jan 2005
    Location
    London
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the throrough responses, an approximate estimation is all that is required. The data will be validated for oddities before insertion into the db.

    I'm going to have a quick butchers into implementing a Java applet to handle onload and unload events within a page before playing with calculations of time spent on secondary pages.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •