Google Analytics Filters to Clean up Your Stats

When you have been working with Google Analytics for a bit you will soon notice two annoying things. One, your data is being polluted with visits by yourself. Two, you will see Google Analytics Spam or referral spam. Luckily enough there are filters to combat both issues. Let’s discuss two Google Analytics Filters to clean up your stats.

First things first

First of all, do make a new view before adding filters. If you create the wrong filter you will lose essential data and no, that cannot be resuscitated somehow. So do create one in the admin under views. Give it a name that will make clear that this will be a filtered view.

Filter Creation

Google Analytics Filters can be found in your Google Analytics Dashboard under admin > filters.

google-analytics-admin-filters

Here you can click on add filter to add one and see existing ones:

Google Analytics Add Filter

In the example above you see the two filters I have added and will discuss in detail. You just click on the red button to add a new one. Then you will be asked to make a few choices. You will need to pick a predefined filter or choose a custom one and you will of course have to give the filter a name. Next, I will discuss how I created the two filters to block my own traffic and referrer spam.

Excluding Home IP Adress

Excluding your own visits can be done using the exclude option and adding the ip address range you would like to exclude. You can pick a predefined one as there is a ready made one you can use. It is called an Exclude filter. That is what you need. You will exclude traffic from your IP addresses range so the option “traffic from the IP addresses“. And you need to pick “that begin with” as we will be using a range. Here a screenshot of my existing filter:

Google Analytics Excluding IP Addresses

Most Bahraini ISPs do give you a dynamic ip address for example. So you will need to add a range. This is done easily as you can just add the first three blocks leaving out the final one. So something like: xx.xxx.xx That way Google Analytics will block a whole range. Do not make it too broad though otherwise you will exclude traffic by customers!

Exclude Dynamic IP Addresses

Another option to exclude multiple ip addresses, especially when you work with a dynamic ip address from your home office for example. This can be done using a custom filter like is used for the referral spam filter in the next section. You will have to use a regular expression to exclude multiple ip addresses / ip address ranges. Here and example of a regular expression that filters two ip ranges:

^192\.168\.1.*$|^192\.100\.0.*$

As you can see addresses start with a ^ and end with a $. Also notice the *? That is to allow all possible numbers for that set of numbers after the dot. Backslashes are needed to make sure the pattern picks up the “.” properly. As Google stated:

\. indicates that the adjacent dot should be interpreted as a period or decimal rather than as a wildcard.

And here the Google Analytics filter screenshot for clarity. It uses an ip address followed by an ip address range. That is why the first has 3 backslashes and the second only two:

ip ranges filter

Filtering out Referrer Spam

To deal with annoying referrer spam like buttons-for-website.com, sharebutton.to:

Google Analytics Referrer Spam

or really creative US politics spam in language agent listing:

Trump Referrer Spam in Google Analytics

you will need to create an include filter to only include valid hostnames related to your site / domains. So pick Include > hostname and then add your domains with some “\” to make sure GA understand the dot has to to stay:

valid hostnames filter

Not hard really, especially if you only run your domain and not a CDN. In my case it is:

imagewize\.com|cdn\.imagewize\.com

Summary

So there you go, two filters to deal with referrer spam as well as excluding your own visits from the Google Analytics stats. If there are other Google Analytics questions you have leave me a comment and I will get back to you on that as soon as I can.

Tagged in : Tagged in : , ,
Jasper Frumau

Jasper has been working with web frameworks and applications such as Laravel, Magento and his favorite CMS WordPress including Roots Trellis and Sage for more than a decade. He helps customers with web design and online marketing. Services provided are web design, ecommerce, SEO, content marketing. When Jasper is not coding, marketing a website, reading about the web or dreaming the internet of things he plays with his son, travels or run a few blocks.