Exclude logged in users in WordPress from Google Analytics

While I was setting up this blog, I noticed that Google Analytics was tracking my activity e.g. editing the theme and previewing posts. So I searched for a solution to exclude all my activities.

I looked over the WordPress functions and found the is_user_logged_in() function. So I went into my header.php file and surrounded the Google Analytics code with some PHP:

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

### Your Google Analytics code goes here! ###

<?php } ?>

If you want to see if it’s working you can add this lines instead the ones above:

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

### Your Google Analytics code goes here! ###

<?php }else{ ?>
<!– Removed Analytics Code –>
<?php } ?>

With this, you can see the the message „Removed Analytics Code“ in the source code if your logged in.