I would like to track the path that leads to a 404 with Google Analytics.
My 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>
The referer 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>
Even after 4 days waiting nothing shows up in my Analytics overview. (I caused some 404 errors myself)
Is anything worng with the code?
You’re mixing the async and the non-async GA code, which can lead to unpredictable results.
The whole latter script block can be replaced by:
However, you may want to consider using event tracking, and not overload your pageview data, since that formatting can be hard to read, difficult to analyze, and means that since you’re sending two pageviews for your 404s, no 404 will ever be recorded as a bounce (which is not likely to be the case.)
This will send a non-interaction event, so not only will it allow you to figure out where your 404s are, it won’t affect your bounce rate, and it will allow you to group data both by the referrer and by the 404’ed landing page .