I am struggling to get google analytics to track navigation using anchor tags. I’ve read that all I need to do is include:
_gaq.push(['_setAllowAnchor', true]);
However this is still not working. I have a 1 page website which uses javascript (jquery) and anchor tags for navigation. When I examine my analytics report all I see is that a user (only me for my website so far) loaded index.html and reloaded it several times, rather than navigated to a specific anchor. My analytics script is below, and is included in the head section. As I say, the script works it just doesn’t track anchor navigation. Are there any work arounds or errors in my script?
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'XXXXXXXXXXX']);
_gaq.push(['_setAllowAnchor', true]);
_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>
Whatever you read about
_setAllowAnchoris mistaken._setAllowAnchordocs is not a feature that enables anchor-aware navigation (or,onhashchange) tracking. Instead, its for detecting campaign tags in the URL’s anchor, so thatgets tracked in Google Analytics with “Campaign” set to bar.
Or, as the documentation puts it:
However, by default, that URL value will still only be tracked in Google Analytics as “/”, since the default method that Google Analytics tracks pageviews is
location.pathname+location.search.Alternatives
To allow for tracking of your internal anchors, you just need to just add a
_trackPageviewcall, with a custom pageview value, into where ever you’re detecting or changing the URL’s anchor, so that it runs any time you’re changing the ‘page’ or the ‘anchor’Basically, that just means adding this line:
where new_url is the URL you’d like to be tracked.
Really, you could just defined new_url as: