jquery Populate _link() for Google Analytics Asynchronous syntax (cross domain)

旧城冷巷雨未停 提交于 2019-12-24 09:59:20

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/5886015/jquery-populate-link-for-google-analytics-asynchronous-syntax-cross-domain

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!