The problems: you want to collect data about visits to your Wordpress powered blog, but you don’t want to count visits of yourself or any authenticated users (that, of course, contribute to your blog). This problem really exists for small to moderate blogs where Google Analytics not cutting this out can really influence your stats. It also shows real visitors on your blog.
I’m not going to describe how Google Analytics works or how to set it up in detail. You can find all this here: http://www.google.com/support/googleanalytics/ and here: http://www.google.com/support/googleanalytics/bin/answer.py?answer=55488&utm_source=DiscoverList&utm_medium=et&utm_campaign=en_us&hl=en
The code that you’re supposed to insert into your code (usually in wordpress footer.php for the current theme) looks like this:
<script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try{
var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
pageTracker._trackPageview();
} catch(err) {}
</script>
If you put only this into the footer you’re going to get Analytics tracking everything on your site.
To enable it only for authenticated users use following code:
<?php
if ( is_user_logged_in() )
{
echo “<!– “;
}
?><script type=”text/javascript”>
var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
document.write(unescape(“%3Cscript src=’” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
</script>
<script type=”text/javascript”>
try{
var pageTracker = _gat._getTracker(“UA-xxxxxx-x”);
pageTracker._trackPageview();
} catch(err) {}
</script><?php
if ( is_user_logged_in() )
{
echo ” –>”;
}
?>
What this does is when an authenticated user is on the site if comments out (by outputting <– before and –> after) the script that reports back to Analytics. That’s it.
Don’t forget to replace UA-xxxxxx-x with your own code.
Related posts:

Leave a Reply
Using Gravatars in the comments - get your own and be recognized!
XHTML: These are some of the tags you can use:
<a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>