jquery为Google Analytics填充_link()异步语法(跨域)
为了重新讨论这里讨论的内容 ,我希望更新现有的插件以帮助我转换为Google Analytic的“异步语法”,以便将onclick事件应用于我们的出站链接以进行跨域跟踪,如此处所示:
click me
这是我目前使用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“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出站链接跟踪并简单地修改它。
我没有看到旧的_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]); }; }); }; }); });