# Google Analytics 4 Without Tracking Logged-In Staff

> Outputs the GA4 tag only for visitors and customers, so your own visits as an author, editor or administrator do not skew the statistics.

- Type: PHP
- Runs: Frontend only
- Category: [Analytics & Tracking](https://wpide.com/library/category/analytics/)
- Tags: analytics, google-analytics, ga4, tracking
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/google-analytics-4-exclude-staff/

## Code

```php
add_action( 'wp_head', function () {
    $measurement_id = 'G-XXXXXXXXXX'; // Your GA4 Measurement ID.

    if ( current_user_can( 'edit_posts' ) || 'G-XXXXXXXXXX' === $measurement_id ) {
        return;
    }

    printf(
        "<script async src=\"%s\"></script>\n<script>window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config',%s);</script>\n",
        esc_url( 'https://www.googletagmanager.com/gtag/js?id=' . rawurlencode( $measurement_id ) ),
        wp_json_encode( $measurement_id )
    );
}, 5 );
```

## Notes

Replace G-XXXXXXXXXX with your Measurement ID. Nothing is printed while the placeholder is still in place.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Google Analytics 4 Without Tracking Logged-In Staff" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
