How to Exclude WordPress Logged in User Page Views from Google Analytics?

Active bloggers keep publishing several blog posts a month. During the process of drafting, editing, publishing, previewing and even modifying it later, a number of page views are recorded by Google Analytics as if an actual blog visitor is browsing your site. For a small time blog with only a few hundred visits per month, this would mean that what you perceived as real blog visits are not exactly so.

Excluding the logged in user or admin page views from your Google Analytics (or other tracking code) would not only project more accurate blog visitor analytics but also help hiding the admin location from Google. For smaller niche blogs that target a particular geographic location, this may be an important aspect. In this short post, let me explain how to exclude your Google analytics tracking code for logged in WordPress users.

Code to exclude Google Analytics for admin

It’s actually pretty simple two lines of PHP code. And all that you have to do is to put your Google Analytics code within these two lines:

<?php if (!is_user_logged_in()) { ?>

Insert your Google Analytics code here

<?php } ?>

For example, it would look something like this after including the actual Google analytics code (Remember to replace the part in RED with your actual analytics account code)

<?php if (!is_user_logged_in()) { ?>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "' type='text/javascript'%3E%3C/script%3E"));
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3943297-1");

<?php } ?>

Once you have the above code in place, all that activities you do as a WordPress admin or editor will not be tracked and accounted by Google Analytics.

Google Analytics in Theme Options

Most WordPress themes have the Google Analytics tracking code pasted as one of the theme options. In such cases, you have to actually dig into the theme code to check where exactly it is including the Google Analytics theme option. This is usually in footer.php or one of the include files. Once it is located you can put the above two lines of PHP code around the relevant rendering part. This part can be tricky unless you know a bit of programming.

Alternate code

Instead of the is_user_logged_in() function, you could also use the following code to detect if any WordPress user is logged in:

<?php if (!$user_ID) { ?>

Insert your Google Analytics code here

<?php } ?>

As I always say, keep a backup of your theme files before attempting any modification on the same.

Next let’s see how can you filter out your own page views regardless of whether you have logged into WordPress or not.

Exclude Google Analytics by IP, IP range or cookie

This involves entering analytics filters in your Google Analytics account. It can be a little bit advanced topic as you have to use regular expressions for your IP or IP address to be filtered out and even creating a custom page if you want to filter out all your IP addresses.

Filter by IP

In order to filter out page views caused by your own browsing (i.e your IP address) you can do the following:

1. Log into your Google Analytics accounts, choose ‘Edit’ and scroll down to click ‘Add Filter‘.

2. Enter a filter name and choose ‘Exclude’ -> ‘Traffic from the IP addresses’ -> ‘that are equal to’ from the three drop downs and enter your IP address there.

3. You are done with your ‘One IP address’ or predictable range of IPs.

To learn more about this approach and how to use filter analytics based on a custom page and cookies, please visit the exclude Google Analytics dynamic IP address topic.

Happy Blogging!


  1. Praveen Rajarao :

    Cool!!! I will surely need to incude this piece of code. My analytics will be all wrong if I dont i realize now. Thanks for sharing Ajith.

  2. Great tip Man!!!

    Thanks will add this right away.

  3. hi Ajith,

    Was looking forward to this post. I have been struggling with it myself. But still, the admin visits i.e. using, does not show up in analytics..still my visits to my website does show up still..any solution to hide that too…


    Raghu Pillai

  4. I found out that Analytic tracks the admin’s actions the other day when one of my top pages for last month was the admin login page!

  5. That’s a nice tip. Will try this out to see the change in my blog’s visitors count. 🙂

  6. You can also do it with php conditionals by fetching the preview value.

    if ( is_user_logged_in()||$_GET[‘preview’]

  7. Hi Ajith,

    Thank you for the tip. I’ve just included your piece of code and I hope it will work out well.

    I’ve set filters to exclude my IP address and it worked fine but then I moved in another place and completely forgot Google Analytics will consider me as another user.

  8. Thanks for this simple PHP code, added to my blog and tested. Its working fine and the page views of logged in users are not counted.

  9. Where does the code go? Header?

  10. Brittany Quinn :

    I tried the line of code in the footer of my website. Any clue why Google analytics is still showing mas as a active user? If I visit my website and type it into the Google search and click on it the real time statics doesn’t show me as a user. However if I login to my dashboard it shows me as a active user. I tried everything I blocked my IP address I tried blocking my hosting companies address. For some reason when I login to my dashboard and the real time statistic shows me as a active user it will go away after five minutes. But it is so annoying. Any tips our thoughts or solutions is there some magic plugin to fix this;.

Speak Your Mind