您的位置:首页 > 其它

使用谷歌统计来跟踪网页加载时间

2011-04-27 13:23 525 查看
之前看过百姓网的分享PPT,里面提到用谷歌统计来做页面加载时间的调查,这几天就看到一篇类似的文章。

http://www.biaodianfu.com/google-analytics-page-loading-time.html

转载如下

Google Analytics可以用来记录网站的加载时间或网页内各个模块的加载时间,其实整个原理非常的简单,只是记录页面呢不同位置javascript的执行时间,两者相减即加载时间。比如我想知道用户加载页面中head部分JS和CSS的时间和加载页面中主体内容body的时间。具体实现方式如下:

1、在页面head中CSS和JS文件加载前添加如下代码:

<script type="text/javascript">var _head_start = new Date();</script>

2、在页面<body>位置后面添加下面的代码:

<script type="text/javascript">var  _body_start = new Date();</script>

3、在页面页脚中,即</body>前添加下面的GA代码:

<script type="text/javascript">var _now= new Date();</script>

4、通过Google Analytics的事件跟踪将加载时间记录下来:

<script type="text/javascript";>

if (typeof(_head_start)==typeof(_now)) {

if (_now-_head_start<1000*10) {

_gaq.push(['_trackEvent', 'Performance', 'head', '/pagepath/', _now-_head_start]);

_gaq.push(['_trackEvent', 'Performance', 'body', '/pagepath/', _now-_body_start]);

}else{

_gaq.push(['_trackEvent', 'Performance', 'slow head', '/pagepath/', _now-_head_start]);

_gaq.push(['_trackEvent', 'Performance', 'slow body', '/pagepath/', _now-_body_start]);

}

}

</script>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: