Prevent Google Analytics From Tracking Logged-in Users

In a previous post, I discussed how it’s useful to have both WordPress Stats and Google Analytics installed on a WordPress blog; the former can be used as a quick dashboard to check in on the day’s traffic, and the latter can be used for more advanced analysis.  But in order to keep our datasets comparable, we need to track users in a similar manner.

How do WordPress Stats and Google Analytics differ?

WordPress Stats doesn’t track visits from logged in users, but Google Analytics doesn’t provide this functionality out-of-the-box.  It is possible to filter out traffic by IP address in Google Analytics using a Custom Filter, but this only works if you have a static IP address (many/most home users don’t).


However, with some simple PHP code using the WordPress conditional tag is_user_logged_in() we can mimic this behavior with Google Analytics, and no matter what browser you choose or what location you work from, as long as you are logged in to WordPress, Google Analytics won’t track your visits.

Modifying the header.php file

In order to add the conditional tag to our Google Analytics code, we need to modify the header.php file where we originally installed the Google tracking code.  What we’re going to do is add the following code around our Google Analytics code, and WordPress will handle the “logged in logic” for us:

The first clause of this code (the “IF” line) checks WordPress to see if you are logged in; if so, the code that is between the {} brackets will execute.  The second clause (the “ELSE” statement) is what happens if the user is NOT logged in.  Since we only want Google Analytics to track our visits when we’re not logged in, we’ll place our tracking code within the “ELSE” clause. When you have done this correctly, the code in your header.php file should look similar to the following:
If you have an external JavaScript file holding your tracking code, the code would look like the following:

Are there any downsides to this method of implementing Google Analytics this way?

By implementing Google Analytics in this manner, you improve your data consistency between WordPress Stats and Google Analytics.  You also avoid inflating your page views and visits, especially when doing blog development and re-designs or while previewing drafts of blog posts.

The one downside I’ve encountered so far is that if you are testing any tracking code changes themselves (such as adding onclick events to links/buttons to track additional data), you have to log in and log out every time you want to test whether your JavaScript code is firing your Google Analytics code properly.

Yes, that’s a bit tedious, but I think it’s small price to pay for better data consistency.

 

Note:  If you are running a multi-user blog, you might consider using the is_admin() conditional tag instead of is_logged_in(), especially if you want to understand how the non-Admin authors are interacting with your blog.  The possibilities are endless!

Comments

  1. I’m not as technical when it comes to writing custom solutions, but the WordPress plug-in I use for Google Analytics deployment — Google Analyticator — allows me to specify whether I want logged-in admin users to be tracked or not, so I can set that in the interface.

    I set it to “don’t track,” and, then, if I want to do troubleshooting by sniffing the requests with Charles Proxy, I simply use — in Google Chrome to open an “Incognito” window and browse the site that way.

    I think that approach gets at a similar end result from what you’ve come up with. I don’t know that one way or the other is “better” — just different ways to skin the same cat.

    • Randy Zwitch says:

      Thanks for stopping by Tim! I use the Yoast Google Analytics plugin on my other blog, and you’re right, they are doing something similar to the code I wrote here. Maybe I just need to break open their plugins and see what they are doing :)

      It’s funny…maybe people don’t care, or the people that know the answer to this problem don’t blog, but I haven’t found that much information about how to do this properly. I’ve got to think that most people using GA don’t have full test environments (i.e. the home user). Of course, maybe the home user isn’t doing a ton of development!

Leave a Reply