Maintaining the list of Referral Exclusions in Google Analytics admin is a pain. Especially if you have a webstore, the number of referral sources you need to exclude to avoid sessions being split can grow really fast. Also, it’s not like the list is has the most intuitive UI. Instead of a handy text area where you could just copy-paste stuff, you’re left with a horrible line-by-line list, and there’s no way of copying lists across properties or anything useful like that.

So, in this tip, I’ll show you how you can modify the data at its source (the website), so that it becomes easier to manage the list of referrals you want to exclude.

X

The Simmer Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava into your email inbox!

Tip 27: Exclude referrals with a Custom JavaScript Variable

First, let’s get one thing straight. The referral exclude list excludes referral traffic, it doesn’t block it. ‘Exclude’ here means that traffic that comes in from a referral you’ve excluded gets converted into the Direct / (none) source/medium bucket. If you know your Google Analytics, you know that GA attributes all hits to the last non-direct acquisition source. In plain English this means that all hits that have no referral information get attributed to whatever non-direct source you previously had active. If you don’t have a previous campaign, or if the Campaign Timeout setting has expired, then it gets attributed to Direct / (none).

That means also that Referral Exclusion Lists should not be used to fight referral spam. So many people have recommended this method, and all these people are thus guilty of giving horrible, horrible advice.

Now that we’ve got that cleared, let’s get on with the solution. For this to work, you need to create a new Custom JavaScript Variable. Give it a descriptive name such as {{JS - Exclude Referrals}}. Copy-paste the following code within:

function() {
  var referrals = [
    'referrer1.com',
    'referrer2.com',
    'referrer3.com',
    'referrer4.com'
  ];
  var hname = new RegExp('https?://([^/:]+)').exec({{Referrer}});
  if (hname) { 
    for (var i = referrals.length; i--;) {
      if (new RegExp(referrals[i] + '$').test(hname[1])) {
        return null;
      }
    }
  }
  return {{Referrer}};
}

The only thing you need to edit is the referrals Array. Each referral source you want to exclude is on its own row, enclosed in single quotes, and all lines end with a comma except the last one. That’s the syntax.

The list gets turned into a regular expression, against which the referrer of the page (i.e. the URL of the page that brought the visitor to the current page) is tested. The regular expression is open on the left, and closed on the right, meaning that an entry such as simoahava.co will match the following:

But not the following:

I hope you get the drift. If you’re handy with regular expressions, feel free to modify the strings in the referrals Array. For example, to only exclude simoahava.com but not, for example store.simoahava.com, you’d create the entry like:

var referrals = [
  '^simoahava.com'
];

The $ which closes the expression at the end is added automatically to each line later in the script.

So, the script tests each entry you’ve added into the referrals Array against the hostname of the current referrer, and if there is a match, null is returned. This means that any GA Tag that uses this Variable will not send the Document Referrer key with the payload, which is what GA uses to establish referral traffic.

To add this to your Google Analytics Tag, browse to More Settings -> Fields to Set, and add the following details:

Field Name: referrer
Value: {{JS - Exclude Referrals}}

Like so.

Note! Try this at your own risk. Remember to test the solution carefully before going ahead with a full-scale implementation. Referrer information is crucial in Google Analytics, as it can make or break your traffic attribution reports.