我从外部js加载以下内容,该js跟踪Google Analytics中的pdf和zip点击次数。 pdfOnClick函数始终工作,但zipOnClick函数间歇性地工作。外部Jquery负载 - 第一个OnClick函数始终工作第二个OnClick函数间歇工作
(function() {
function loadScript(url, success) {
var script = document.createElement('script');
script.src = url;
var head = document.getElementsByTagName('head')[0],
done = false;
script.onload = script.onreadystatechange = function() {
if (!done && (!this.readyState || this.readyState == 'loaded' || this.readyState == 'complete')) {
done = true;
success();
script.onload = script.onreadystatechange = null;
head.removeChild(script);
}
};
head.appendChild(script);
}
loadScript('http://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js',
function() {
$("a[href$='pdf']").each(function (index) {
pdfLabel = $(this).attr('href');
pdfOnClick = "ga('send','event','PDF','downloaded','" + pdfLabel + "');";
$(this).attr("onClick", pdfOnClick);
});
$("a[href$='zip']").each(function (index) {
zipLabel = $(this).attr('href');
zipOnClick = "ga('send','event','ZIP','downloaded','" + zipLabel + "');";
$(this).attr("onClick", zipOnClick);
});
});
})();
我创建了一个jsfiddle来查看jshint是否显示了任何问题,但它回来干净。
我的问题是你看到pdfOnClick函数始终工作的原因,但zipOnClick不?
http://jsfiddle.net/cbww3zr3/2/
@osowskit你提到处理用户未启用JavaScript。难道是最好的报废jQuery和只是将其分配到的动作是这样的:
<a onclick="ga('send','event','zip','downloaded','label',{'hitCallback': function() {document.location = 'http://example.com/test.zip';}}); return false;" href="http://example.com/test.zip" target=_blank>Click Here</a>
要正常工作,GA hitCallback必须阻止页面导航,直到GA事件回调返回。在没有JS的会话中,这会破坏导航。关键是要处理这种情况下禁用JS - 如果你觉得这是必要的。 – osowskit 2014-09-09 18:42:02
@osowskit - 我想我需要找出背后的现实,如果我真的需要它,然后浪费更多的时间。不知道如何确定有多少用户禁用了js。在昨晚看了看周围,但没有找到任何关于这个。将与我现在已经发布的jquery一起,因为添加target = _blank修复了最初的问题。如果我需要扩展这个,我会发布另一个问题。再次感谢您的回复。 – 2014-09-09 19:38:28