Don’t Track Analytics for Admins

18 Jan 2013

Don’t Track Analytics for Admins


I often run into situations where I am editing a site, or checking my pages, but do not want to count the time i spend on my own site to count towards my Analytics Tracking. To fix this, I only display my analytics code when the user is not logged in, or is logged in as someone other than the administrator.

<?php
  global $user_ID;
  if( $user_ID ){
    if( !current_user_can('level_10') ){
      //logged in as someone other than admin
      //place analytics
    }
  } else {
    //not logged in
    //place analytics
  }
?>

This code checks for a user ID, if it cant find one it displays the tracking code. If there is an ID, then the user is logged in. If the user level is at 10, then it is an administrator and should not show the code. We use the exclamation point (the NOT logical operator) to verify that the user is NOT an admin. As a general rule of thumb, you should not have more than one administrator account on a site.

Leave a Reply

Your email address will not be published. Required fields are marked *