4/22/2011: This blog post was completely rewritten to describe page tracking with Analytics' asynchronous loading method.
2011年4月22日:这篇博客文章已被完全重写,以描述使用Google Analytics(分析)的异步加载方法进行页面跟踪。
With more and more websites becoming completely AJAX-driven, the typical "do this every time a page loads" strategy isn't always enough. This is especially try if you're using Google Analytics or other analytics tracking software. Google is, of course, forward-thinking so they've provided a way to easily track page views of AJAX requests.
随着越来越多的网站完全由AJAX驱动,典型的“每次加载页面时执行此操作”策略并不总是足够的。 如果您使用的是Google Analytics(分析)或其他分析跟踪软件,则尤其可以尝试。 Google当然是有远见的,因此他们提供了一种轻松跟踪AJAX请求的页面浏览量的方法。
Google Analytics JavaScript (The Google Analytics JavaScript)
The current standard for loading Google Analytics is the asynchronous method:
加载Google Analytics(分析)的当前标准是异步方法:
var _gaq=[["_setAccount","UA-#######-#"],["_trackPageview"]];
(function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];g.async=1;
g.src=("https:"==location.protocol?"//ssl":"//www")+".google-analytics.com/ga.js";
s.parentNode.insertBefore(g,s)}(document,"script"));
That bit tracks the initial page load but not subsequent AJAX calls. Any time you want to track a page load, place add the following snippet:
该位跟踪初始页面加载,但不跟踪后续的AJAX调用。 每当您要跟踪页面加载时,请添加以下代码段:
// "_trackEvent" is the pageview event,
_gaq.push(['_trackPageview', '/some-page']);
_trackPageview
is used again but this time we provide the URL of the AJAX address that was loaded. Using the small JavaScript snippet above allows you to keep track of pageviews just as you would if the entire page reloaded. I'd recommend using this snippet with MooTools' History plugin.
再次使用_trackPageview
,但这次我们提供已加载的AJAX地址的URL。 使用上面的小JavaScript代码段,您可以跟踪页面浏览量,就像重新加载整个页面一样。 我建议将此代码段与MooTools的“历史记录”插件一起使用 。
旧方法 (The Old Method)
If you're still using the synchronous method of loading Google Analytics, the following snippet will accomplish the same task:
如果您仍在使用同步方法加载Google Analytics(分析),则以下代码段将完成相同的任务:
pageTracker._trackPageview('/some-page');
It's great to know that Google Analytics is as dynamic as your web applications. Allowing developers to track AJAX page clicks prevents the need to code apps just to keep track of statistics.
很高兴知道Google Analytics(分析)与您的Web应用程序一样动态。 允许开发人员跟踪AJAX页面点击次数,从而无需为了跟踪统计信息而对应用程序进行编码。