Selectively Disabling Analytics on Wordpress

So, you want your Google (or other) analytics to accurately represent the visitors to your WordPress site, rather than the 20 times you loaded it to test a new widget? Here’s how to turn off your analytics Javascript(s) for your own visits, thus preserving the sanctity of your reports:

  1. Find out what your Wordpress login cookie is called

    This is where the Web Developer plugin for Firefox comes in handy. Make sure you’re logged into your blog, and go to Web Developer -> Cookies -> View Cookie Information. Scroll down until your “wordpress_logged_in_[hashstring]” cookie appears (I am not entirely sure what the long hashstring in the cookie’s name represents, but it seems to stay the same for the given user). Copy the cookie’s name.

  1. Find the analytics code in your theme

    I keep my analytics code in footer.php, and I recommend that you do also.</li>

  2. Write a little PHP magic

    Before the analytics code begins, write this line:

    <?php if(!$_COOKIE['{cookiename}']): ?>

    and after it, write:

    <?php endif ?>

    This will prevent the analytics code from being loaded, thus hiding your visit from your reports. It might be desirable to always have the option of turning off analytics, though: perhaps you’re checking the blog from a friend’s computer and don’t want those hits to show up. Here’s how to do it:

  3. Decide on a suffix for your URL

    The suffix will be in the format “?{suffix}=true”. You can make this as easy or as difficult to remember as you want, but let’s presume you want to use “hideme”.

  4. PHP magic

    Change the above if statement to:

    if(!$_COOKIE['{cookiename}'] and !$_GET['hideme']):

  5. Browse, silently

    Go to http://blog.yoursite.com/?hideme=true - your analytics provider will never know.