尽管脚本中包含了 setAllowAnchor,但 Google Analytics 并未使用锚点记录导航

发布于 2024-12-06 09:56:34 字数 845 浏览 0 评论 0原文

我正在努力让谷歌分析使用锚标签跟踪导航。我读过我需要做的就是包括:

  _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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔一刀 2024-12-13 09:56:34

无论您读到的有关 _setAllowAnchor 的内容都是错误的。

_setAllowAnchor docs 不是一项支持锚点感知导航(或 onhashchange)跟踪的功能。相反,它用于检测 URL 锚点中的活动标签,以便

http://google.com/#foo&utm_campaign=bar

通过 Google Analytics(分析)进行跟踪“活动”设置为栏。

或者,正如文档所说:

此方法将 # 符号设置为营销活动跟踪中的查询字符串分隔符。该选项默认设置为 false。

不过,默认情况下,该 URL 值在 Google Analytics(分析)中仍将仅以“/”形式进行跟踪,因为 Google Analytics(分析)跟踪浏览量的默认方法是 location.pathname+location.search

替代方案

要允许跟踪您的内部锚点,您只需将带有自定义综合浏览量值的 _trackPageview 调用添加到您检测或更改 URL 锚点的位置,以便它运行每当您更改“页面”或“锚点”时

,基本上,这仅意味着添加以下行:

_gaq.push(["_trackPageview", new_url]);

其中 new_url 是您想要跟踪的 URL。

实际上,您可以将 new_url 定义为:

var new_url = location.pathname+location.search+location.hash

Whatever you read about _setAllowAnchor is 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 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
何以心动 2024-12-13 09:56:34

在新的analytics.js中,它应该是

ga('create', 'UA-XXXX-Y', {'allowAnchor': true});

In the new analytics.js it should be

ga('create', 'UA-XXXX-Y', {'allowAnchor': true});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文