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.

I was going to make exactly this, but I told myself, let’s look for it before making it custom and here it is!
Thanks a lot, and the ‘admin’ link is a good idea :)

I’m one of a few admins on a site… and we each have multiple computers, locations, and browsers we use.

From what I can tell, your solution seems like it’ll take care of all of that by just inserting your code… problem is, where how/ does one use it?

Obviously, I’m completely clueless on coding, I’m just a blogger who is tired of seeing 2/3 of our hits come from the admins (not to mention how much that’s skewing the other results- notably time on site and bounce rate).

Thank you for your help.

Hi Sean, you can just insert it wherever your Analytics code is at the moment, it’s usually in the header.php or footer.php theme files.

If that’s still too technical there’s various plugins that offer the same functionality without having to tinker with your theme files.

Hi Johnathan.I’m sorry, but I don t think your solution sticks?Why?What happens if I’m not logged in the first time when I access the website or I delete my cookies often?The very first visit before i log in will be counted so i will have at least 30 visits a month only from me.What do you think?Thank you for the article anyway and looking forward your answer!

Adrian, you are correct that this relies on your login cookie which by default expires after two weeks. You could set another cookie with a longer expiry and modify the code to check for that cookie also. Alternatively you could add a rule to your Google Analytics account to exclude your IP from reports.

That’s nice and I would have done it like this but what about it when you use a cache plugin like WP Super Cache? If you’re the first to go on a page while logged in as the admin, you will generate a cached version without Analytics’ code, right? So you would miss logging the audience in Analytics.


Karl, I’ve not used WP Super Cache, but it shouldn’t be caching requests for admin users as that would also include things like the admin bar and edit post links (if the theme includes any).