User Timings

This guide describes how to measure periods of time using analytics.js.

Overview

Studies have shown that reducing page load time improves the overall user experience of a site. Google Analytics has a number of powerful reports that automatically measure and report on page load times. However, it is also possible to measure custom timing information to measure performance specific to your site.

User timings allow developers to measure periods of time using the analytics.js library. This is particularly useful for developers to measure the latency, or time spent, making AJAX requests and loading web resources.

Implementation

User timings hits can be sent using the send command and specifying a hitType of timing. The send command has the following signature for the timing hit type:

ga('send', 'timing', [timingCategory], [timingVar], [timingValue], [timingLabel], [fieldsObject]);

User timings fields

The following table summarizes the user timings fields:

Field Name Value Type Required Description
timingCategory text yes A string for categorizing all user timing variables into logical groups (e.g. 'JS Dependencies').
timingVar text yes A string to identify the variable being recorded (e.g. 'load').
timingValue integer yes The number of milliseconds in elapsed time to report to Google Analytics (e.g. 20).
timingLabel text no A string that can be used to add flexibility in visualizing user timings in the reports (e.g. 'Google CDN').

Examples:

The following command sends a user timing hit to Google Analytics indicating that it took 3549 milliseconds for the current web page to load all its external JavaScript dependencies:

ga('send', 'timing', 'JS Dependencies', 'load', 3549);

Note that as with all send commands, the fields passed in the convenience parameters may also be specified in the fieldsObject. The above command could be rewritten as:

ga('send', {
  hitType: 'timing',
  timingCategory: 'JS Dependencies',
  timingVar: 'load',
  timingValue: 3549
});

Measuring time

When sending user timing data, you specify the amount of milliseconds spent in the timingValue parameter. It’s up to you to write code to capture this period of time.

The easiest way to do this is to create a timestamp at the beginning of a period of time and create another timestamp at the end of the period. Then you can take the difference between both timestamps to get time spent.

Most modern browsers support the Navigation Timing API, which includes methods on the window.performance object for measuring the performance of web pages through high-resolution time data.

The following example uses the performance.now() method, which returns the amount of time that has elapsed since the page first started loading:

// Feature detects Navigation Timing API support.
if (window.performance) {
  // Gets the number of milliseconds since page load
  // (and rounds the result since the value must be an integer).
  var timeSincePageLoad = Math.round(performance.now());

  // Sends the timing hit to Google Analytics.
  ga('send', 'timing', 'JS Dependencies', 'load', timeSincePageLoad);
}

Sampling considerations

Google Analytics will sample timing hits during processing in order to ensure an equitable distribution of system resources for this feature.

The rate at which timing hits are sampled is determined by the total number of pageview hits received during the previous day for the property. The following table outlines how the timing sampling rate is determined:

Total pageview hit count (previous day) Maximum number of timing hits that will be processed
0 - 1,000 100
1,000 - 100,000 10% of total pageview hit count
100,000 - 1,000,000 10,000
1,000,000+ 1% of total pageview hit count

Limiting the number of hits sent

To avoid sending Google Analytics hits that will not be processed, analytics.js allows you to control the percentage of hits that are sent via the sampleRate and siteSpeedSampleRate configuration options. By default these fields are set to 100% and 1%, respectively. You can adjust these values to more closely approximate the number of timing hits Google Analytics will process based on your average daily pageview counts.