Google Analytics is the web's most favorite tracking application used by millions. They are also rolling out real-time analytics. But if you're the admin of a site and you're testing several features of a site, you probably won't want to a track your visits. Here's how you can do that in Wordpress. Google Analytics' tracking code is inserted inside the <head> tag. So, you can restrict this insertion using the following codes in your theme's header.php file:
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXXX-X']);
  _gaq.push(['_setDomainName', 'domain.tld']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);

<?php endif; ?>
This is pretty simple: it checks whether the logged in user is admin. If not, it inserts the tracking codes. You can find more about Roles and Permissions in Wordpress. Note: Do change UA-XXXXXX-X and domain.tld with correct values.

Labels: ,

blog comments powered by Disqus