javascript - _gaq.push( ['_trackPageLoadTime' ]) 是如何工作的?

标签 javascript performance html google-analytics

Google Analytics 站点速度功能 _gaq.push(['_trackPageLoadTime']) 如何工作?是否有关于其工作原理的文档?

最佳答案

编辑:截至 2011 年 11 月 16 日,the _trackPageLoadTime function has been deprecated and its functionality has been set as a default setting . (从功能上讲,它已经从一个选择加入功能变成了一个选择退出功能。)

_setSiteSpeedSampleRate 是设置此功能采样率的新函数;它的默认值为 1(如 1%)。要选择不使用此站点速度功能,您必须将 0 传递给此函数:

_gaq.push(["_setSiteSpeedSampleRate", 0]);

来自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

因此,它没有像许多以前的 homeback 解决方案那样实现自己的计时器来计算加载页面需要多长时间。相反,它使用了一个新的 HTML5 功能,目前仅在上面列出的情况下受支持,称为 NavigationTiming。

编辑:Firefox 7 现在支持此功能

(重要的是要注意,它不会在每次加载时都运行;相反,它目前采样了大约 2% 的综合浏览量,尽管它被配置为尝试跟踪 10% 的所有页面加载访问;随着越来越多的浏览器支持 NavigationTiming API,您可以预期总采样百分比将开始接近 10%。)

此接口(interface)在 DOM 对象 window.performance 下访问(或者,在早期版本的 Chrome 中,window.webkitPerformance),使用 timing 属性(因此,window.performance.timing)。该对象存储所有关键页面加载事件时间的测量值,Google Analytics 减去 2 个更重要的外部值来判断页面加载速度。

对于没有缓存的 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
}

这些数字是纪元毫秒,即自 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);

对于上面的示例,这意味着它将在 _trackPageLoadTime 调用中记录 4.14 秒

来自 W3C 导航计时规范:

fetchStart attribute

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.

loadEventStart attribute

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.

对于好奇的各方,顺序似乎如下:

connectStart, connectEnd, domainLookupStart, domainLookupEnd, fetchStart, navigationStart, requestStart, responseStart, domLoading, responseEnd, domContentLoadedEventStart, domInteractive, domContentLoadedEventEnd, domComplete, loadEventStart, loadEventEnd

对于列出的 0 值:

unloadEventStartunloadEventStart 显示上一个页面加载卸载的时间(但前提是该页面与当前页面具有相同的来源。)

redirectEndredirectStart 测量页面加载链中存在 HTTP 重定向时增加的延迟。

secureConnectionStart 似乎是用于测量 SSL 连接时间的可选测量值。

关于javascript - _gaq.push( ['_trackPageLoadTime' ]) 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6166074/

相关文章:

javascript - jQuery 移动 : Pinch/Zoom/Scale Gesture - console. 日志 ('something' );

mysql - 在 Mysql 5.6 中加速具有 100.000.000 行的 innodb 表中查询的最佳方法

html - 谷歌浏览器中的文本自动刷新(渲染问题)

jquery - 谷歌地图嵌入与粘性导航栏重叠

javascript - 如果存在转译错误,ng build --watch 不会输出 dist 目录

javascript - Match a URL with placeholders 与 URL with filled placeholders

javascript - 将数组中的字符串拆分为更多数组

c++ - 了解基本代码分析

Android - 定期有效地安排任务?

javascript - 如何在没有提交按钮的情况下向 JavaScript 提交表单?