使用 setTimeout 调用函数时出现问题
我们的应用程序在跟踪谷歌分析请求时存在问题。除非通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最好不要引用 setTimeout 中的操作。它使用了 eval(),这有点混乱(而且实践不佳)。最好使用匿名函数。
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.
为什么不把
callGA
函数放在setTimeout里面呢?Why not put the
callGA
function inside the setTimeout?