Tuesday 10 January 2012

How to Leave out WordPress Soaked in Person Page Opinions from Search engines 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 + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-3943297-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>

<?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.

ADVERTISEMENT

Related Posts Plugin for WordPress, Blogger...