How to Control Sample Rate and Page Views Using GTM

When they work correctly, site speed reports in Google Analytics provide you with priceless information. You receive automatic report milestones as the browser renders content. Usually, this data is collected on the first page view hit of a page, utilizing the Navigation Timing API. This should allow you to notice any issues in the client–server negotiation.how-to-control-sample-rate-1

Why Site Speed Reports Fail

Problems can occur when you have multiple trackers on a page to collect site speed samples. As automatic page timings are collected once per tracker instance per page, the same page speed data may end up being sent more than once. This means numbers in the data are higher than they should be and, unless you attempt de-duplication (which is an extremely difficult process), the data are useless.

Are You Impacted?

There are four situations where site speed reports can fail. The first is when a site consists of a single page and sends virtual page views to GA. The second is if you have multiple trackers on every page collecting the same Universal Analytics Property (UA-XXXXX-Y). The third is if you have manually chosen your site speed rate. The fourth is if your page view tag in GTM has multiple triggers attached.

However, not every site in these situations is affected. To know if your data could be useless, you also need to look at the code on your site. This code is fine:

ga('send', ‘pageview');

As is this:

ga('send', ‘pageview’);
ga('send', 'pageview', {page: ‘/some-custom-page/‘});

Bear in mind that, by default, 1 percent of page view hits are sampled for page timings. Therefore, in the first example, there is a one in 100 chance that the first time the command is executed the request will send navigation timing data to GA. In the second, although there are two separate page view hits on the same page, they use the same tracker. This means that, even if both are hit, the page timing data will only be sent once.

However, you do have a problem if you have the code:

ga('send', ‘pageview’);
ga(‘secondTracker.send', 'pageview', {page: ‘/some-custom-page/‘});

This means that both trackers are collecting the same Universal Analytics property (UA-XXXXX-Y). Both have a 1 in 100 chance of sending data, and if both do, the same data will reach GA twice.

To make matters worse, GTM assigns a unique tracker object for every tag fired, no matter whether the same tag is firing multiple times. This is a problem for virtual page views.

For instance, with a typical virtual page view, you will have a page view tag with siteSpeedSampleRate set to 100 along with a trigger that fires when a custom event is pushed into the dataLayer. This means that the trigger will fire multiple times on the page, each time with a different tracker name and each time sending the same page timing data.

How to Resolve the Problem

Sample a Single Tracker

One option is to sample just a single tracker. This is one of the easiest methods. Simply set the siteSpeedSampleRate to a proper value in just one tracker and to 0 in all others. This means a single set of page timings will be sent from each page.

This is possible if you are using a Universal Analytics snippet. However, by default it is impossible in GTM, as each tag has its own unique tracker name.

Sample the First Page View

Another possibility is to sample just the first page view. In contrast with the above, this is unnecessary in Universal Analytics, as UA only samples the first page view anyway. However, in GTM, this could fix your problem.

The best option is to have the all pages trigger on the page view tag followed by another trigger for the custom page view. This can still cause the page timing sample to be sent each time the tag fires, but you can fix the issue by using the following custom JavaScript variable for the value of the siteSpeedSample rate:

function() {
return {{Event}} === 'gtm.js' ? 100 : 0;
}

This will mean all pages triggered will only send the site speed sample, sending data just once per page.

If you are unable to distinguish the first page view from the ensuing triggers, combine this tactic with hitCallback on the page view tag to set a global flag or dataLayer variable to true. You will then need to use this flag as a condition for the siteSpeedSampleRate field to send the 100 value just when the flag is false.

Add a Tracker Name

By inputting a value in the tracker name field, whenever the tag fires it will use the same tracker object. This means the timing sample will only be sent once. You can configure the tracker name in the page view tag settings.how-to-control-sample-rate-2These are just a few ways to resolve the problem of sending page speed data more than once. You may even be able to come up with your own solutions to better meet the needs of your unique situation.

 

If you like my articles,