Track and send to the server the time user was on the page.

// Track and send to the server the time page was opened.
// It support focus/blur.
// It doesn’t work in Opera.

<script type=”text/javascript”><!–
var startTime = null;
var timeOnPage = 0; // in milliseconds
var startTrack = function()
{
var d = new Date();
if (null != startTime)
timeOnPage += d.getTime() – startTime;
startTime = d.getTime();
}
var stopTrack = function()
{
var d = new Date();
if (null != startTime)
timeOnPage += d.getTime() – startTime;
startTime = null;
}
window.onfocus = function(){ startTrack(); };
window.onblur = function(){ stopTrack(); };
window.onbeforeunload = function()
{
stopTrack();
var i = new Image();
i.src = ‘/tracking?time=’ + timeOnPage;
}
startTrack();
//–></script>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: