Hide Your Visits from Google Analytics

When I visit my own websites, I don’t want Google Analytics to count those visits.  Google provides two ways to exclude your own visits, but these techniques don’t work in many cases.  In this article, I’ll provide a better way to do it using PHP, as well as a link on how to do it using Javascript (although I prefer the PHP method and I’ll explain why).

The Google Solutions

But first, let me quickly go over the Google-provided techniques described in this article:  How do I exclude my internal traffic from reports?

The first Google technique lets you specify certain IP addresses not to track.  If you have a static IP address that is great.  However, it doesn’t work for the vast majority of small-time users who have dynamic IP address.  If you don’t know whether you have dynamic or static IP, you probably have dynamic.  Static IP usually costs more and needs to be requested from your provider.  This solution just doesn’t work for me.

The next Google technique requires you to run a snippet of code to create a cookie to tell Google Analytics to ignore your visits.  This all fine and good until you clear your cookies, which is something I do ALL of the time as a web developer in order to test my sites.  If you do clear your cookies, you have to remember to re-run the little snippet of code.  That doesn’t work for me, not because it’s hard, but because I’ll forget to do it.  Plus, this method is unintuitive and inelegant.

The PHP Solution

Below is the solution using PHP that I like:

<?php
if (!isset($_GET['test']))
{
?>
<!--PUT GOOGLE TRACKING CODE HERE-->
<?php } ?>

Simple, right?  Move your Google Analytics tracking code where indicated and place this code just before the closing </body> tag of your page.  Note that in order to run PHP code, your file type has to be .php instead of .html.  (For example, you’ll need to change your landing page from “index.html” to “index.php”, and similarly for all of your other html files with the Google tracking code.)

To exclude your visit, simply visit your site with this query string added:

http://www.yoursite.com?test=1

Save this link to your browser favorites or bookmarks so you don’t have to remember it.  Whenever you visit your site using this link, Google Analytics won’t count the visit!  In fact, the code won’t even appear to the browser, which will speed things up over Google’s methods!!

Testing Your Code

Testing this solution by checking Google Analytics can be tricky because it takes a while for visits to show up there.  How can you be sure you gave the tracking code enough time to propagate?  Even worse, how can you be sure another visitor didn’t trigger the code and not your script?

The best way to check is to view your site’s source code in your browser.  If you run with the query string set to “1” (or any other nonzero value), the Google tracking code should not appear at all in your site’s source.  You should also check to make sure that the Google tracking code is there when do you don’t use the query string!


The Javascript Solutions

Let’s say that you don’t want to use PHP code for some reason.  There are lots of ways to read the query string using Javascript.  However, they are complicated and not as easy to understand as the PHP method.  You can find them here.

If you took a look, you’ll believe me when I say the solutions are not as easy to understand as the PHP solution.  But aside from that, there are more important disadvantages to the Javascript solutions.

Why I Prefer PHP

After working on a few mobile websites, I’ve learned that you really want to have your server do as much of the work as possible instead of the tiny processor on the client device (i.e., the user’s phone).  This can be done by moving as much code as possible from Javascript (which runs on the wimpy phone) to PHP (which runs on the beefy server).  I’ve found that running Javascript on mobile devices really slows down the perceived load time!

Of course, you really don’t care how long it takes this particular Javascript to run, because it’s just tracking code.  However, we are still tying up the client’s processor, which can slow down other stuff that you do care about, and which will affect the rendering speed of your site.

Another small, perhaps philosophical, advantage of the PHP method is that it completely prevents the Google tracking code to be downloaded by the browser.  As far as the browser is concerned, it never existed!  This frees up bandwidth and makes your page load a tiny bit faster.  Yes, the Google tracking is miniscule in the grand scheme of things and probably takes a fraction of a millisecond to download.  But, this is a general philosophy that you should strive for if you want your sites to be mobile-friendly.

Hope this helps, and let me know how it goes.

Brian

9 thoughts on “Hide Your Visits from Google Analytics

    1. Hi Bandita,

      Other than visiting a URL with query string as I describe above, I don’t have another way to exclude traffic from your mobile device.

      Thanks,
      Brian

  1. Hi – with the php solution, does it not fall down by the fact that I navigate round my own site, using the links that everyone else does, and therefore deeper clicks than the initial bookmark would not have the extended url? I’m hoping you’re going to tell me I’m wrong and tell me why! Thanks!

    1. Hi Rich, good point, you are totally correct! I suppose a more robust solution would be to set a cookie. This was just something quick and dirty. Let me know if you find a cookie solution, or I’ll post it if I ever get around to it. – Brian

Leave a Comment or Ask a Question