jquery Populate _link() for Google Analytics 异步语法(跨域)
为了借鉴这里讨论的内容,我我希望更新现有的插件,以帮助我切换到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到旧的
_link
调用,但我假设它就在_trackPageview
之后。迁移非常简单。I don't see the old
_link
call but I'm assuming it's just after the_trackPageview
. The migration is pretty straightforward.