使用 setTimeout 调用函数时出现问题

发布于 2024-12-07 08:54:46 字数 1077 浏览 0 评论 0原文

我们的应用程序在跟踪谷歌分析请求时存在问题。除非通过 setTimeout 调用用于向 google 发送请求的方法,否则 Firefox 中不会跟踪内部链接。所以我让这段代码正常工作,但离开几个月后发现一位同事完全更改了配置文件并删除了 setTimeout 调用。现在,当我将代码放回(带有附加新变量)时,我在浏览器中收到一个 js 错误,指出 callGA 未定义。这是我的代码:

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 行上。

任何帮助将不胜感激。

Our application has an issue with tracking google analytics requests. Internal links are not tracked in firefox unless the method that is used to send the request to google is called via a setTimeout. So I had this code working but have been away for a few months to find that a colleague completely changed the config file and removed the setTimeout call. Now when I put the code back in (with additional new variables) I get a js error in the browswer saying that callGA is not defined. Here is my code:

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 is being called in a number of locations. I have put an alert in here and its fine, the issue is on the setTimeout line.

Any help would be much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪痕残 2024-12-14 08:54:46

最好不要引用 setTimeout 中的操作。它使用了 eval(),这有点混乱(而且实践不佳)。最好使用匿名函数。

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

It's best not to quote the action in setTimeout. It uses the eval(), which is a bit messy (and poor practice). It's better to use an anonymous function.

setTimeout(function() {
    callGA(id,countryCity,unique,bu,bg,areaClick,uri)
}, 1);
风情万种。 2024-12-14 08:54:46

为什么不把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);
}

Why not put the callGA function inside the 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文