简体   繁体   English

使用GA测量单页面应用的页面加载时间

[英]Measuring Page Load Time of single-page app with GA

I'm using Google Analytics in an SPA. 我在SPA中使用Google Analytics。 For any virtual page redirects (like an AJAX call to refresh the body of the page), I'm getting a page load time of 0ms . 对于任何虚拟页面重定向(如刷新页面主体的AJAX调用),我的页面加载时间为0ms Is there a way to track how long that takes, just as if it was a full page refresh? 有没有办法跟踪需要多长时间,就像是整页刷新一样? I'm hoping to include how long it takes for the AJAX call and also the time to download and display images that are loaded as a result. 我希望包括AJAX调用所需的时间以及下载和显示因此而加载的图像的时间。

As you have found, Google Analytics will not provide page timings for SPA's. 如您所见,Google Analytics不会为SPA提供页面时间安排。 This includes if you increase the site speed sample rate to 100. This is because Google Analytics calculate the page timings using the Navigation Timing API. 这包括您将网站速度采样率提高到100.这是因为Google Analytics使用Navigation Timing API计算网页计时。

For example, DOM loaded would be: 例如,加载的DOM将是:

$(document).ready(console.log((Date.now() - 
    performance.timing.domComplete)/1000))

To over come this problem, you will need to use custom metrics. 要解决此问题,您需要使用自定义指标。 The solution has three steps. 该解决方案有三个步骤。

1) Set up a custom metric in GA. 1)在GA中设置自定义指标。

Go to Admin > Property > Custom Definitions > Custom Metric. 转至管理>属性>自定义定义>自定义度量标准。

Create a new Custom Metric, with the scope of Hit and the formatting type of time. 创建一个新的自定义度量标准,具有Hit的范围和格式化的时间类型。 Note: Specify time in seconds, but it appears as hh:mm:ss in your reports. 注意:以秒为单位指定时间,但在报告中显示为hh:mm:ss。

2) Set up a timer. 2)设置计时器。

You will need to capture the time when you want to start the measurement of page load time. 您需要捕获开始测量页面加载时间的时间。

An example solution to this might be by decorating all of your internal links, eg: 对此的示例解决方案可能是通过装饰所有内部链接,例如:

$('a[href*="stackoverflow"]').click(function(){
  time1 = Date.now()
})

3) Send the time eclipsed (in sec) to Google Analytics on the virtual pageview event. 3)在虚拟网页浏览事件中将时间黯然失色(以秒为单位)发送给Google Analytics。

When the virtual pageview event occurs (which triggers your virtual pageviews), retrieve the difference between the current time (Date.now()) and the time which the timer was started (time1). 当虚拟网页浏览事件发生时(触发虚拟网页浏览),检索当前时间(Date.now())与计时器启动时间(time1)之间的差异。

Using Google Tag Manager, a custom javascript variable can be created as below: 使用Google跟踪代码管理器,可以创建自定义javascript变量,如下所示:

function(){
  return (Date.now() - time1)/1000
}

This value then needs to be sent with the pageview, against the custom metric index set up in step1. 然后,此值需要与页面视图一起发送,与步骤1中设置的自定义指标索引相对应。

ga('send', 'pageview', {
  'metricX': pageLoadSpeed
});

Using the custom metric along with calculated metrics (eg {{virtualPageTimings}}/{{pageViews}}, you will be able to calculate your average virtual page timings. 使用自定义指标和计算的指标(例如{{virtualPageTimings}} / {{pageViews}},您将能够计算平均虚拟网页时间。

Bonus: 奖金:

To make the measurement more accurate, set up a secondary custom metric to count the number of virtual pageviews. 要使测量更准确,请设置辅助自定义指标以计算虚拟综合浏览量。 This will make sure that pageviews which users are directly navigating to are not taken into consideration. 这将确保不考虑用户直接导航到的网页浏览量。

To do this, create a custom metric with the scope hit and the formatting integer. 为此,请使用范围命中和格式化整数创建自定义指标。

Then with every virtual pageview, send the value 1 against the custom metric index. 然后,对于每个虚拟页面视图,将值1发送到自定义度量标准索引。 Eg: 例如:

ga('send', 'pageview', {
  'metricX': pageLoadSpeed,
  'metricX': 1
});

This allows for the calculated metric: 这允许计算的度量:

{{virtualPageTimings}}/{{virtualPageViews}}

If you checkout the Google Analytics docs, you can find out about the siteSpeedSampleRate option, which basically allows you to turn on your site tracking beacons for a percentage of your users. 如果您查看Google Analytics文档,则可以了解siteSpeedSampleRate选项,该选项基本上允许您为一部分用户启用网站跟踪信标。

By default this value is at 1 , but I'm assuming you might want to turn it to 100 . 默认情况下,此值为1 ,但我假设您可能希望将其设置为100 It might affect a bit in term of network usage since it will have to transfer more data to GA, so take that into account depending on your users and how they access your website (through mobile, bad coverage in some countries...). 它可能会在网络使用方面产生影响,因为它必须将更多数据传输到GA,因此请根据您的用户以及他们访问您网站的方式(通过移动设备,某些国家/地区的糟糕报道......)将其考虑在内。

You'll have to modify your tracking code to integrate something like the following: 您必须修改跟踪代码以集成以下内容:

ga('create', 'UA-XXXX-Y', { siteSpeedSampleRate: 10 })

You can send the timing manually, As everything on Google Analytics. 您可以手动发送时间,作为Google Analytics上的所有内容。 But it's a little bit tricky to do,if I'm honest I would no do it unleast it's necessary. 但是这样做有点棘手,如果我说实话我就不会这么做,这是必要的。 All the data on the time report are based on a hit called timing, this hit is send after the pageView and contains the following information. 时间报告中的所有数据都基于称为计时的命中,此命中在pageView之后发送并包含以下信息。 在此输入图像描述 If you can see my example, I forced the tool to send the hit, just after the pageview goes another hit with a special list of parameters. 如果你可以看到我的例子,我强迫该工具发送命中,就在页面视图进入另一个具有特殊参数列表的命中之后。

plt : Specifies the time it took for a page to load. The value is in milliseconds.
pdt : Specifies the time it took for the page to be downloaded. The value is in milliseconds.
dns : Specifies the time it took to do a DNS lookup.The value is in milliseconds.
rrt : Specifies the time it took for any redirects to happen. The value is in milliseconds.
srt : Specifies the time it took for the server to respond after the connect time. The value is in milliseconds.
tcp : Specifies the time it took for a TCP connection to be made. The value is in milliseconds.
dit : Used to send a random number in GET requests to ensure browsers and proxies don't cache hits. It should be sent as the final parameter of the request since we've seen some 3rd party internet filtering software add additional parameters to HTTP requests incorrectly. This value is not used in reporting.
clt : pecifies the time it took for the DOMContentLoaded Event to fire. The value is in milliseconds.

More info of this parameters on : https://developers.google.com/analytics/devguides/collection/protocol/v1/parameters and You can see more info of this hit on https://developers.google.com/analytics/devguides/collection/analyticsjs/user-timings 有关此参数的详细信息,请访问: https//developers.google.com/analytics/devguides/collection/protocol/v1/parameters ,您可以在https://developers.google.com/analytics/上查看此详细信息。 devguides /收集/ analyticsjs /用户定时

So what happens now?, if I launch another pageview in this SPA, the second pageview on the same page will not carry this hit and you will ever get 0 of loadtime. 那么现在发生了什么?,如果我在此SPA中启动另一个综合浏览量,同一页面上的第二个综合浏览量将不会带来此命中,您将获得0加载时间。 You can use the command as the official documentation but if you use it you will notice that is not the same hit (i have to double check that). 您可以使用该命令作为官方文档,但如果您使用它,您会注意到不同的命中(我必须仔细检查)。 Other option is send it manually using the command 'send' and attaching the desire information. 其他选项是使用命令'send'手动发送并附加所需信息。 Check your pageview hit structure to be sure that your timming is actually attaching to your previous hit. 检查您的综合浏览量命中结构,以确保您的调整实际上附加到您之前的点击。

The comand to send the timming after the pageview is send will be something like that, use the &dl parameter or 'dp' parameter to attach the timming to the ajax page. 发送页面视图后发送调整的命令将是这样的,使用&dl参数或'dp'参数将调整附加到ajax页面。

ga('send', {
  hitType: 'timing',
  '&plt': 1,
  '&pdt': 1,
  '&dns': 1,
  '&rrt': 1,
  '&srt': 1,
  '&dit': 1,
  '&clt': 1,
  '&dl': 'http://cl.edreams.com/',
});

Now all the values '1' needs to be updated for the correct one, now how to determine the time of each parameter not sure at all. 现在所有值'1'都需要更新为正确值,现在如何确定每个参数的时间根本不确定。 Also remember that the sampling by default is only for the 1% of the sessions, send this hit only in a few cases. 还要记住,默认情况下,采样仅适用于1%的会话,仅在少数情况下发送此命中。

Disclaimer : This is a very experimental implementation, we are forcing the Js to send unexpected information. 免责声明:这是一个非常实验性的实现,我们正在迫使Js发送意外信息。 Test it well before to pass it to a final project 在将其传递给最终项目之前进行测试

Greetings 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM