我正在使用 ga.js。我的代码正在记录一些内部链接,但在我测试过的页面上,内部链接显示的事件总数为 1,而实际上应该是 50。我还查看了事件中的实时报告。我所有的外在都会立即显现出来,但我的内在却没有。我不明白为什么除了内部链接之外一切都有效。我的代码如下,
<script type="text/javascript">
if (typeof jQuery != 'undefined') {
jQuery(document).ready(function ($) {
var pdffiletype = /\.(pdf)$/i;
var filetypes = /\.(zip|exe|doc*|xls*|ppt*|mp3)$/i;
var baseHref = '';
if (jQuery('base').attr('href') != undefined)
baseHref = jQuery('base').attr('href');
jQuery('a').each(function () {
var href = jQuery(this).attr('href');
if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
jQuery(this).click(function () {
var extLink = href.replace(/^https?\:\/\//i, '');
_gaq.push(['_trackEvent', 'External', 'Click', extLink]);
if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
setTimeout(function () { location.href = href; }, 200);
return false;
}
});
}
else if (href && href.match(/^javascript\:/i)) {
//do not track
return true;
}
else if (href && href.match(/^mailto\:/i)) {
jQuery(this).click(function () {
var mailLink = href.replace(/^mailto\:/i, '');
_gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
});
}
else if (href && href.match(pdffiletype)) {
jQuery(this).click(function () {
var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
var filePath = href;
_gaq.push(['_trackEvent', 'PDF', 'Click', $(this).html(), 0]);
if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
setTimeout(function () { location.href = baseHref + href; }, 200);
return false;
}
});
}
else if (href && href.match(filetypes)) {
jQuery(this).click(function () {
var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
var filePath = href;
_gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
if (jQuery(this).attr('target') != undefined && jQuery(this).attr('target').toLowerCase() != '_blank') {
setTimeout(function () { location.href = baseHref + href; }, 200);
return false;
}
});
}
else if (href) {
//assume internal link
_gaq.push(['_trackEvent', 'Internal', 'Click', href]);
}
});
});
}
</script>
最佳答案
只需更改此 ligne : _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);到 ga('发送', '事件', '下载', '点击', filePath); 它再次为我工作
关于javascript - Google Analytics trackEvent 内部链接无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29295149/