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>

我正在尝试将此概念应用于 onload JavaScript 事件作为 Start Time 和 onunload 作为 Stop time ,例如

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

但是没有得到它的工作..甚至没有抛出错误

任何的想法

4

1 回答 1

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

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

然后(我用这个功能)

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

它采用函数名,不带 () 或引号

与其将其直接分配给 onload 属性,不如将其添加为事件侦听器

检查此链接

于 2012-04-04T18:56:51.233 回答