When you spend enough time in your analytics, certain referrers of traffic to your site will definitely stand out if they keep appearing. Whilst referrers are typically a good thing as it means somebody is sending traffic to your site, this isn't always the case. In this post we will cover how we blocked a source of referrer spam from this site, however this same method can be used for blocking any unwanted referrers.
How to Block SPAM Domains From Accessing Your Site
The first step I undertook was to block the domains via my .htaccess file. You should find that all WordPress sites have one of these in your websites root directory. The code that I used is pasted below and was inspired by Paul Thompson from Buzzwords to Business. You can add the code to the bottom of your existing .htaccess file making sure to include any domains causing referrer spam. This should prevent further abuse from the problematic domains but unfortunately it is an uphill struggle and new domains are popping up all the time, so you may need to update this file over time.
The second step I took was to filter the referral domains from Google Analytics so that the spam attacks did not affect the accuracy of my analytics. The above methods should work for any unwanted referrers that you need to be blocked from accessing your site and skewing your statistics in the process.
“Data that is loved tends to survive.” — Kurt Bollacker, computer scientist, open-source advocate
2019 Update: Block Even More Referral SPAM
A large percentage of referral spam bots will never even visit your website. This allows you to delete them by filtering out sessions that don't come from your domain name. Here is how…
Total Time: 7 minutes
Log in to your Google Analytics dashboard
Go to Admin > View > Filters
Click the + ADD FILTER Button
Create a filter to only include your hostname
Select a Custom Filter Type to Include traffic with Filter Field set to Hostname and the following Filter Pattern replaced with your own domain name and extension: ^yourdomain\.com$
Click to Save to create the filter
WARNING: The method described above will filter the raw traffic data being saved to your Google Analytics account so you may want to add the filter in a new view.
It is also important to note that it will not impact any existing referral SPAM shown in your analytics already.