Javascript _gaq.push(['_trackPageLoadTime']) 如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6166074/
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
How does _gaq.push(['_trackPageLoadTime']) work?
提问by ilhan
How does the Google Analytics Site Speed feature, _gaq.push(['_trackPageLoadTime'])
, work? Is there any documentation about how it works?
Google Analytics 站点速度功能如何_gaq.push(['_trackPageLoadTime'])
工作?有没有关于它如何工作的文档?
回答by Yahel
Edit: As of November 16th 2011, the _trackPageLoadTime
function has been deprecated and its functionality has been set as a default setting. (Functionally speaking, it has gone from being an opt-in feature to being an opt-out feature.)
编辑:截至 2011 年 11 月 16 日,该_trackPageLoadTime
功能已被弃用,其功能已设置为默认设置。(从功能上讲,它已经从选择加入功能变成了选择退出功能。)
_setSiteSpeedSampleRate
is the new function for setting the sample rate on this feature; its default value is 1
(as in 1%). To opt out of using this the Site Speed feature, you have to pass a 0
to this function:
_setSiteSpeedSampleRate
是在此功能上设置采样率的新功能;它的默认值是1
(如 1%)。要选择不使用此站点速度功能,您必须将 a 传递0
给此功能:
_gaq.push(["_setSiteSpeedSampleRate", 0]);
From the Google Analytics Help Center:
This report currently supports the following browsers: Chrome, Internet Explorer 9 and previous versions of Internet Explorer with the Google Toolbar installed. More specifically, the Site Speed reports require browsers that support the HTML5 NavigationTiming interface or have the Google Internet Explorer toolbar installed
此报告目前支持以下浏览器:Chrome、Internet Explorer 9 和安装了 Google 工具栏的 Internet Explorer 以前的版本。更具体地说,站点速度报告要求浏览器支持 HTML5 NavigationTiming 界面或安装了 Google Internet Explorer 工具栏
So, it doesn't implement its own timer, like many prior homeback solutions had, to figure out how long it takes a page to load. Instead, it uses a new HTML5 feature, currently only supported in the above listed cases, called NavigationTiming.
因此,它并没有像许多以前的 Homeback 解决方案那样实现自己的计时器来计算加载页面需要多长时间。相反,它使用了一个新的 HTML5 功能,目前仅在上面列出的情况下支持,称为 NavigationTiming。
EDIT: This is now supported in Firefox 7
编辑:Firefox 7现在支持
(Important to note that it doesn't run on every load; instead, it currently samples around 2% of pageviews, though it is configured to tryto track all page loads on 10% of visits; as more browsers support the NavigationTiming API, you can expect the total sampled percentage to begin to get closer to 10%.)
(重要的是要注意它不会在每次加载时运行;相反,它目前对大约 2% 的浏览量进行采样,尽管它被配置为尝试在 10% 的访问时跟踪所有页面加载;随着越来越多的浏览器支持 NavigationTiming API,您可以预期总采样百分比开始接近 10%。)
This interface is accessed under the DOM object window.performance
(or, in earlier versions of Chrome, window.webkitPerformance
), using the timing
attribute (so, window.performance.timing
). The object stores measured values of all of the key page load event times, and Google Analytics subtracts 2 of the more important outer values to judge page load speed.
这个接口是在 DOM 对象下访问的window.performance
(或者,在早期版本的 Chrome 中,window.webkitPerformance
),使用timing
属性 (so, window.performance.timing
)。该对象存储所有关键页面加载事件时间的测量值,Google Analytics 减去 2 个更重要的外部值来判断页面加载速度。
For a load of Mashable.com without cache, here's an example of what it measures (in Chrome 11):
对于没有缓存的 Mashable.com 负载,以下是它测量的示例(在 Chrome 11 中):
timing = {
connectEnd: 1306677079337,
connectStart: 1306677079337,
domComplete: 1306677083482,
domContentLoadedEventEnd: 1306677081765,
domContentLoadedEventStart: 1306677081576,
domInteractive: 1306677081576,
domLoading: 1306677079478,
domainLookupEnd: 1306677079337,
domainLookupStart: 1306677079337,
fetchStart: 1306677079337,
loadEventEnd: 1306677083483,
loadEventStart: 1306677083482,
navigationStart: 1306677079337,
redirectEnd: 0,
redirectStart: 0,
requestStart: 1306677079394,
responseEnd: 1306677079669,
responseStart: 1306677079476,
secureConnectionStart: 0,
unloadEventEnd: 0,
unloadEventStart: 0
}
Those numbers are epoch milliseconds, or milliseconds since January 1, 1970. I have not seen any documentation as to which values they subtract to generate their values, but from a cursory inspection of the ga.js, it looks like it is loadEventStart-fetchStart
:
这些数字是纪元毫秒,或自 1970 年 1 月 1 日以来的毫秒数。我没有看到任何关于它们减去哪些值来生成它们的值的文档,但从ga.js的粗略检查来看,它看起来像loadEventStart-fetchStart
:
h&&h[c]!=k&&h.isValidLoadTime?b=h[c]:e&&e[a]&&(b=e[a].loadEventStart-e[a].fetchStart);
For the above sample, that means it would record 4.14 secondsin the _trackPageLoadTime
call.
对于上面的示例,这意味着它将在通话中记录4.14 秒_trackPageLoadTime
。
From the W3C Navigation Timing spec:
来自 W3C 导航计时规范:
If the new resource is to be fetched using HTTP GET or equivalent, fetchStart must return the time immediately before the user agent starts checking any relevant application caches. Otherwise, it must return the time when the user agent starts fetching the resource.
This attribute must return the time immediately before the load event of the the current document is fired. It must return zero when the load event is not fired yet.
如果要使用 HTTP GET 或等效方法获取新资源,则 fetchStart 必须立即返回用户代理开始检查任何相关应用程序缓存之前的时间。否则,它必须返回用户代理开始获取资源的时间。
此属性必须返回触发当前文档的加载事件之前的时间。当加载事件尚未触发时,它必须返回零。
For curious parties, the ordering appears to be as follows:
对于好奇的派对,顺序似乎如下:
connectStart, connectEnd, domainLookupStart, domainLookupEnd, fetchStart, navigationStart, requestStart, responseStart, domLoading, responseEnd, domContentLoadedEventStart, domInteractive, domContentLoadedEventEnd, domComplete, loadEventStart, loadEventEnd
connectStart、connectEnd、domainLookupStart、domainLookupEnd、fetchStart、navigationStart、requestStart、responseStart、domLoading、responseEnd、domContentLoadedEventStart、domInteractive、domContentLoadedEventEnd、domComplete、loadEventStart、loadEventEnd
For the 0 values listed:
对于列出的 0 值:
unloadEventStart
and unloadEventStart
show the times for the previous page load's unloading (but only if that page has the same origin as the current one.)
unloadEventStart
并unloadEventStart
显示上一页加载卸载的时间(但前提是该页面与当前页面具有相同的来源。)
redirectEnd
and redirectStart
measure the latency added if there was an HTTP redirect in the page load chain.
redirectEnd
和redirectStart
测量添加的等待时间,如果在页面承载链有一个HTTP重定向。
secureConnectionStart
appears to be an optional measurement for measuring the SSL connection time.
secureConnectionStart
似乎是用于测量 SSL 连接时间的可选测量。