Removing Google Analytics referrer spam

Referrer spam is one of the more frustrating developments on the web in recent years. The premise is simple enough, a site owner sees a new referrer in their analytics and visits the website in question to investigate the source of new traffic.

The page in question does not contain any such links and instead advertises anatomical enhancements or attempts to install malware on the visitor’s computer.

Of the top ten referrers to my site in Google Analytics, seven are spammers. It’s an annoyance more than anything, all the same I’m trying a technique to kill it dead once and for all.

For the most part, referrer spammers do not hit your site. They don’t need to, they just run the Google Analytics code using incrementing tracking ids: UA-10000001-1, UA-10000002-1, UA-10000003-1, and so on and so on.

This gives site owners an advantage, the referrer spammers know nothing of custom dimensions the website owner has set. Google Analytics allows owners to include only traffic which includes a particular custom dimension.

I use a few custom dimensions, but not the allowed twenty. I decided to set up a dimension to appear on all page loads on my site. I wanted to use dimension20, so I filled the remain spots through to 19 with unused and inactive custom dimensions. In position 20, I added the dimension h8referrerspam.

Google Analytics custom dimensions

To my Google Analtyics, code I added the new custom dimension using the same value on all pages:

ga('create','UA-NNNNNNNN-1','auto',{'allowAnchor':true});
ga('set', 'dimension20', 'idontwanttobeacouch');
ga('send','pageview');

I went with a silly in-joke between my partner and I because it had to be something.

In Google Analytics, I then set up a filter:

  • Name: h8referrerspam
  • Filter type: Custom, include
  • Filter field: Custom dimensions > h8referrerspam
  • Filter pattern: ^idontwanttobeacouch$
  • Case sensitive: checked

I don’t think it will remove all the referrer spam, some do hit the site and run the JavaScript but I’ll be interested to see how much it helps over the next few months. Wish me luck.

By Peter Wilson

Peter has worked on the web for twenty years on everything from table based layouts in the 90s to enterprise grade CMS development. Peter’s a big fan of musical theatre and often encourages his industry colleagues to join him for a show or two in New York or in the West End.

7 comments

    1. It’s been going well, the referrals page remains mostly real sites.

      There is still too much spam, for which Google should be condemned. Gmail shows they know how to deal with spam; they are choosing not to for GA.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.