jquery Populate _link() for Google Analytics 异步语法(跨域)

发布于 2024-11-04 21:41:15 字数 2504 浏览 8 评论 0原文

为了借鉴这里讨论的内容,我我希望更新现有的插件,以帮助我切换到 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.com 点击到 mysite.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“异步分析”代码

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 出站链接跟踪并对其进行简单的修改。

To piggy back off what was discussed here, I'm looking to have an existing plug-in updated to help switch me to Google Analytic's "Asynchronous syntax" so onclick events could be applied to our outbound links for cross-domain tracking as shown here:

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

This is my current implementation to track outbound links with jquery, which I was hoping could be modified to support Google Analytic's "Asynchronous syntax"

$(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);
                        };
                    };
                });
            };
        });
});

When a user should click from example.com to mysite.com, with both sites mine, the cookie information would get passed by _link and it would all be considered one visit.

This is our current Google Analytics code:

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

This is my new Google Analytics "Analytics Asynchronous" code

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']);

My site operates under a CMS and adding onclick events to links manually is not possible, so I need to do so with jquery, which is why I was hoping to take advantage of our existing jquery outbound link tracking and have it simply modified.

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

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

发布评论

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

评论(1

请帮我爱他 2024-11-11 21:41:15

我没有看到旧的 _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]);
                };
            });
        };
    });
});

I don't see the old _link call but I'm assuming it's just after the _trackPageview. The migration is pretty straightforward.

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