尽管脚本中包含了 setAllowAnchor,但 Google Analytics 并未使用锚点记录导航
我正在努力让谷歌分析使用锚标签跟踪导航。我读过我需要做的就是包括:
_gaq.push(['_setAllowAnchor', true]);
但这仍然不起作用。我有一个 1 页网站,它使用 javascript (jquery) 和锚标记进行导航。当我检查我的分析报告时,我看到的是一个用户(到目前为止只有我的网站)加载了index.html并重新加载了多次,而不是导航到特定的锚点。我的分析脚本如下,包含在头部部分。正如我所说,该脚本可以正常工作,只是不跟踪锚点导航。我的脚本中有任何解决方法或错误吗?
<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无论您读到的有关
_setAllowAnchor
的内容都是错误的。_setAllowAnchor
docs 不是一项支持锚点感知导航(或onhashchange
)跟踪的功能。相反,它用于检测 URL 锚点中的活动标签,以便通过 Google Analytics(分析)进行跟踪“活动”设置为栏。
或者,正如文档所说:
不过,默认情况下,该 URL 值在 Google Analytics(分析)中仍将仅以“/”形式进行跟踪,因为 Google Analytics(分析)跟踪浏览量的默认方法是
location.pathname+location.search
。替代方案
要允许跟踪您的内部锚点,您只需将带有自定义综合浏览量值的
_trackPageview
调用添加到您检测或更改 URL 锚点的位置,以便它运行每当您更改“页面”或“锚点”时,基本上,这仅意味着添加以下行:
其中 new_url 是您想要跟踪的 URL。
实际上,您可以将 new_url 定义为:
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 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
_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:
where new_url is the URL you'd like to be tracked.
Really, you could just defined new_url as:
在新的analytics.js中,它应该是
ga('create', 'UA-XXXX-Y', {'allowAnchor': true});
In the new analytics.js it should be
ga('create', 'UA-XXXX-Y', {'allowAnchor': true});