我已设置Google Analytics事件以跟踪出站链接。我跟着the tutorial on Google's site。
我遇到的问题是Google Analytics中没有跟踪'url'参数(事件标签)。 'outbound'和'click'都可以通过ga()调用正常工作。如果我在ga()调用之前警告()行上的'url'变量,它将成功提醒屏幕。
为什么Category(出站)和Action(点击)工作正常。但是,标签(var url)不会显示在Google Analytics中?
jQuery(document).ready(function() {
initExternalLinkLogging();
});
function initExternalLinkLogging() {
// ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external
externalLinks = jQuery("a:external");
jQuery(externalLinks).click(function() {
trackOutboundLink(this);
});
}
function trackOutboundLink(url) {
// This is the triggered on each outbound link click successfully
// 'url' has the correct value, but is not showing up in Google Analytics
try {
ga('send', 'event', 'outbound', 'click', url, {
'hitCallback': function() {
document.location = url;
}
});
} catch (err) {
// Do nothing for now
}
}
答案 0 :(得分:0)
我已经解决了这个问题。在此上下文中,'url'变量是一个对象,即使屏幕上的警报将其显示为URL的字符串。只需更改此函数调用即可解决问题。
//This doesn't work
trackOutboundLink(this);
//This fixes it. See ".href"
trackOutboundLink(this.href);
因此,在工作状态下引用的完整代码是:
jQuery(document).ready(function() {
initExternalLinkLogging();
});
function initExternalLinkLogging() {
// ':external' is from here and works great: http://stackoverflow.com/questions/1227631/using-jquery-to-check-if-a-link-is-internal-or-external
externalLinks = jQuery("a:external");
jQuery(externalLinks).click(function() {
trackOutboundLink(this);
});
}
function trackOutboundLink(url) {
// This is the triggered on each outbound link click successfully
// 'url' has the correct value, but is not showing up in Google Analytics
try {
ga('send', 'event', 'outbound', 'click', url, {'hitCallback':
function() {
document.location = url;
}
});
} catch (err) {
}
}