# Send Login Status and User Role to GA4

> Sets GA4 user properties for whether the visitor is logged in and their role (for example customer or subscriber), so you can compare members and guests in reports.

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

## Code

```php
add_action( 'wp_head', function () {
    $user  = wp_get_current_user();
    $props = array(
        'logged_in' => $user->exists() ? 'yes' : 'no',
        'user_role' => $user->exists() && $user->roles ? (string) reset( $user->roles ) : 'guest',
    );

    echo "<script>window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('set', 'user_properties', " . wp_json_encode( $props ) . ");</script>\n";
}, 2 );
```

## Notes

Register logged_in and user_role as user-scoped custom dimensions in GA4 > Admin > Custom definitions. No personal data such as names or emails is sent.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Send Login Status and User Role to GA4" 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/
