Back to Community

Troubleshooting Missing Customer Data in GTM4WP for WooCommerce Guest Orders

Content

Why is Customer Data Missing for Guest Checkouts in GTM4WP?

A common issue reported by users of the 'GTM4WP – A Google Tag Manager (GTM) plugin for WordPress' is that the customer data layer appears empty on the order confirmation page when a customer checks out as a guest. This is particularly problematic for tracking solutions like Google Ads Enhanced Conversions, which rely on data points like customerBillingEmail.

Understanding the Expected Behavior

Based on community reports, this behavior is, in fact, by design in many versions of the plugin. The "Customer data in data layer" feature primarily pulls information from the logged-in WordPress user object. When a customer is not logged in (a "guest"), this user object does not exist, and consequently, the corresponding data layer variables (e.g., customerBillingFirstName, customerBillingEmail) remain empty.

How to Resolve Missing Guest Data

There are two primary methods to populate the data layer for guest orders.

Method 1: Using a Custom Code Snippet (Recommended for Developers)

The most robust solution is to use a WordPress filter hook to add the necessary order data to the data layer on the thank you page. Add the following code to your theme's functions.php file or a custom functionality plugin.

add_filter( 'gtm4wp_woocommerce_datalayer', 'my_custom_guest_order_data' );
function my_custom_guest_order_data( $dataLayer ) {
    // Check if we are on the order received page and an order ID exists
    if ( ! is_order_received_page() || empty( $_GET['key'] ) ) {
        return $dataLayer;
    }

    // Get the order ID from the order key
    $order_id = wc_get_order_id_by_order_key( $_GET['key'] );
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return $dataLayer;
    }

    // Populate the data layer variables from the order object
    $dataLayer['customerBillingEmail'] = $order->get_billing_email();
    $dataLayer['customerBillingFirstName'] = $order->get_billing_first_name();
    $dataLayer['customerBillingLastName'] = $order->get_billing_last_name();
    $dataLayer['customerBillingCity'] = $order->get_billing_city();
    $dataLayer['customerBillingPostcode'] = $order->get_billing_postcode();
    $dataLayer['customerBillingCountry'] = $order->get_billing_country();
    $dataLayer['customerBillingState'] = $order->get_billing_state(); // Adds missing state field
    $dataLayer['customerBillingPhone'] = $order->get_billing_phone();
    // Add any other custom fields you need

    return $dataLayer;
}

Important Note: This code directly accesses the WooCommerce order object, which contains all the customer's billing information regardless of their login status, effectively solving the problem for guest checkouts.

Method 2: Checking Plugin and Theme Conflicts

In some cases, the issue might be exacerbated by a conflict. Before adding code, try this basic troubleshooting:

  1. Update Everything: Ensure WordPress, WooCommerce, the GTM4WP plugin, and your theme are all updated to their latest versions. The GTM4WP team has been known to address such issues in updates.
  2. Conflict Test: Temporarily switch to a default WordPress theme (like Twenty Twenty-Four) and disable all plugins except for WooCommerce and GTM4WP. If the data appears, re-enable your plugins and theme one by one to identify the culprit.
  3. Check Checkout Flow: Some third-party checkout or funnel builder plugins can interfere with the standard WooCommerce order confirmation page. Test with a standard WooCommerce checkout process.

Conclusion

The absence of customer data for guest users in the GTM4WP data layer is a known limitation of the plugin's default functionality. While the built-in option is designed for logged-in users, the provided custom code offers a powerful and reliable workaround to capture this critical information for all orders, enabling full compatibility with advertising conversion tracking requirements.

Related Support Threads Support