Google Analytics is not recording navigation using anchors despite inclusion of setAllowAnchor in the script
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 || [];开发者_如何学C
_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 _setAllowAnchor
is mistaken.
_setAllowAnchor
docs is not a feature that enables anchor-aware navigation (or, onhashchange
) tracking. Instead, its for detecting campaign tags in the URL's anchor, so that
http://google.com/#foo&utm_campaign=bar
gets tracked in Google Analytics with "Campaign" set to bar.
Or, as the documentation puts it:
This method sets the # sign as the query string delimiter in campaign tracking. This option is set to false by default.
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 _trackPageview
call, 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:
_gaq.push(["_trackPageview", new_url]);
where new_url is the URL you'd like to be tracked.
Really, you could just defined new_url as:
var new_url = location.pathname+location.search+location.hash
In the new analytics.js it should be
ga('create', 'UA-XXXX-Y', {'allowAnchor': true});
精彩评论