Why Your GTM4WP Plugin Shows visitorType: visitor-logged-out for Logged-In Users (And How to Fix It)
Content
If you're using the GTM4WP plugin and find that your dataLayer is incorrectly reporting a logged-in user's visitorType as "visitor-logged-out", you're not alone. This is a common issue that can break user tracking and reporting. This guide explains why this happens and provides the most effective solutions.
Why This Happens
The root cause of this issue is often related to custom user roles. The GTM4WP plugin identifies a user's type by checking their assigned role in WordPress. By default, the plugin's code attempts to read the first role in the user's roles array. However, some plugins or methods for creating custom user roles (such as the popular "User Role Editor" plugin) can assign the role to a different index in that array (e.g., index 4 instead of 0). When the plugin can't find a role at the expected index, it defaults to labeling the user as visitor-logged-out.
How to Fix It
Solution 1: Update the Plugin (Recommended)
The development team behind GTM4WP – A Google Tag Manager (GTM) plugin for WordPress has addressed this issue in a plugin update. The fix involves modifying the code to properly handle roles from any index in the array.
- Navigate to your WordPress dashboard.
- Go to Plugins and check if there is an update available for "GTM4WP".
- If an update is available, update the plugin. This should resolve the issue immediately.
Solution 2: Manual Code Edit (For Older Versions)
If you are unable to update the plugin immediately or are using an older version, you can apply the fix manually. Warning: Always back up your site and use a child theme or a custom functionality plugin before editing core plugin files, as your changes will be overwritten by the next update.
- Access your website's files via FTP or your hosting provider's file manager.
- Navigate to the plugin directory:
/wp-content/plugins/duracelltomi-google-tag-manager/ - Open the file
public/frontend.phpfor editing. - Find line 130 (or search for
'visitorType'). - Replace the existing line of code with the following:
$dataLayer['visitorType'] = ( $current_user->ID == 0 ? 'visitor-logged-out' : implode(",", $current_user->roles) ); - Save the file and clear your website and browser cache.
This change ensures all of a user's roles are compiled into a string, preventing the error that occurs when a role is not at the expected index.
Verifying the Fix
After applying either solution, test to confirm the fix is working:
- Open your website in a private/incognito browser window.
- Open your browser's developer tools (F12).
- Navigate to the Console tab.
- Type
dataLayerand press Enter to view its contents. - Look for the
visitorTypevariable. It should now correctly display the user's role (e.g.,"administrator","product_manager") instead of"visitor-logged-out".
By following these steps, you should be able to resolve the incorrect visitorType data and ensure your user tracking is accurate.
Related Support Threads Support
-
How to let GTM4WP know that the user has logged in?https://wordpress.org/support/topic/how-to-let-gtm4wp-know-that-the-user-has-logged-in/
-
User Id Tracking w/ GTM/GA4 Resulting In “Undefined” Valuehttps://wordpress.org/support/topic/user-id-tracking-w-gtm-ga4-resulting-in-undefined-value/
-
UserID–>visitorId or IP please?https://wordpress.org/support/topic/userid-visitorid-or-ip-please/
-
Users by User property Trackinghttps://wordpress.org/support/topic/users-by-user-property-tracking/
-
Leads & Logged out user information – dataLayerhttps://wordpress.org/support/topic/leads-logged-out-user-information-datalayer/
-
Ability to Hash Visitor Email using SHA256https://wordpress.org/support/topic/ability-to-hash-visitor-email-using-sha256/
-
Incorrect formName, formIDhttps://wordpress.org/support/topic/incorrect-formname-formid/
-
GTM Woo visitor type = visitor-logged-outhttps://wordpress.org/support/topic/gtm-woo-visitor-type-visitor-logged-out/
-
I’m logged in but the dataLayer says visitorType: “visitor-logged-out”. Why?https://wordpress.org/support/topic/im-logged-in-but-the-datalayer-says-visitortype-visitor-logged-out-why/
-
How to get data from data layer into GA4https://wordpress.org/support/topic/how-to-get-data-from-data-layer-into-ga4/
-
visitorEmail empty (“”) instead of undefinedhttps://wordpress.org/support/topic/visitoremail-empty-instead-of-undefined/
-
User Idhttps://wordpress.org/support/topic/user-id-15/
-
Registration Date formathttps://wordpress.org/support/topic/registration-date-format-2/
-
Get users data from BudyPress?https://wordpress.org/support/topic/get-users-data-from-budypress/
-
Event IDs for web and server conversionshttps://wordpress.org/support/topic/event-ids-for-web-and-server-conversions/
-
Blocking triggers for page viewshttps://wordpress.org/support/topic/blocking-triggers-for-page-views/
-
UserIdhttps://wordpress.org/support/topic/userid-2/
-
Parent Id instead of variation id in datalayer view-itemhttps://wordpress.org/support/topic/parent-id-instead-of-variation-id-in-datalayer-view-item/