Google Analytics(分析) - Track 404 referer

Tom*_*Tom 2 javascript analytics tracking google-analytics

我想跟踪通过Google Analytics获取404的路径.

我的Google Analytics JS:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-11111111-1']);
  _gaq.push(['_setDomainName', 'example.com']);
  _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);
  })();
</script>
Run Code Online (Sandbox Code Playgroud)

参考者JS:

<script type="text/javascript">
 try{
 var pageTracker = _gat._getTracker("UA-11111111-1");
pageTracker._trackPageview("/404.html?page=" + document.location.pathname + document.location.search + "&from=" + document.referrer);
 } catch(err) {}
 </script>
Run Code Online (Sandbox Code Playgroud)

即使在等待4天后,我的Google Analytics概述中也没有显示任何内容.(我自己造成了404错误)代码有什么问题吗?

Yah*_*hel 7

您正在混合异步和非异步GA代码,这可能导致不可预测的结果.

整个后一个脚本块可以替换为:

_gaq.push(["_trackPageview", "/404.html?page=" + document.location.pathname +     document.location.search + "&from=" + document.referrer]);
Run Code Online (Sandbox Code Playgroud)

但是,您可能需要考虑使用事件跟踪,而不是重载您的网页浏览数据,因为该格式可能难以阅读,难以分析,并且意味着由于您要为404发送两次网页浏览,因此不会有404记录为反弹(不太可能是这种情况.)

_gaq.push(["_trackEvent", "404", location.pathname + location.search, document.referrer, 0, true]);
Run Code Online (Sandbox Code Playgroud)

这将发送一个非交互事件,因此它不仅可以让您弄清楚404的位置,它不会影响您的跳出率,并且它允许您通过引荐来源和404'对数据进行分组ed登陆页面.