Home / Admin / Hide Admins from being tracked
Duplicate Snippet

Embed Snippet on Your Site

Hide Admins from being tracked

Adds a function that will allow you to properly add all your tracking scripts and stop admins, editors, and authors from being tracked in Analytics, Tag Manager, or any other tracking scripts you have enabled. This will let you place the normal script in the header and the in the body. For example, Google Tag Manager will not fire when an Author, Editor, or Admin is logged into the site. This will help keep your analytics clean and only track real users.

Code Preview
php
<?php
/**
 * Start Tracking Tags
 **/
function hook_gtm_header() {
    if( !current_user_can('administrator') && !current_user_can( 'author' ) && !current_user_can( 'editor' ) ) {
    ?>
        <!-- Tracking Scripts Go Here -->
    <?php
        };
}
add_action('wp_head', 'hook_gtm_header');
function gtm_body_tag_code() {
  if( !current_user_can('administrator') && !current_user_can( 'author' ) && !current_user_can( 'editor' ) ) {
    ?>
        <!-- (noscript) Tracking Scripts Go Here -->
    <?php
        };
}
add_action('wp_body_open', 'gtm_body_tag_code');
/**
 * End Tracking Tags
 **/

Comments

Add a Comment