2011-09-29 70 views
1

我们的应用程序在跟踪谷歌分析请求时遇到了问题。除非用于将请求发送到Google的方法通过setTimeout调用,否则不会在Firefox中跟踪内部链接。所以我有这个代码工作,但已经离开了几个月,发现一个同事彻底改变了配置文件并删除了setTimeout调用。现在,当我将代码放回(带有其他新变量)时,我在browswer中发现一个js错误,指出callGA未定义。这里是我的代码:使用setTimeout调用函数的问题

function trackPageview(id, countryCity, unique, bu, bg, areaClick, uri) { 
     setTimeout("callGA('" + id + "','" + countryCity + "','" + unique + "','" + bu + "','" + bg + "','" + areaClick + "','" + uri +"')", 1); 
    } 

    function callGA(id, countryCity, unique, bu, bg, areaClick, uri) { 
     _gaq.push([ '_setAccount', id ]); 
     _gaq.push([ '_setCustomVar', 1, 'Location', countryCity, 3 ]); 
     _gaq.push([ '_setCustomVar', 2, 'Unique', unique, 3 ]); 
     _gaq.push([ '_setCustomVar', 3, 'BU', bu, 3 ]); 
     _gaq.push([ '_setCustomVar', 4, 'BG', bg, 3 ]); 
     _gaq.push([ '_setCustomVar', 5, 'Portlet', areaClick, 3 ]); 
     if (uri) { _gaq.push([ '_trackPageview', uri ]); } else { _gaq.push([ '_trackPageview' ]); } 
    }; 

trackPageviews在多个位置被调用。我在这里提出了一个警告,它的罚款,问题是在setTimeout线。

任何帮助将不胜感激。

回答

1

为什么不把callGA函数放在setTimeout中?

function trackPageview(id, countryCity, unique, bu, bg, areaClick, uri) { 
    setTimeout(function (id, countryCity, unique, bu, bg, areaClick, uri) { 
    _gaq.push([ '_setAccount', id ]); 
    _gaq.push([ '_setCustomVar', 1, 'Location', countryCity, 3 ]); 
    _gaq.push([ '_setCustomVar', 2, 'Unique', unique, 3 ]); 
    _gaq.push([ '_setCustomVar', 3, 'BU', bu, 3 ]); 
    _gaq.push([ '_setCustomVar', 4, 'BG', bg, 3 ]); 
    _gaq.push([ '_setCustomVar', 5, 'Portlet', areaClick, 3 ]); 
    if (uri) { 
     _gaq.push([ '_trackPageview', uri ]); 
    } else { 
     _gaq.push([ '_trackPageview' ]); 
    } 
    } , 1); 
} 
+0

可爱,这工作,我不需要其他功能,但不会很好用JavaScript!谢谢 – Kaskade

3

最好不要引用setTimeout中的动作。它使用eval(),这有点混乱(和不好的做法)。最好使用匿名函数。

setTimeout(function() { 
    callGA(id,countryCity,unique,bu,bg,areaClick,uri) 
}, 1);