【发布时间】:2012-09-06 14:25:08
【问题描述】:
几个月来,我一直在使用脚本来跟踪出站链接。 脚本可以运行,但在 Google Analytics(分析)生成的报告中,许多 URL 的末尾都有一个尾随“:80”(默认端口号)。继续阅读以了解更多详情。
可能需要提及的是,跟踪这些出站链接的网站有大量的出站流量(将您的幻想乘以 ∞)。
脚本的目的
它会跟踪所有出站链接,并在 Google Analytics 中将它们标记为“出站链接”。
该脚本有大量注释,并且有一些 console.log() 实例来帮助调试(这些都被注释掉了)。
“出站链接”显示在 GA 上,如下:
内容 > 事件 > 热门事件 > “出站链接”[点击它] > [报告显示所有点击的网址]
问题
在“出站链接”报告下,我得到了所有被点击的链接,我在报告的所有链接的至少 2/3(可能更多)的末尾得到“:80”。 GA 将 http://example.com 和 http://example.com:80 视为不同的链接,在报告中将它们分开。 这当然是不希望的。
值得一提:
以“:80”结尾的链接总是比没有“:80”的链接有更多的点击率,点击率高出 40% 到 60%。
想要的解决方案
- 将以“:80”结尾的链接与没有它的链接合并,或者
- 尽可能避免在链接中附加“:80”。
- 奖励:了解为什么我们会得到以“:80”结尾的链接。
脚本
// Outbound Link Tracking with Google Analytics
// Requires jQuery 1.7 or higher (use .live if using a lower version)
$(function() {
$("a").on('click',function(e){
var url = $(this).attr("href");
// Console logs shows the domain name of the link being clicked and the current window
// console.log('e.currentTarget.host: ' + e.currentTarget.host);
// console.log('window.location.host: ' + window.location.host);
// If the domains names are different, it assumes it is an external link
// Be careful with this if you use subdomains
if (e.currentTarget.host != window.location.host) {
// console.log('external link click');
// Outbound link! Fires the Google tracker code.
_gat._getTrackerByName()._trackEvent("Outbound Links", e.currentTarget.host, url, 0);
// Checks to see if the ctrl or command key is held down
// which could indicate the link is being opened in a new tab
if (e.metaKey || e.ctrlKey) {
// console.log('ctrl or meta key pressed');
var newtab = true;
}
// If it is not a new tab, we need to delay the loading
// of the new link for a just a second in order to give the
// Google track event time to fully fire
if (!newtab) {
// console.log('default prevented');
e.preventDefault();
// console.log('loading link after brief timeout');
setTimeout('document.location = "' + url + '"', 100);
}
}
/*
else {
console.log('internal link click');
}
*/
});
});
【问题讨论】:
-
非常有价值。请考虑为后代显示编辑后的代码。
标签: javascript jquery google-analytics outbound