jquery为Google Analytics填充_link()异步语法(跨域)

时间:2011-05-04 15:36:54

标签: jquery google-analytics analytics tracking

要退回what was discussed here,我希望更新现有的插件,以帮助我转换为Google Analytic的“异步语法”,以便onclick事件可以应用于我们的跨域出站链接跟踪如下所示:

<a href="http://example.com/test.html" onclick="_gaq.push(['_link', 'http://example.com/test.html']); return false;">click me</a>

这是我使用jquery跟踪出站链接的当前实现,我希望可以对其进行修改以支持Google Analytic的“异步语法”

$(document).ready(function(){
    $('a:not(.popupwindow)').filter(function() {
            var theHref = this;
            if (theHref.hostname && theHref.hostname !== location.hostname) {
                $(theHref).not(".noAutoIcon").addClass("offSite");
                $(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
                    var code=event.charCode || event.keyCode;
                    if (!code || (code && code == 13)) {
                        if(pageTracker){
                            var fixedLink = this.href;
                            fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
                            fixedLink = '/outgoing/' + fixedLink;
                            pageTracker._trackPageview(fixedLink);
                        };
                    };
                });
            };
        });
});

当用户点击example.commysite.com时,如果两个网站都被挖掘,则Cookie信息将由_link传递,并且全部被视为一次访问。

这是我们当前 Google Analytics代码:

    try {
    var pageTracker = _gat._getTracker("UA-111222333-1");
    pageTracker._setDomainName(".example.com");
    pageTracker._setAllowLinker(true);
    pageTracker._setAllowHash(false); 
    pageTracker._trackPageview();
    } catch(err) {}

这是我的 Google Analytics“Analytics异步”代码

var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-111222333-1']);
  _gaq.push(['_setXDomain', {  
  domainName: '.example.com',
  include: /(firstsite.com|secondsite.com)/
  }]);
  _gaq.push(['_trackOutbound']);
  _gaq.push(['_trackDownload']);
  _gaq.push(['_trackMailTo']);
  _gaq.push(['_trackError']);
  _gaq.push(['_formAnalysis',{minFields: 3}]);
  _gaq.push(['_setDayOfWeek']);
  _gaq.push(['_trackPageview']);

我的网站在CMS下运行并且无法手动向链接添加onclick事件,所以我需要使用jquery这样做,这就是为什么我希望利用我们现有的jquery出站链接跟踪并简单地修改它

1 个答案:

答案 0 :(得分:0)

我没有看到旧的_link电话,但我假设它刚好在_trackPageview之后。迁移非常简单。

$(document).ready(function(){
    $('a:not(.popupwindow)').filter(function() {
        var theHref = this;
        if (theHref.hostname && theHref.hostname !== location.hostname) {
            $(theHref).not(".noAutoIcon").addClass("offSite");
            $(theHref).not(".noAutoLink").attr('target','_blank').bind('click keypress', function(event) {
                var code=event.charCode || event.keyCode;
                if (!code || (code && code == 13)) {
                    var fixedLink = this.href;
                    fixedLink = fixedLink.replace(/https?:\/\/(.*)/,"$1");
                    fixedLink = '/outgoing/' + fixedLink;
                    _gaq.push(['_trackPageview', fixedLink]);
                    _gaq.push(['_link', this.href]);
                };
            });
        };
    });
});