How to remove localhost from my referrer list

If somebody on your team is running a development version of your application with the Google Analytics tracking code, then you'll get a few annoying side effects.

Not only will localhost show up in your Referrers, but your aggregate metrics like Bounce Rate, Time On Site, Conversion, and others will be incorrect because the unusual behavior of a developer's will be mixed in with that of normal users and skew our results. We don't want that.

This is easy to fix and you have a choice of three different methods, but it's important to note that all fixes will only apply to new visits. Existing tainted data will remain the way it is, unfortunately.

Add a Google Analytics exclusion filter

Adding a filter to Google Analytics might be the easiest method with the least intervention required from the development team.

  1. Open Google Analytics and choose your property view.
  2. Navigate to Admin.
  3. Click on Filters under the View column.
  4. Click on New Filter.
  5. Create a new "Predefined filter" which excludes traffic to the "localhost" hostname.

Your filter should look like this:

You may need to change localhost to whatever hostname your development team uses. Or better yet, if all development happens from the same network, you can exclude an entire IP range so that none of the company's behavior will affect your analytics. Just set the filter to exclude "traffic from ip address" and off you go.

Change the JavaScript tracking snippet to exclude a host

If you'd rather not mess with Google Analytics Filters and the application is fairly frontend-heavy, then making the fix in JavaScript is a reasonable choice.

In your Google Analytics tracking snippet, find the part which defines the property ID for which to record analytics. If you've upgraded to Universal Analytics, then it looks something like ga('create', 'UA-XXXXXX', '');.

Now we want to wrap the track with a condition that checks the visitor's hostname for this visit. Something like this:

    (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

    // Skip recording GA events to our account if in development.
    if (document.location.hostname != 'localhost') {
        ga('create', 'UA-XXXXXXX-XX', '');

    ga('send', 'pageview');

You'll need to make sure that you used your original UA-code and website, rather than If your team uses a development hostname other than localhost (such as, for example), then don't forget to adjust for that.

One variation of this method is to put the if-statement around the entire Google Analytics block, but the benefit of putting it just around the ga('create', ...) statement is that the rest of your Google Analytics related code will continue without actually recording the data to your account, rather than raise JavaScript errors.

Skip the JavaScript tracking snippet in the template

If you're using something like PHP (such as with Wordpress), you could do something like this in the PHP footer file which includes the Google Analytics snippet:

    <? if($_SERVER["REMOTE_ADDR"] != "") { ?>
        <script> Google Analytics snippet here ...</script>
    <? } ?>

Some serves are capable of setting up a global or environment variable which keeps track of whether the application is being run in development or production.

If you're using Django, then your template snippet might look something like this...

    {% if not DEBUG %}
        <script> Google Analytics snippet here ...</script>
    {% endif %}

Or with Ruby on Rails, it might look like this...

    <% unless RAILS_ENV == "development" %>
        <script> Google Analytics snippet here ...</script>
    <% end %>

This method probably requires some involvement with your development team but it also allows to have better control over when your Google Analytics snippet shows up or not. This is also a great opportunity to add more production-specific JavaScript services into a wired-off development flag.

Moving forward...

With your changes in place, you'll want to double-check your Referrers analytics a day or two later. Make sure to select a recent date range that does not include days up to and including when the changes were made.

For more discussion on this topic, check this StackOverflow thread:
Is there a way to stop Google Analytics counting development work as hits?