onload and onunload JavaScript events with GA time tracker

Go To StackoverFlow.com

0

<html>  
<head>  
<title>Latency Tracking Demo</title>  
</head>  
<body>  
<script type="text/javascript" src="http://www.google-analytics.com/ga.js"></script>  
<script type="text/javascript" src="http://www.example.com/scripts/time-tracker.js"></script>  
<script type="text/javascript">  
var timeTracker = new TimeTracker();  
var pageTracker = _gat._getTracker('UA-1735986-1');   
</script>  
<input type="button" value="Start Timer" onclick="timeTracker._recordStartTime();"/>  
<input type="button" value="Stop Timer" onclick="timeTracker._recordEndTime();"/>  
<input type="button" value="Track!" onclick="timeTracker._track(pageTracker, undefined, 'Manual Test');"/>  
</body>  
</html>

I am trying to apply this concept on onload JavaScript event as Start Time and onunload as Stop time , like

  onload="timeTracker._recordStartTime();"
    onunload="timeTracker._recordEndTime(); timeTracker._track(pageTracker, 'Category','Action');"

But have not get it work ..and even not error was thrown

any idea

2012-04-04 18:53
by buni


1

function loadIt(){
  timeTracker._recordStartTime();
}

function unLoadIt(){
  timeTracker._recordEndTime(); 
  timeTracker._track(pageTracker, 'Category','Action');
}

Then (I use this function)

function addOnloadEvent(func){
    var oldOnload = window.onload;
    if(typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if(oldOnload) {
                oldOnload();
            }
            func();
        }
    }
}

addOnloadEvent(loadIt);
//window.onunload = unLoadIt  //Do the same for unload

It takes the function name, without () or quotes

Instead of assigning it directly to the onload property add it as an event listener

Check this link

2012-04-04 18:56
by Hitham S. AlQadheeb
Ads