Back to Community

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.

  1. Navigate to your WordPress dashboard.
  2. Go to Plugins and check if there is an update available for "GTM4WP".
  3. 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.

  1. Access your website's files via FTP or your hosting provider's file manager.
  2. Navigate to the plugin directory: /wp-content/plugins/duracelltomi-google-tag-manager/
  3. Open the file public/frontend.php for editing.
  4. Find line 130 (or search for 'visitorType').
  5. Replace the existing line of code with the following:
    $dataLayer['visitorType'] = ( $current_user->ID == 0 ? 'visitor-logged-out' : implode(",", $current_user->roles) );
  6. 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:

  1. Open your website in a private/incognito browser window.
  2. Open your browser's developer tools (F12).
  3. Navigate to the Console tab.
  4. Type dataLayer and press Enter to view its contents.
  5. Look for the visitorType variable. 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