如何使用 Google Analytics 跟踪锚标记
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
默认情况下,Google Analytics(分析)会禁用跟踪锚标记< /a>. 要为旧跟踪代码启用它,请使用以下形式:
为变量
_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:
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.简而言之,Google Analytics(分析)无法跟踪包含“#”字符的页面链接。 对index.html#foo 的点击被简单地视为对index.html 的点击,“#”之后的所有内容都将被忽略。
您可以考虑转义“#”字符:
在您的示例中,这将记录
/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:
In your example, this would record a page view for
/tab/?main
, which should work fine with Google Analytics.如果您想跟踪页内锚点点击,则以下操作将在全局范围内起作用:
请注意,如果您有一些特定的
onclick
处理程序,例如您需要手动添加哈希值以触发
onhashchange
If you want to track in-page anchor clicks, the following would work globally:
Beware that if you have some specific
onclick
handlers such asYou would need to manually prepend the hash to trigger the
onhashchange
听起来您想要 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.
我尝试使用
_uanchor = 1;
方法。 这不起作用。 它不起作用的原因是它旨在以哈希值形式发送 Google Analytics(分析)参数,而不是发送您的哈希值。从Török Gábor 的回答引用的指南中,将
_uanchor
设置为 1 具有以下效果效果:<块引用>
启用后,使用
#
而不是默认的?
将请求主干与查询字符串分开这位于“广告系列跟踪”部分。
Rob Knight 的答案效果很好,但我做了一个小修改。 在伪代码中:
不确定对于 GA 来说拥有像 foo.html&bar=baz 这样的 URL 是否真的很重要,但对我来说它看起来更干净。
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:this is in the 'Campaign Tracking' section.
Rob Knight's answer works fine, but I made a small modification. In pseudo code:
Not sure if it really matters to GA to have a URL like
foo.html&bar=baz
but it just seemed cleaner to me.https://developers.google.com/analytics/devguides/collection/gajs /方法/gaJSApiCampaignTracking
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiCampaignTracking