Say Goodbye to Google Analytics Referral Spam

Lately, referral spam has become a big issue, significantly skewing results in everyone’s analytics reports. The “exclusion filters” feature of Google Analytics has been a help, but it’s tedious to keep adding more filters whenever new spammers pop up, and a lot of the times the filters just don’t cut it.

Lucky for you, we’ve put together something that will make referral a spam a problem of the past.

Because referral spam has become such a problem, a small community of people have decided to put together a Referrer spam blacklist which is updated whenever someone finds a new spammer in their reports. The list is well-maintained and updated quite often. Now, what if there was a way for you to somehow integrate this list into your website, such that whenever a referral is detected from any domain in that blacklist, they automatically get exluded from your tracking? Well, we built exactly that, and it’ll only take a few minutes for you to set it up!

We’ve put together a special version of the google analytics tracking code which will only run if the referrer is not included in the blacklist. This way, you don’t have to set up any filters to get rid of spammers, and it automatically updates itself whenever new spammers are found. Here’s how it works:

  1. First, it requests a copy of the blacklist before allowing any tracking to happen. Because Github doesn’t allow you to include files directly from their website, we’ve hosted a copy of it on an Amazon S3 server and setup a task that updates the blacklist daily.
  2. Next, it checks the referrer against the blacklist, and tracks the visitor if and only if the referrer does not match any of the referrers in the blacklist.

Now let’s move on to getting this setup on your website.

Instructions

Setting this up is pretty easy. All you need to do is replace your current Google Analytics tracking script with this one:

Once you’ve done that, that’s it! No more referral spam.

Note that if you have a customized tracking script because you’re using enhanced link attribution or some other feature, you can freely replace the current script in the example. You can also add any other tracking scripts you have, as long as they are inserted below the Place any tracking scripts here comment and above the End tracking scripts comment.

I hope this is the last time you have to deal with referral spam. If you have any questions or need help setting this up, leave a comment below. Enjoy!


In case you’re curious about the exact source for this script, here’s the un-minified code below:

/*
 * Detect if the referrer is a referral spammer and prevent google analytics
 * loading if that is the case.
 *
 * @author Cooper Maruyama
 * @website convertify.io
 */

// Runs if referrer is valid.
window.trackVisitor = function() {
  // Your analytics script goes here.
}

/*======================================================
=    Don't edit anything below!
======================================================*/

(function(document, trackVisitor) {
  var BLACKLIST_URL="https://s3.amazonaws.com/s3.convertify.io/spammers.txt";

  // Fetch blacklist and perform validation.
  var request = new XMLHttpRequest();
  request.open('GET', BLACKLIST_URL, true);

  request.onreadystatechange = function() {
    if (this.readyState === 4) {
      if (this.status >= 200 && this.status < 400) {
        // Parse the list
        var data = this.responseText;
        validateReferrer(data, function(err, isValid) {
          if (isValid) {
            trackVisitor();
          } else {
            // Spam detected! bypass tracking.
          }
        });
      } else {
        // AJAX request failed. Track anyways.
        trackVisitor();
      }
    }
  };

  request.send();
  request = null;

  /**
   *  Validates referrer against a blacklist of known spammers.
   *
   *  @param   {String}   blacklist - Blacklist of known spammers, delimited by
   *                                  new line.
   *
   *  @param   {Function} callback  - Callback to call. Passes an error as the
   *                                  as the 1st argument or null if no error.
   *                                  Passes true as the 2nd argument if the
   *                                  visitor is valid, or false if the visitor
   *                                  is a spammer and should not be tracked.
   *
   *
   *  @return  {Null}
   */
  var validateReferrer = function (blacklist, callback) {
    var badReferrers = blacklist.split('\n');
    var isReferrerBad = false;
    var listLength = badReferrers.length - 1;

    for (i = 0; i < listLength; i++) {
      var re = new RegExp(badReferrers[i]);
      if ( re.test(document.referrer) ) {
        isReferrerBad = true;
        break;
      }
    }
    if (!isReferrerBad) {
      callback(null, true);
    } else {
      // Referral spam detected!
      callback(null, false);
    }
  };
})(document, window.trackVisitor);

Recommended For You


Why Your Winning A/B Tests Aren't Improving Your Bottom-line

Posted 03 March 2015

tips testing platforms

Goals Are Nothing But Outcomes We Want

Posted 07 June 2015

tips analytics


© Convertify 2012

Mobile Analytics