Hide Analytics from logged in users

Is there a way to edit the plugin to not track logged in users. Im getting a lot of hit on the preview pages when I'm testing a new post or when i am checking things out when i am logged in.

  • Bojan Radonic
    • Head of Support

    Hey there @jfriedman,

    How are you doing today?

    We could try to limit that with PHP by adding a condition to remove the tracking code for logged in users, please try adding the following to your theme functions.php (Ideally you'd want this added to your child theme functions.php so you can avoid having it removed each time you update the theme)

    if(is_user_logged_in()) {
    remove_action( 'admin_head', array( $google_analytics_async, 'tracking_code_output' ) );
    }

    Hope this helps :slight_smile:

    Cheers,
    Bojan

    • Ellis Benus
      • Site Builder, Child of Zeus

      I just attempted this and it did not remove the code when I'm logged in.

      Based on your code, wouldn't that have to be added to the header.php file of a theme?

      For it to be in functions, wouldn't we need to remove the the google_analytics_async then add a new action to admin_head that checks if the user is logged in and adds the action if they aren't?

      Also, where is it pulling the variable $google_analytics_async from? There is nothing referenced there...?

      Update: Completely wrong way to do this, but I immediately fixed the issue by modifying file google-analytics-async.php and "function tracking_code_output()" I added if ( !is_user_logged_in() ) : immediately after the function call and added endif; right before the closing braces of the function.

      function tracking_code_output() {
                if ( !is_user_logged_in() ) :
                // the $network_settings line below is the first line of the function, above which I added my IF statement
                $network_settings = isset( $this->network_settings['track_settings'] ) ? $this->network_settings['track_settings'] : array();
      
                // a lot more code not shown here
      
                <?php
                endif; // this was an existing endif already present
                endif; // end if !is_user_logged_in
      }

      Again, this is a terrible fix, but it worked for me for now, until support tells me the right way to remove_action for logged in users.

      • Kasia Swiderska
        • Support nomad

        Hello Ellis,

        Please check this code:

        add_action('plugins_loaded', 'ga_remove_logged_in');
        function ga_remove_logged_in() {
        global $google_analytics_async;
        if(is_user_logged_in()) {
        remove_action( 'admin_head', array( $google_analytics_async, 'tracking_code_output' ) );
        remove_action( 'wp_head', array( $google_analytics_async, 'tracking_code_output' ) );
        }
        }

        kind regards,
        Kasia

  • rootdown
    • Flash Drive

    Hi thanks for the info and sorry for the delayed response. I have yet to try this but I will and I will report back how it works. In the meantime I typically used Firefox Developer Edition when working on my own pages. I installed a extension called Ghostery which effectively blocks the analytics javascript from being executed within my browser session.

  • Bojan Radonic
    • Head of Support

    Hey guys :slight_smile:

    Ellis Benus I could move this to features and feedback section and if it proves to be a popular feature we'll consider adding it in the future versions.

    Jeff thanks for posting an alternative solution so other members can benefit from it, I'll throw couple of points to you for being awesome :slight_smile:

    Have a great day!

    Cheers,
    Bojan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.