google-analyticsuniversal-analyticsgtag.js

Setting setSiteSpeedSampleRate in gtag.js


I want to increase the Sample Rate for Page Timings from 1% to 10% in google analytics.

We are using Global site tag (gtag.js) for adding GA to our site.

Following is current config:

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-123456-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-123456-1');
</script>

As per the doc, the following will capture the user load time, but it won't increase the sampling rate.

// 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 event to Google Analytics.
  gtag('event', 'timing_complete', {
    'name': 'load',
    'value': timeSincePageLoad,
    'event_category': 'JS Dependencies'
  });
}

Solution

  • This was solved by setting, gtag('config', 'UA-XXXX-X',{'site_speed_sample_rate': 100})