jquery Populate _link() for Google Analytics Async

2019-08-04 19:24发布

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条回答
孤傲高冷的网名
2楼-- · 2019-08-04 19:48

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]);
                };
            });
        };
    });
});
查看更多
登录 后发表回答