A Flash Developer Resource Site

Page 3 of 4 FirstFirst 1234 LastLast
Results 41 to 60 of 63

Thread: How Get Time Spent on Webpage? Help, wont solve! over 5 hours :( !!

  1. #41
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    about you saying google gets "time spent" from clicking links within the site? Wait...are you talking about Google Analytics? Because my time spent is recorded by loading, and unloading the page.

    Also, sorry once again there is a minor problem i noticed. Lets say i visit my site, and i click a page on my site, then i click back -- when i get to my homepage, the php either doesnot work or inserting a new row in my database. BUT the javascript does work... Is there a way to fix this? Like make a php script run when you click "Back" or "Forward"?

    Thanks
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  2. #42
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    does anyone have a clue about the minor problem?
    when i press back or forward, javascript on that page runs, but not php.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  3. #43
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I tried back and forth a few times, and every time it sent something to the server. You should see a few entries in your database - the same time as this posting - with a time id of 723

    Musicman

  4. #44
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    yes, but what happens is, when you press back the php doesnt add a new row, but instead that old cookie is still the same and never gets updated. And instead javascript sends the time spent with the old cookie id, and the other php script just changes the time spent for the last row.

    Basically, even though the row has a time var, it changes it because the php on the homepage never updates the cookie.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  5. #45
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    two choices: either force a new cookie every time the start page is loaded (and force no-cache in the first place so that reloading occurs), or just add the value to the database rather than inserting it.

    Musicman

  6. #46
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    How am I gonna force a new cooki every time the page starts? To get the cookie value I need the php script to run because the php has the vars with the info that will be put in the cookie.
    Wait what do you mean by adding instead of inserting?
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  7. #47
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    I thought about a sql
    mysql_query("update mytable set timespent = timespent +$spent where id = ....")

    Which script is setting the cookie? Not the one that records the time spent when leaving but one that runs when the page is loaded? So if you use anti-cache headers within your main page, you could force a new cookie.

    Musicman

  8. #48
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    hey,

    The script that records the time spent already used 'update'.

    Sorry I'm not familiar with cache and how to force it to make a new cookie...
    I did read online that its sort of a memory holder for accessing objects, and i googled and found this about anti-cache headers:

    PHP Code:
    header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
    header("Cache-Control: post-check=0, pre-check=0"false);
    header("Pragma: no-cache"); // HTTP/1.0 
    Is that what i should use?

    Thanks
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  9. #49
    Registered User
    Join Date
    Feb 2001
    Posts
    13,041
    Hi,

    if I understand your scenario right, I would suggest to use the update like in my previous post: if a visitor temporarily leaves the page and returns, database would calculate total time. The anti-cache thing is correct, but it would result in separate visits recorded in the database.

    There is one more header that makes sense with stopping cache - an "Expires" with a date in the past

    Musicman

  10. #50
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    ahhh, my bad I didnt notice you said "+ $spent". Now that is a very good idea, thank you very much!
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  11. #51
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    also one more question, im building the framework of the ajax thing, cuz most of the fields i have in my tracking table show empty fields for time. So here's what i have so far and it won't really work, since im not experienced I guess you would know what the problem could be. Thanks
    PHP Code:
    function ajaxFunction(){

        var 
    ajaxRequest;  // The variable that makes Ajax possible!
        
        
    function start()
    {      
     
    starttime = (new Date()).getTime();
            
    }
        try{
        
        function 
    leave()
    {      

    stoptime = (new Date()).getTime();
            var 
    params "spent="+(stoptime-starttime);
    xmlobj = new XMLHttpRequest();

            
    xmlobj.open('POST''timetest.php'true);
            
    xmlobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
            
    xmlobj.setRequestHeader("Content-length"params.length);
            
    xmlobj.setRequestHeader("Connection""close");
            
    xmlobj.send(params);

            
    // Opera 8.0+, Firefox, Safari
            
        
    } catch (e){
            
    // Internet Explorer Browsers
            
    try{
            
                
                function 
    leave()
    {      

    stoptime = (new Date()).getTime();
            var 
    params "spent="+(stoptime-starttime);

    xmlobj = new ActiveXObject("Msxml2.XMLHTTP");

            
    xmlobj.open('POST''timetest.php'true);
            
    xmlobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
            
    xmlobj.setRequestHeader("Content-length"params.length);
            
    xmlobj.setRequestHeader("Connection""close");
            
    xmlobj.send(params);

            } catch (
    e) {
                try{
                    function 
    leave()
    {      

    stoptime = (new Date()).getTime();
            var 
    params "spent="+(stoptime-starttime);

    xmlobj = new ActiveXObject("Microsoft.XMLHTTP");

            
    xmlobj.open('POST''timetest.php'true);
            
    xmlobj.setRequestHeader("Content-type""application/x-www-form-urlencoded");
            
    xmlobj.setRequestHeader("Content-length"params.length);
            
    xmlobj.setRequestHeader("Connection""close");
            
    xmlobj.send(params);

                    
                    
                } catch (
    e){
                    
    // Something went wrong
                    
    alert("Your browser does not support AJAX, for a better experience on our site (and the web) please use IE7/Firefox/Safari. Thank you");
                    return 
    false;
                }
            }
        }

    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  12. #52
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    help anyone?
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  13. #53
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Look into jQuery dude. There is a nice little ajax function built in, and the entire jQuery library is only 18k.

    Just include jQuery on every page, then you can bind appropriate event listeners and do cross browser Javascript with no problems at all.

    http://jquery.com/

    Here's the unload event: http://docs.jquery.com/Events/unload
    And here's AJAX Post functions: http://docs.jquery.com/Ajax/jQuery.post

    We can rewrite your page like this:
    PHP Code:
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
    <
    script type="text/javascript">
    $(
    document).ready(function(){
      var 
    starttime = (new Date()).getTime();
      $(
    window).unload(function(){
        $.
    post('timetest.php', {spent: (new Date()).getTime() - starttime});
      });
    });
    </
    script
    No more complicated cross-browser Javascript debugging.

    Put that in your <head> and see if it works. I haven't tested.
    Last edited by MyFriendIsATaco; 01-19-2009 at 04:35 PM.

  14. #54
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Okay so im trying this on this page only: www.trixmasta.com/about_us.html
    And im using firebug, for some reason when i load about_us it sends the time randomly when it shouldnt, it doesnt send the timespent var when the page unloads but sends it randomly when it the page loads.

    Why is doing that?

    And by using jquery, do you think chrome will also be able to pass vars with ajax?

    Thanks
    Last edited by flashtrickster; 01-19-2009 at 05:09 PM.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  15. #55
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    What do you mean, it sends the time randomly? It sent it correctly for me.

    And about Chrome, using jQuery would be your best chance. If it doesn't work with jQuery, you won't be able to write something to work on your own.

  16. #56
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Oh hey, sorry for not responding soon.
    I sort of see the problem now...but still cant solve it. Basically, when you load a page on my site here is what happens:

    1. The php immediately sets a cookie with a id
    2. When the user leaves that page, the time_spent is sent to a php script
    3.The php script reads the cookie on the computer, getting the id value.
    4.The php script then inserts the time_spent into the row that contains the id that was stored as a cookie

    Okay so for some reason, when you leave webpage A, the time is sometimes not sent. And when the webpage B starts loading (by the time a new cookie is set) THEN the time_spent gets sent to the timetest.php script. So the timetest.php script inserts the time_spent for webpage A, in webpage B's id (because a new cookie was set by the time webpage A's time_spent was sent).

    So, I guess this is happening because jQuery must take a few seconds to do its job... Which is not good Because then the time_spent is recorded in different pages, and screws up the whole table.

    Do you have a way to fix this?

    If this issue is not happening to you when you visit www.trixmasta.com and its pages, then your internet must be fast enough to handle the jQuery or something. But still, it means its a problem for people on wifi that sometimes is slow, like me.

    Please help, thanks!
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  17. #57
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    I dunno dude, it's working flawlessly for me. jQuery is as quick as they come, so I don't think that's your issue. Your bottleneck will be your request to timetest.php. Making an HTTP request to that is going to be way slower than it is to execute a piece of Javascript. And regardless of how you implement it, that will always be the case.

    Explain what is happening wrong, and what you'd expect to happen. In detail. Maybe you're just interpreting your results wrong.

  18. #58
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Hey,

    So I visit my site, and while I'm on my homepage I check my tracking table. My homepage row has no value for timespent (of course since the homepage hasnt unloaded yet), then i click a other link on my site. I look at the table and I see a new row, that HAS my homepage's timespent value! But my homepage row, still has no value! This happens for any way I navigate through my site. The time is recorded into the link I visit to, and I believe the problem is what i said earlier. But I may be wrong.
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

  19. #59
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    No, that is an error with your logic. You're not updating the previous row, and instead inserting a new row with that value.

  20. #60
    Senior Member
    Join Date
    Sep 2007
    Location
    USA................. Posts: 87,683
    Posts
    337
    Umm, well this is what I have in my script:
    PHP Code:
    $query "UPDATE counter SET time_spent= time_spent + $spent WHERE id='$id'";
    $result mysql_query($query) or die(mysql_error()); 
    That's updating the row, not inserting... So what could be the problem?
    ...♠♣♦♥::Trickmaster::♥♦♣♠...
    ╔════════════════════╗
    ║------------------------- ║
    ║-- www.Trixmasta.com--- ║
    ║------------------------- ║
    ╚════════════════════╝

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center