我可能在leastartupcircle.com搞砸了onClick
javascript 。我正在尝试将使用 javascript 的链接跟踪添加到我用属性标记的所有出站链接。我所做的一切都是阻止运作,也可能不会触发事件。target="_blank"
另一条信息target="_blank"
在暂存和开发方面工作良好,因为我没有在这些服务器上显示谷歌分析初始化代码。这就是为什么我认为我的 javascript 不正确。
可以用一双更好的眼睛。我究竟做错了什么?
function parseUrl( url ) {
var a = document.createElement('a');
a.href = url;
return a;
};
function getDomain( url ) {
hostname = parseUrl(link).hostname
splitHostname = hostname.split('.');
if (splitHostname.length > 2) {
domain = splitHostname[1] // Most likely the domain
} else {
domain = splitHostname[0]
};
return domain;
};
function trackLinks() {
// Setup outbound link tracking and push events to GA based on link attribute linkTracking
$('a').each(function () {
link = $(this).attr('href');
linkTracking = $(this).attr('linkTracking');
if (linkTracking) {
category = linkTracking.toLowerCase()
action = getDomain(link);
label = link;
$(this).attr('target', '_blank').attr('onClick', '_gaq.push(["_link", "' + link + '"]); return false; ' +
'recordOutboundLink(this, "' + category + '", "' + action + '", "' + label + '"); return false;');
};
});
};
这是我的谷歌分析代码。为了使用跨域跟踪,谷歌建议进行一些修改:
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]); // Enhanced Link Attribution
_gaq.push(['_setAccount', 'UA-30468280-1']);
_gaq.push(['_setDomainName', 'leanstartupcircle.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
// Delay outbound page load in order to record outbound links
function recordOutboundLink(link, category, action) {
try {
var myTracker=_gat._getTrackerByName();
_gaq.push(['myTracker._trackEvent', category , action ]);
setTimeout('document.location = "' + link.href + '"', 100)
}catch(err){}
}