How to Disable Google Analytics for Logged In Users in WordPress

Home » Snippets » How to Disable Google Analytics for Logged In Users in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you using Google Analytics to track your website’s traffic and user behavior? If so, you may have noticed that even logged in users, such as admins or contributors, are being counted in your analytics data. This can skew your insights and make it difficult to accurately analyze your website’s performance. In this article, we will show you how to disable Google Analytics for logged in users in WordPress, so you can get a more accurate representation of your website’s traffic.

					function wpturbo_disable_google_analytics() {
    if (is_user_logged_in()) {
        ?>

        <script>
            gtag('config', 'YOUR_ANALYTICS_ID', {
                'send_page_view': false
            });
        </script>

        <?php
    }
}
add_action('wp_head', 'wpturbo_disable_google_analytics');
				

The code snippet provided allows you to disable Google Analytics tracking for logged-in users on your WordPress website. This can be useful if you don't want to track your own visits or the visits of other users with admin access.

The wpturbo_disable_google_analytics() function serves as a conditional wrapper for the script that disables tracking. The is_user_logged_in() function is used to check if the current visitor is logged in as a user. If they are, the script to disable tracking is outputted.

Inside the script, we use the gtag() function, provided by the Google Analytics Tracking code, to configure the tracking for the specified tracking ID (YOUR_ANALYTICS_ID). The 'send_page_view': false configuration parameter is set to disable sending the page view when a logged-in user visits the website.

By adding the wp_head action hook and attaching it to the wpturbo_disable_google_analytics() function, the script is outputted in the <head> section of your website's HTML. This ensures that the tracking code is disabled for logged-in users on all pages of your WordPress website.

Remember to replace YOUR_ANALYTICS_ID with your actual Google Analytics tracking ID.

Register an account to save your snippets or go Pro to get more features.