How to Keep Staging and Sandbox Traffic out of Google Analytics

We’ve all been there. You’re looking at your Google Analytics reporting and notice a heavy spike in traffic or sales revenue and get excited. Then upon investigating, you discover that this is actually traffic from a testing or staging environment. Recognizing this test traffic is helpful today, but in the future you may not remember this data exists and it could seriously impact your reporting in the future. Unfortunately, there are no means of removing this data retroactively, however, with a few preventative steps you can stop this from happening again.

Disable Analytics in Testing Environments

This is the simplest, and most obvious solution to this problem. If Google Analytics is not installed on a local or sandbox environment, it cannot send traffic data. Removing the tracking snippet, or disabling any plugins are the strongest line of defense, however, if this step is ever forgotten you’ll need to set some fallbacks.

IP Filtering

Creating a series of filters to prevent this traffic from being recorded can be very helpful in preventing unwanted traffic from being recorded. Traffic data caught in the filter is immediately deleted and will not be saved in your reporting.

The broadest net for catching unwanted traffic is an IP filter. This will block all traffic from your network from being recorded in GA. Not only does this have the effect of blocking traffic from testing sites, but it also removes your activity from the live domain.

In addition to blocking your network’s IP address, I recommend blocking 127.0.0.* to ensure GA hits created by the Measurement Protocol on local environments are not included.

Hostname Filtering

IP filtering does a great job at removing most testing traffic, however, not all hits to GA are blocked this way. If your website is using a plugin which uses Google’s Measurement Protocol, events and eCommerce transactions are generated on the server and would bypass the IP filter.

To prevent this, add an additional filter which only includes traffic that shares a hostname with your site’s domain. This will continue to allow server generated events on the live site to continue being tracked while suppressing events from local and staging domains.

An important note here is that this filter will only work if the hostname is set as part of the request. If hostname is not included, Google will ignore the filter and save the data to your account. Depending on your integration, you may need to review your code and force the hostname to be included on every request.

Segments

If all else fails, you can use segments to remove unwanted data when reporting. Segments do not delete traffic data from GA, but it does limit the type of traffic you want to see.

Any test data which slipped past your defenses can be removed from your reporting here. Because of this data, as well as the amount of spam traffic on the web, we strongly encourage all reporting to be conducted through the use of segments.