Skip to content

In my previous two blogs I explained how to identify spam traffic in Google Analytics data and then how to remove the false hits from historical data using a segment view.

In this final part I’m going to cover how I set up a filter to prevent it being recorded in the first place.

Effectively, I needed to take the two filters I had applied to the segment view and turn them into an actual filtered view.

Google Analytics Filters

Google Analytics filters ready for action!

Before starting I had two things to consider:

  1. It’s considered best practice to set up the filter as a second view on an Analytics property (leaving the default unfiltered ‘All website data’ to do it’s own thing).
  2. Ideally, the best time to do this is when first setting up Analytics on a new website so both views have been collecting data for the same amount of time.

This way it’s easy to compare the two views to check the filters are working correctly by comparing the collected data.

Creating a New Filtered View in Google Analytics

To begin, I went to the Admin tab and selected the account and property of the site I wanted to filter.

I then clicked on the View dropdown and selected Create New View.

Google Analytics Create New View

Creating a new view in Google Analytics

I named the new view ‘Remove Bot Spam’, set the timezone and then clicked Create View.

I then returned to the main Admin screen and clicked on the Filters option under the view I just created.

As I established when setting up the Segment view, I needed to apply two different filters to block out both the Ghost Referrals and Crawler Bot types of spam hits.

Check out Part 1 of my blog for more information on identifying types of spam on Google Analytics.

Creating a Valid Hostname Filter to Block Out Ghost Referral Hits

I’d previously figured out that Ghost Referral hits all have the common attribute that their Hostname attribute (ie. where the hit was tracked) does not match up to that of my website as the hit did not actually take place on my website (hence the Ghost name)! Presumably, someone had harvested my Analytics tracking code and stuck it somewhere in the deep corners of the internet.

Therefore, the most effective way of blocking these types of fake hits was to set up a new filter to only include traffic with a Hostname value of my website (in my case as this is the only place I want to measure traffic.

Google Analytics valid hostname filter

Setting up a Valid Hostname filter in Google Analytics

Note that if you are using your Analytics code on any other website (such as on Paypal if you have a checkout there) you’ll also need to include that in your filter pattern in regex format.

Creating a Filter to Block Bot Crawler Hits

The second filter I needed to create was to block out all the Crawler Bot traffic. As these bots actually did visit my website their Hostname attribute was set to, however, by looking at the Referrals data in the Acquisition tab of my original unfiltered data they were clearly identifiable as spam by where they have come from.

Google Analytics bot crawler referrals

I had already created a list of these spam referral sites and converted them into regex format when I first set up my segment view so now I needed to apply this to my new view.

Read my blog on setting up a Filtered Segment View on Google Analytics for more information on how I put the list of spam sites together.

My second filter was set to exclude any hits with a referral value of my regex list. Like this…

Google Analytics exclude referral spam

Setting up a filter in Google Analytics to block referral spam

Most likely this filter will need updating over time as new Bot Crawlers start hitting my site.


Google Analytics filter comparison

Google Analytics unfiltered versus filtered views

The image above was taken a week later, revealing that over half of my site traffic recorded by the unfiltered view as spam!! My filtered view therefore gives a far more accurate session duration and bounce rate (both of which where affected by the Bot Crawlers leaving the site immediately after arriving). This makes the data actually useful for making informed design decisions for future updates.

I’ll be monitoring my new filters over time and comparing them against the original unfiltered view to compare how they are performing. As I mentioned earlier, it’s likely that the second filter will need updating so I’ll be keeping a close eye on my referral traffic in particularly.

Thank you for reading and I hope you’ve found this information useful! As ever you can get in touch with me on Facebook and Twitter.

If you are interested in learning more about removing Google Analytics spam I highly recommend reading the Definitive Guide to Removing Referral Spam on Analytics Edge.

The PJWD Newsletter

Get helpful advice on web design, digital privacy and sustainability into your inbox once per month.


Helpful website advice in your inbox

The monthly PJWD newsletter is full of tips and advice on web design, digital privacy and sustainability (plus dog photos).

Get the PJWD Newsletter