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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4811172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:12:03  来源:igfitidea点击:

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

javascriptjquerygoogle-analytics

提问by Dave

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

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

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.

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

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?

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

回答by Yahel

Generically, your code could look like this

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

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

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.

您可以在每次在应用程序中进行哈希更改时将该代码绑定到,或者您可以使用通用的 hashchange 插件,该插件使用 HTML5 onhashchange 和一些用于旧浏览器的向后兼容的 hack,并将此代码绑定到该事件,以便每次哈希更改时都会触发。

Using that plugin, your code could look like:

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

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

})



UPDATE 2014:2014 年更新:

This is how you'd do this in the new Universal Analytics:

这是您在新的 Universal Analytics 中执行此操作的方式:

ga('send', 'pageview', {
 'page': location.pathname + location.search  + location.hash
});

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

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

  • Go to your macros
  • Updated the URL Macro to "Fragment"
  • 转到您的宏
  • 将 URL 宏更新为“片段”

回答by katjam

Looks like this might be useful too: https://developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications

看起来这也可能有用:https: //developers.google.com/analytics/devguides/collection/analyticsjs/single-page-applications

Very helpful with clear 'What to do' and 'What not to do'

明确“做什么”和“不做什么”非常有帮助

回答by Gareth

Google Analytics allows you to track custom events, for example AJAX page loads.

Google Analytics 允许您跟踪自定义事件,例如 AJAX 页面加载。

(The usual caveats apply when doing this - hopefully there are non-javascript ways to access the same data :)

(执行此操作时适用通常的警告 - 希望有非 javascript 方法可以访问相同的数据:)

回答by keatch

Good question. To track the hash link, you must track an event or a pageview, for every link to this hash. For the pageView, a sample code is below

好问题。要跟踪散列链接,您必须针对指向此散列的每个链接跟踪事件或综合浏览量。对于 pageView,示例代码如下

onclick="_gaq.push(['_trackPageview','/page/hashLink1']);"

Note: This method create a virtual page view that is summing up to the count of the pages of your site. If your site is a big html files with anchors (maybe there is a slider to this page), this method gives you an estimated of the interaction of the user with your "content"

注意:此方法创建一个虚拟页面视图,该视图汇总到您网站的页面数。如果您的网站是一个带有锚点的大 html 文件(也许此页面有一个滑块),则此方法可让您估计用户与您的“内容”的交互

回答by benpalmer

For new universal tracking this doesn't work anymore. You will have to go to https://developers.google.com/analytics/devguides/collection/analyticsjs/eventsand update to something like

对于新的通用跟踪,这不再起作用。你将不得不去https://developers.google.com/analytics/devguides/collection/analyticsjs/events并更新到类似的东西

ga('send', 'event', 'category', 'action', {'page': '/my-new-page'});