使用Google Analytics执行trackPageview时出现问题 [英] Problem executing trackPageview with Google Analytics

查看:91
本文介绍了使用Google Analytics执行trackPageview时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试捕获某些下载链接的点击次数并在Google Analytics中跟踪它们。这是我的代码

I'm trying to capture the clicks of certain download links and track them in Google Analytics. Here's my code

var links = document.getElementsByTagName("a");
for (var i = 0; i < links.length; i++) {
    linkpath = links[i].pathname;
    if( linkpath.match(/\.(pdf|xls|ppt|doc|zip|txt)$/) || links[i].href.indexOf("mode=pdf") >=0 ){
        //this matches our search
        addClickTracker(links[i]);
    }
}
function addClickTracker(obj){
    if (obj.addEventListener) {
        obj.addEventListener('click', track , true);
    } else if (obj.attachEvent) {
        obj.attachEvent("on" + 'click', track);
    }
}
function track(e){
    linkhref = (e.srcElement) ? e.srcElement.pathname : this.pathname;
    pageTracker._trackPageview(linkhref);

 }

在pageTracker._trackPageview()调用工作之前的所有内容。在我的调试中,linkhref正在以字符串形式传递。没有异常的人物,没什么。问题是,在观看我的http请求时,Google从不再对跟踪gif进行第二次调用(如果你在onclick属性中调用此函数的话)。从我的JS控制台调用跟踪器也可以按预期工作。这只是在我的听众中。

Everything up until the pageTracker._trackPageview() call works. In my debugging linkhref is being passed fine as a string. No abnormal characters, nothing. The issue is that, watching my http requests, Google never makes a second call to the tracking gif (as it does if you call this function in an "onclick" property). Calling the tracker from my JS console also works as expected. It's only in my listener.

在有机会联系Google的服务器之前,我的听众是不是推迟默认操作(加载新页面)?我已经看到其他跟踪脚本执行类似的事情,没有任何延期。

Could it be that my listener is not deferring the default action (loading the new page) before it has a chance to contact Google's servers? I've seen other tracking scripts that do a similar thing without any deferral.

推荐答案

尝试

pageTracker._trackPageview('/ pagex / downloadlink.html')

pageTracker._trackPageview('/pagex/downloadlink.html')

此外,只是为了好玩,请确保在此之前首先加载GA代码脚本。有时GA是挑剔而且很奇怪。

Also, just for fun make sure the GA code is loaded first before this script. Sometime GA is picky and weird like that.

如果有帮助请告诉我

@ctrentmarketing

@ctrentmarketing

这篇关于使用Google Analytics执行trackPageview时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆