gtm4wp icon indicating copy to clipboard operation
gtm4wp copied to clipboard

Add Custom Fields to datalayer

Open hans2103 opened this issue 11 months ago • 8 comments

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.

  • The attributes array has to be extended with shipping_weight
  • The customer array has to be extended with custom_group
  • The customer array has to be extended with client_type (new / exisiting)
  • The order_items has to be extend with product_weight

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

How can I achieve these changes?

hans2103 avatar Mar 12 '24 15:03 hans2103

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 avatar Mar 13 '24 15:03 hans2103

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

hans2103 avatar Mar 13 '24 15:03 hans2103

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

hans2103 avatar Mar 15 '24 19:03 hans2103

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

duracelltomi avatar Mar 18 '24 09:03 duracelltomi

is it an idea to release 1.20.1 ?

hans2103 avatar Mar 18 '24 10:03 hans2103

yes, it will be in v1.20.1

duracelltomi avatar Mar 18 '24 10:03 duracelltomi

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

hans2103 avatar Mar 20 '24 15:03 hans2103

I just released the v1.20.1

duracelltomi avatar Mar 21 '24 14:03 duracelltomi