在下面的代码中,我试图:-
- 在页面上定义谷歌分析
- 如果页面有特定的查询字符串和域中的字符,添加一个jquery点击事件
- 点击事件调用谷歌分析跟踪事件
- 我还对域中的查询字符串和值进行代码检查
什么不起作用:-
- 第三项是调用 Google Analytics 跟踪事件,我似乎点击了它,但没有任何内容返回到我的 GA 帐户中。
我尝试过的:-
- 我已经三次检查
_gaq_.push
的信息是否正确链接到正确的帐户。 - 使用 firebug 和 IE9 开发人员工具来跟踪 javascript 并分析正在发生的事情。当我的场景为真时,这就是我知道我正在触发事件跟踪的方式。
- 在像 jsfiddle 这样的孤立环境中尝试过,不会提供指向它的链接,因为我实际上无法提供公司信息。它在 js fiddle 中不起作用。 (主要是因为 jsfiddle 无法使用我的 ga 帐户进行身份验证(域不同)。
我已经在相关域中的一个独立文件中尝试过,但仍然没有成功。
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-XXXXX-X']); _gaq.push(['_setDomainName', 'mydomain.co.uk']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_setSiteSpeedSampleRate', 100]); // this is a new line, allowing us to see how fast all pages load _gaq.push(['_trackPageview']); // we’ve moved this line down, as ‘setdomain’ (etc) should appear before it (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); })(); $(document).ready(function () { var querystring = (function (a) { if (a == "") return {}; var b = {}; for (var i = 0; i < a.length; ++i) { var p = a[i].split('='); if (p.length != 2) continue; b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " ")); } return b; })(window.location.search.substr(1).split('&')); if (querystring["utm_expid"] != null) { $('a').click(function () { if ($(this).attr("href") != 'undefined' && $(this).attr("href") != null) { if ($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0) { _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']); } else if (($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword3") >= 0) || ($(this).attr("href").toLowerCase().indexOf("keyword4") >= 0)) { _gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']); } } }); } });
最佳答案
Google Analytics _trackEvent
(和 _trackPageview
等)通过从分析服务器发出跟踪像素请求来工作。如果点击导致在跟踪请求完成之前将新页面加载到同一窗口,您可能会丢失数据,或者只跟踪部分数据。
以下代码在点击链接之前添加了轻微的延迟:
var delayLink = function(e, url) {
e.preventDefault();
setTimeout(function(){location.href = url}, 150);
};
if (querystring["utm_expid"] != null) {
$('a').click(function (e) {
if ($(this).attr("href") != 'undefined' && $(this).attr("href") != null) {
if ($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0) {
_gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
if (this.target != '_blank') delayLink(e, this.href);
} else if (($(this).attr("href").toLowerCase().indexOf("keyword") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword2") >= 0 && $(this).attr("href").toLowerCase().indexOf("keyword3") >= 0) || ($(this).attr("href").toLowerCase().indexOf("keyword4") >= 0)) {
_gaq.push(['_trackEvent', 'eventCategories', 'eventAction', 'eventLabel']);
if (this.target != '_blank') delayLink(e, this.href);
}
}
});
}
关于javascript - Google Analytics 事件跟踪无法反馈?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12671774/