duracelltomi / gtm4wp

Google Tag Manager plugin for WordPress
https://gtm4wp.com/
GNU General Public License v3.0
146 stars 96 forks source link

Add Custom Fields to datalayer #330

Closed hans2103 closed 5 months ago

hans2103 commented 6 months ago

The array below contains the order data of a WooCommerce order

https://github.com/duracelltomi/gtm4wp/blob/a08afa9b5bf3b8f107152b5dbc14db53d880db98/integration/woocommerce.php#L226-L291

I want to be able to extend the list with custom fields.

In Google Tag Manager I will create variables to push this information to Google Analytics

How can I achieve these changes?

hans2103 commented 6 months ago

After placing a dummy order i can see the dataLayer being filled with a lot of data. After opening the same url (/checkout/order-received/57955/?key=wc_order_PvUK5QCkAuiPa) the dataLayer is not filled anymore,

How can I test the success page of an order with a filled DataLayer?

hans2103 commented 6 months ago

I am trying to create a filter for https://gtm4wp.com/gtm4wp-for-developers/actions-and-filters-in-gtm4wp-for-developers#gtm4wpeecproductarray-filter

hans2103 commented 6 months ago

New_Customer

Found the new_customer = true/false in the code. Added a new GTM variable to get new_customer from the dataLayer and send it to GA4 using the GA4-Event Tag

Product Weight

add_filter('gtm4wp_eec_product_array', 'custom_gtm4wp_eec_product_array', 10, 2);

function custom_gtm4wp_eec_product_array(array $eec_product, string $ecommerce_action): array
{
    // add an attribute only on purchase pages
    if ($ecommerce_action === "purchase")
    {
        $product               = wc_get_product($eec_product['internal_id']);
        $eec_product['weight'] = $product->get_weight();
    }

    return $eec_product;
}

Created a Custom Dimension in GA4 to retrieve the product weight from the orderData.items information

onwards to add total order weight to orderData

duracelltomi commented 6 months ago

Some parts of the data layer is not shown while loading the same order received page multiple times to prevent tracking the same purchase event multiple times. This is not the case the the orderData variable since this is not directly meant to be used with ecomerce tracking in Google Analytics.

I've added a new filter to be able to add custom data to the orderData variable: https://github.com/duracelltomi/gtm4wp/commit/68c07ba9dcc8f94f4c1f42c38be32adbf27a9b00

hans2103 commented 6 months ago

is it an idea to release 1.20.1 ?

duracelltomi commented 6 months ago

yes, it will be in v1.20.1

hans2103 commented 6 months ago

can you give me an indication when the release will appear?

duracelltomi commented 6 months ago

I just released the v1.20.1