如何使用 Google Analytics 跟踪锚标记

发布于 2024-07-30 08:32:39 字数 595 浏览 5 评论 0原文

我正在尝试通过 Google Analytics 跟踪不会产生新请求的点击次数。 具体来说,单击通过 jQuery UI 选项卡小部件 创建的选项卡。 我正在使用旧版本的代码(“urchin tracker”)并尝试像这样记录点击:

$('.ui-tabs-nav li a').click(function() {
    val = "/tab/" + $(this).attr('href');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

在另一个实例中,相同的方法有效,据我所知,其唯一的显着区别是缺乏字符串中的井号 (#) 符号。 urchinTracker() 跟踪的字符串中是否不允许使用该字符,或者可能有其他原因(除了没有人点击链接!)?

I'm trying to track clicks via Google Analytics that do not result in a new request. Specifically, clicks on tabs that are created via the jQuery UI tabs widget. I'm using the older version of the code ('urchin tracker') and trying to log the clicks like so:

$('.ui-tabs-nav li a').click(function() {
    val = "/tab/" + $(this).attr('href');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

The same method works, in another instance, whose only significant difference, as far as I can tell, is the lack of a hash (#) symbol in the string. Is that character not allowed in a string tracked by urchinTracker(), or could there be some other cause (other than no-one having clicked on the links!)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

找回味觉 2024-08-06 08:32:39

默认情况下,Google Analytics(分析)会禁用跟踪锚标记< /a>. 要为旧跟踪代码启用它,请使用以下形式:

_uanchor = 1;
urchinTracker(val);

为变量 _uanchor 设置值相当于使用最新 GA 代码库时调用方法 _setAllowAnchor。 (您可以在 Google Analytics(分析)中找到详细的比较跟踪代码迁移指南 - 此链接已过时)。

Google 参考网站

By default, Google Analytics disables tracking anchor tags. To enable it for the legacy tracking code, use the following form:

_uanchor = 1;
urchinTracker(val);

Setting a value to variable _uanchor is the equivalent of calling method _setAllowAnchor when using the latest GA code base. (You find a detailed comparison in the Google Analytics Tracking Code Migration Guide - this link is outdated).

The method _setAllowAnchor is described on the Google Reference Site.

轻拂→两袖风尘 2024-08-06 08:32:39

简而言之,Google Analytics(分析)无法跟踪包含“#”字符的页面链接。 对index.html#foo 的点击被简单地视为对index.html 的点击,“#”之后的所有内容都将被忽略。

您可以考虑转义“#”字符:

$('.ui-tabs-nav li a').click(function() {
    val = new String("/tab/" + $(this).attr('href')).replace('#', '&');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

在您的示例中,这将记录 /tab/?main 的页面视图,这应该可以与 Google Analytics 配合使用。

In short, Google Analytics can't track on-page links containing a '#' character. A click on index.html#foo is treated simply as a click on index.html, with everything after the '#' ignored.

You could consider escaping the '#' character:

$('.ui-tabs-nav li a').click(function() {
    val = new String("/tab/" + $(this).attr('href')).replace('#', '&');

    // when uncommented, the following line reports, for example:
    //   /tab/#main
    // as expected.
    // console.log(val);

    res = urchinTracker(val);
});

In your example, this would record a page view for /tab/?main, which should work fine with Google Analytics.

白日梦 2024-08-06 08:32:39

如果您想跟踪页内锚点点击,则以下操作将在全局范围内起作用:

window.onhashchange = function() {
  _gaq.push(['_trackPageview', location.pathname + location.search + location.hash]);
}

请注意,如果您有一些特定的 onclick 处理程序,例如

 $('#someid a').click(function(event){
   event.preventDefault();
   var target = $(this).attr('href');
   // some more clever stuff
 });

您需要手动添加哈希值以触发 onhashchange

 $('#someid a').click(function(event){
   event.preventDefault();
   var target = $(this).attr('href');
   // triggers onhashchange
   location.hash = target;
   // some more clever stuff
 });

If you want to track in-page anchor clicks, the following would work globally:

window.onhashchange = function() {
  _gaq.push(['_trackPageview', location.pathname + location.search + location.hash]);
}

Beware that if you have some specific onclick handlers such as

 $('#someid a').click(function(event){
   event.preventDefault();
   var target = $(this).attr('href');
   // some more clever stuff
 });

You would need to manually prepend the hash to trigger the onhashchange

 $('#someid a').click(function(event){
   event.preventDefault();
   var target = $(this).attr('href');
   // triggers onhashchange
   location.hash = target;
   // some more clever stuff
 });
染火枫林 2024-08-06 08:32:39

听起来您想要 Google Analytics 事件跟踪示例使用 onclick ="" 垃圾,但我打赌你也可以将它们绑定到 jQuery 单击事件。

Sounds like you want Google Analytics Event Tracking the examples use onclick="" rubbish but I bet you could probably bind these to jQuery click events as well.

时间你老了 2024-08-06 08:32:39
  1. 我尝试使用_uanchor = 1;方法。 这不起作用。 它不起作用的原因是它旨在以哈希值形式发送 Google Analytics(分析)参数,而不是发送您的哈希值。

    Török Gábor 的回答引用的指南中,将 _uanchor 设置为 1 具有以下效果效果:

    <块引用>

    启用后,使用 # 而不是默认的 ? 将请求主干与查询字符串分开

    这位于“广告系列跟踪”部分。

  2. Rob Knight 的答案效果很好,但我做了一个小修改。 在伪代码中:

    if (href 包含 ?) { 
      href = href.replace('#', '&'); 
      } 别的 { 
      href = href.replace('#', '?'); 
      } 
      

不确定对于 GA 来说拥有像 foo.html&bar=baz 这样的 URL 是否真的很重要,但对我来说它看起来更干净。

  1. I tried using the _uanchor = 1; method. That does not work. The reason it does not work is that it is designed to send the Google Analytics parameters as hash values, not send your hash value.

    From the guide quoted in Török Gábor's answer, setting _uanchor to one has the following effects:

    When enabled, uses # instead of the default ? to separate the request stem from the query string

    this is in the 'Campaign Tracking' section.

  2. Rob Knight's answer works fine, but I made a small modification. In pseudo code:

    if (href contains ?) {
    href = href.replace('#', '&');
    } else {
    href = href.replace('#', '?');
    }
    

Not sure if it really matters to GA to have a URL like foo.html&bar=baz but it just seemed cleaner to me.

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