是否可以使用谷歌分析跟踪页面等哈希链接? [英] Is it possible to track hash links like pages with google analytics?

查看:26
本文介绍了是否可以使用谷歌分析跟踪页面等哈希链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用谷歌分析跟踪页面等哈希链接?

Is it possible to track hash links like pages with google analytics?

例如,我希望 index.php/#1、index.php/#2 和 index.php/#3 都显示为单独的页面点击次数以及在页面上花费的单独时间.

For example, I want index.php/#1, index.php/#2, and index.php/#3 to all show up as individual page hits with individual time spent on page.

如果没有简单的方法可以做到这一点,如何使用 jquery 将跟踪事件添加到 onclick 事件?我还能通过这种方式在页面"信息上获得准确的时间吗?

If there is no simple way of doing this, how can I add a track event to an onclick event with jquery? Can I still receive accurate time on "page" information this way?

推荐答案

通常,您的代码可能如下所示

Generically, your code could look like this

_gaq.push(['_trackPageview',location.pathname + location.search  + location.hash]);

您可以在每次在应用程序中进行哈希更改时绑定该代码,也可以使用 通用 hashchange 插件,它使用 HTML5 onhashchange 和一些针对旧浏览器的向后兼容黑客,并将此代码绑定到该事件,以便在每次哈希更改时触发.

You could either bind that code to every time you have a hash change within your application, or you could use a generic hashchange plugin, that uses the HTML5 onhashchange, and some backwards compatible hacks for older browsers, and bind this code to that event, so that it fires every time your hash changes.

使用该插件,您的代码可能如下所示:

Using that plugin, your code could look like:

$(window).hashchange( function(){
    _gaq.push(['_trackPageview',location.pathname + location.search  + location.hash]);

})

<小时>** 2014 年更新:**

这就是您在新的通用分析:

ga('set', 'page', location.pathname + location.search  + location.hash);
ga('send', 'pageview');

来自 Google Analytics 文档的说明:

Note from Google Analytics documentation:

虽然从技术上讲,用于网页浏览点击的 send 命令接受一个可选的 page 字段作为第三个参数,但以这种方式传递 page 字段并不是在衡量单页应用程序时推荐使用.

While technically the send command for pageview hits accepts an optional page field as the third parameter, passing the page field that way is not recommended when measuring single page applications.

如果您在 Google Tag Manager 中使用 Google Analytics,您会这样做:

This is how you'd do it if you're using Google Analytics within Google Tag Manager:

  • 转到您的宏
  • 将 URL 宏更新为片段"

这篇关于是否可以使用谷歌分析跟踪页面等哈希链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆