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:
<!--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:
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!
Why I Prefer PHP
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.