简体   繁体   English

使用Google Analytics(分析)执行trackPageview时发生问题

[英]Problem executing trackPageview with Google Analytics

I'm trying to capture the clicks of certain download links and track them in Google Analytics. 我正在尝试捕获某些下载链接的点击并在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);

 }

Everything up until the pageTracker._trackPageview() call works. 直到pageTracker._trackPageview()调用起作用为止的所有操作。 In my debugging linkhref is being passed fine as a string. 在我的调试linkhref被传递罚款为字符串。 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). 问题在于,看着我的http请求,Google不会再调用跟踪gif(就像您在“ onclick”属性中调用此函数一样)。 Calling the tracker from my JS console also works as expected. 从我的JS控制台调用跟踪器也可以正常工作。 It's only in my listener. 只在我的听众中。

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. 我看过其他跟踪脚本,它们执行类似的操作而没有任何延迟。

Try 尝试

pageTracker._trackPageview('/pagex/downloadlink.html') pageTracker._trackPageview( '/ pageX属性/ downloadlink.html')

Also, just for fun make sure the GA code is loaded first before this script. 此外,仅出于娱乐目的,请确保在此脚本之前先加载GA代码。 Sometime GA is picky and weird like that. 有时候,GA有点挑剔和怪异。

Let me know if that helps 让我知道是否有帮助

@ctrentmarketing @ctrentmarketing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM