2
votes
    <script type="text/javascript">
   var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-123456789-1']);
  _gaq.push(['_trackPageview']);
 (function() {
      var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
 ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
 var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();
</script>

Google analytics provided me this script to put up on my website to track the visitors of my website, but i have observed that my own visits are also counted into the analysis. How do I disable this script if the admin is logged in to the wordpress account as the current user?

2

2 Answers

6
votes

If any user is logged in.

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

<script type="text/javascript">

GA code    

</script>

  <?php } ?>

http://codex.wordpress.org/Function_Reference/is_user_logged_in

To select a particular user, you need to use if($user_id) {

1
votes

This is one of the alternatives I can think of. Using this code the GA code won't execute for any Author, Contributor nor Admin, but In case your site allows Subscribers to log in you will track em.

<?php if ( !current_user_can('edit_posts') ) { ?>
GA code
<?php } ?>