Stop Google Analytics from counting Admin visits on WordPress

As a designer one of the most annoying things about developing and maintaining a WordPress theme is that Google Analytics counts every visit from your machine, even when your just working on an offline development sever as I do. This is especially crucial when a site is launching and all the visits from the local server can throw your stats way off, as seen in the image here.

There are a few existing ways to stop Analytics from counting your visits, one of the best is by using this method on HubPages that gives your browser a name and then creating an exclude filter on Google Analytics.

It’s a good method but it doesn’t work cross-browser and involves some tinkering on the Analytics backend. The method I use, uses PHP to check if the current user is an Admin and inserts a link to the WordPress admin panel. If the user isn’t an Admin it loads the Analytics code instead.

Don’t forget to change the UA-000000 to your own Analytics ID. You can change the first line to exclude Editors and Authors by changing the current_user_can function, have a look at the Capability vs. Role table in the WordPress Codex. You also can completely remove the second line if you don’t want or need a link to the Admin panel, but I found it a useful visual indicator that the code was working.

Hello World!

Welcome to my new quaint little blog. I’ll be sharing some tips, reviews and other assorted snippets. Come back soon Y’all!