Welcome Guest, Not a member yet? Register   Sign In
Track user time spent on web pages
#1

[eluser]RaZoR LeGaCy[/eluser]
Hello,

I would like to track the time spent per page then increment the time in the database thus tracking total time spent on the site over the users lifetime.

I read up on this a little bit and it looks like javascript is used as well.
Code:
<head>

< /*SCRIPT LANGUAGE="JavaScript">
&lt;!-- Begin
startday = new Date();
clockStart = startday.getTime();
function initStopwatch() {
var myTime = new Date();
return((myTime.getTime() - clockStart)/1000);
}
function getSecs() {
var tSecs = Math.round(initStopwatch());
var iSecs = tSecs % 60;
var iMins = Math.round((tSecs-30)/60);  
var sSecs ="" + ((iSecs > 9) ? iSecs : "0" + iSecs);
var sMins ="" + ((iMins > 9) ? iMins : "0" + iMins);
document.forms[0].timespent.value = sMins+":"+sSecs;
window.setTimeout('getSecs()',1000);
}
// End --&gt;
< /*/script>
&lt;/head&gt;

&lt;body&gt;

That records the time on the page.

Now how do I insert it into the DB on the next web page?
#2

[eluser]bretticus[/eluser]
An easier and more accurate way would be to store it in a database table. Use sessions to keep track of the user. Measuring time is then just a comparison of the times for adjoining database records.

If you want to keep the js route. Look into the javascript onunload event of jquery unload. You could, perhaps, hijack the url as you leave or call an ajax routine. Like I said, the former is easier Smile

Good luck!

EDIT: google analytics and omniture, etc. have the page call some javascript url at their websites. Assuredly, this timestamp is stored it in a database also.
#3

[eluser]RaZoR LeGaCy[/eluser]
javascript is kind of the bane of my existence so far LoL

How can I use jquery to call say a CI Controller thus updating the DB?

I would use the onload which should be similar to the above script. I will research that jquery onload. But how do I make the ajax call to CI Controller? MVC examples
#4

[eluser]eoinmcg[/eluser]
Quote:How can I use jquery to call say a CI Controller thus updating the DB?

Try something like this:
Code:
$(document).ready(function(){

    $.ajax({
        type: 'GET',
        url: '/controller/method',
        success: function(data) {
            //  alert("It worked: "+date);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            // alert("It failed: "+errorThrown);
        }
    });

});

In terms of MVC you can call models from the controller and load output in views as you would normally. One handy trick for testing if the call was Ajax
Code:
function test()
    {
        if($this->_is_ajax())
        {
            echo 'ajax';
        }
        else
        {
            echo 'not ajax';
        }
    }


    function _is_ajax()
    {
        return (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'));
    }
#5

[eluser]RaZoR LeGaCy[/eluser]
Still not seeing in my mind how to mash it all together.

So onunload I run the above code to add the time to the DB?

Please I need a little more.




Theme © iAndrew 2016 - Forum software by © MyBB