diff --git a/admin/admin-tab-integrate.php b/admin/admin-tab-integrate.php
index c84f9552..eb84e23d 100644
--- a/admin/admin-tab-integrate.php
+++ b/admin/admin-tab-integrate.php
@@ -147,6 +147,24 @@
),
'phase' => GTM4WP_PHASE_STABLE,
),
+ GTM4WP_OPTION_INTEGRATE_WCCLEARECOMMERCEDL => array(
+ 'label' => esc_html__( 'Clear ecommerce object before new event', 'duracelltomi-google-tag-manager' ),
+ 'description' => sprintf(
+ gtm4wp_safe_admin_html(
+ // translators: 1: anchor element linking to the official GA4 doc about clearing the ecommerce object. 2: closing anchor element.
+ __(
+ 'Clear the ecommerce object before any new event being pushed into the data layer.
+ Althought it is %1$srecommended by Google%2$s, it is not mandatory to activate this feature as
+ the GA4 event tag reads only the last pushed ecommerce data on any new event.
+ Use it if you encounter issues with your GTM implementation.',
+ 'duracelltomi-google-tag-manager'
+ )
+ ),
+ '',
+ ''
+ ),
+ 'phase' => GTM4WP_PHASE_STABLE,
+ ),
GTM4WP_OPTION_INTEGRATE_GOOGLEOPTIMIZEIDS => array(
'label' => esc_html__( 'Google Optimize container ID list', 'duracelltomi-google-tag-manager' ),
diff --git a/common/readoptions.php b/common/readoptions.php
index 43616484..3ed29365 100644
--- a/common/readoptions.php
+++ b/common/readoptions.php
@@ -124,6 +124,7 @@
define( 'GTM4WP_OPTION_INTEGRATE_WCEXCLUDETAX', 'integrate-woocommerce-exclude-tax' );
define( 'GTM4WP_OPTION_INTEGRATE_WCEXCLUDESHIPPING', 'integrate-woocommerce-exclude-shipping' );
define( 'GTM4WP_OPTION_INTEGRATE_WCNOORDERTRACKEDFLAG', 'integrate-woocommerce-do-not-use-order-tracked-flag' );
+define( 'GTM4WP_OPTION_INTEGRATE_WCCLEARECOMMERCEDL', 'integrate-woocommerce-clear-ecommerce-datalayer' );
define( 'GTM4WP_OPTION_INTEGRATE_GOOGLEOPTIMIZEIDS', 'integrate-google-optimize-idlist' );
define( 'GTM4WP_OPTION_INTEGRATE_GOOGLEOPTIMIZETIMEOUT', 'integrate-google-optimize-timeout' );
@@ -225,6 +226,7 @@
GTM4WP_OPTION_INTEGRATE_WCEXCLUDETAX => false,
GTM4WP_OPTION_INTEGRATE_WCEXCLUDESHIPPING => false,
GTM4WP_OPTION_INTEGRATE_WCNOORDERTRACKEDFLAG => false,
+ GTM4WP_OPTION_INTEGRATE_WCCLEARECOMMERCEDL => false,
GTM4WP_OPTION_INTEGRATE_GOOGLEOPTIMIZEIDS => '',
GTM4WP_OPTION_INTEGRATE_GOOGLEOPTIMIZETIMEOUT => 4000,
diff --git a/integration/woocommerce.php b/integration/woocommerce.php
index 95b8f6de..bd9954c0 100755
--- a/integration/woocommerce.php
+++ b/integration/woocommerce.php
@@ -398,6 +398,7 @@ function gtm4wp_woocommerce_addglobalvars( $return ) {
$return['gtm4wp_needs_shipping_address'] = (bool) $gtm4wp_needs_shipping_address;
$return['gtm4wp_business_vertical'] = esc_js( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCBUSINESSVERTICAL ] );
$return['gtm4wp_business_vertical_id'] = gtm4wp_get_gads_product_id_variable_name( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCBUSINESSVERTICAL ] );
+ $return['gtm4wp_clear_ecommerce'] = (bool) ( $gtm4wp_options[ GTM4WP_OPTION_INTEGRATE_WCCLEARECOMMERCEDL ] );
return $return;
}
diff --git a/js/gtm4wp-woocommerce-enhanced.js b/js/gtm4wp-woocommerce-enhanced.js
index 580a21ad..4adce3a1 100644
--- a/js/gtm4wp-woocommerce-enhanced.js
+++ b/js/gtm4wp-woocommerce-enhanced.js
@@ -57,6 +57,29 @@ function gtm4wp_map_eec_to_ga4( productdata ) {
return ga4_product;
}
+function gtm4wp_push_ecommerce( event_name, items, extra_params, event_callback=false, event_timeout=2000 ) {
+ const ecom_obj = extra_params || {};
+ ecom_obj.items = items;
+
+ if (gtm4wp_clear_ecommerce) {
+ window[ gtm4wp_datalayer_name ].push({
+ ecommerce: null
+ });
+ }
+
+ const dl_obj = {
+ 'event': event_name,
+ 'ecommerce': ecom_obj
+ };
+
+ if (event_callback) {
+ dl_obj.eventCallback = event_callback;
+ dl_obj.eventTimeout = event_timeout;
+ }
+
+ window[ gtm4wp_datalayer_name ].push(dl_obj);
+}
+
function gtm4wp_handle_cart_qty_change() {
document.querySelectorAll( '.product-quantity input.qty' ).forEach(function( qty_el ) {
const original_value = qty_el.defaultValue;
@@ -111,13 +134,9 @@ function gtm4wp_handle_cart_qty_change() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_to_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency, // ga4 version
- 'value': productprice.toFixed(2) * (current_value - original_value),
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- }
+ gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency, // ga4 version
+ 'value': productprice.toFixed(2) * (current_value - original_value)
});
} else {
// no => handle remove from cart event
@@ -144,13 +163,9 @@ function gtm4wp_handle_cart_qty_change() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'remove_from_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': productprice.toFixed(2) * (original_value - current_value),
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- }
+ gtm4wp_push_ecommerce( 'remove_from_cart', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': productprice.toFixed(2) * (original_value - current_value)
});
}
} // end if qty changed
@@ -192,14 +207,10 @@ function gtm4wp_handle_payment_method_change() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_payment_info',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'payment_type': payment_type,
- 'value': window.gtm4wp_checkout_value,
- 'items': window.gtm4wp_checkout_products_ga4
- }
+ gtm4wp_push_ecommerce( 'add_payment_info', window.gtm4wp_checkout_products_ga4, {
+ 'currency': gtm4wp_currency,
+ 'payment_type': payment_type,
+ 'value': window.gtm4wp_checkout_value
});
gtm4wp_checkout_step_fired.push( 'payment_method' );
@@ -240,14 +251,10 @@ function gtm4wp_handle_shipping_method_change() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_shipping_info',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'shipping_tier': shipping_tier,
- 'value': window.gtm4wp_checkout_value,
- 'items': window.gtm4wp_checkout_products_ga4
- }
+ gtm4wp_push_ecommerce( 'add_shipping_info', window.gtm4wp_checkout_products_ga4, {
+ 'currency': gtm4wp_currency,
+ 'shipping_tier': shipping_tier,
+ 'value': window.gtm4wp_checkout_value
});
gtm4wp_checkout_step_fired.push( 'shipping_method' );
@@ -374,12 +381,8 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'view_item_list',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'items': ga4_chunk
- }
+ gtm4wp_push_ecommerce( 'view_item_list', ga4_chunk, {
+ 'currency': gtm4wp_currency
});
}
} else {
@@ -465,13 +468,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_to_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': productprice.toFixed(2),
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- }
+ gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': productprice.toFixed(2)
});
});
@@ -519,13 +518,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_to_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity,
- 'items': [ gtm4wp_map_eec_to_ga4( gtm4wp_last_selected_product_variation ) ]
- }
+ gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_map_eec_to_ga4( gtm4wp_last_selected_product_variation ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity
});
}
} else if ( product_is_grouped ) {
@@ -577,13 +572,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_to_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': sum_value,
- 'items': ga4_products
- }
+ gtm4wp_push_ecommerce( 'add_to_cart', ga4_products, {
+ 'currency': gtm4wp_currency,
+ 'value': sum_value
});
} else {
const product_id_el = gtm4wp_use_sku_instead ? product_form.querySelector( '[name=gtm4wp_sku]' ) : product_form.querySelector( '[name=gtm4wp_id]' );
@@ -609,13 +600,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'add_to_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': product_data.price * product_data.quantity,
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- }
+ gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': product_data.price * product_data.quantity
});
}
});
@@ -673,13 +660,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'remove_from_cart',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': product_data.price * product_data.quantity,
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- }
+ gtm4wp_push_ecommerce( 'remove_from_cart', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': product_data.price * product_data.quantity
});
});
@@ -789,26 +772,20 @@ function gtm4wp_process_woocommerce_pages() {
}
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'select_item',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'items': [ gtm4wp_map_eec_to_ga4( product_data ) ]
- },
- 'eventCallback': function( container_id ) {
- if ( "undefined" !== typeof container_id && window.gtm4wp_first_container_id != container_id) {
- // only call this for the first loaded container
- return true;
- }
-
- if ( ( target_new_tab || ctrl_key_pressed ) && productpage_window ) {
- productpage_window.location.href = dom_productdata.getAttribute( 'data-gtm4wp_product_url' );
- } else {
- document.location.href = dom_productdata.getAttribute( 'data-gtm4wp_product_url' );
- }
- },
- 'eventTimeout': 2000
- });
+ gtm4wp_push_ecommerce( 'select_item', [ gtm4wp_map_eec_to_ga4( product_data ) ], {
+ 'currency': gtm4wp_currency
+ }, function( container_id ) {
+ if ( "undefined" !== typeof container_id && window.gtm4wp_first_container_id != container_id) {
+ // only call this for the first loaded container
+ return true;
+ }
+
+ if ( ( target_new_tab || ctrl_key_pressed ) && productpage_window ) {
+ productpage_window.location.href = dom_productdata.getAttribute( 'data-gtm4wp_product_url' );
+ } else {
+ document.location.href = dom_productdata.getAttribute( 'data-gtm4wp_product_url' );
+ }
+ }, 2000);
},
'eventTimeout': 2000
});
@@ -876,13 +853,9 @@ function gtm4wp_process_woocommerce_pages() {
});
// fire ga4 version
- window[ gtm4wp_datalayer_name ].push({
- 'event': 'view_item',
- 'ecommerce': {
- 'currency': gtm4wp_currency,
- 'value': current_product_detail_data.price,
- 'items': [ gtm4wp_map_eec_to_ga4( current_product_detail_data ) ]
- }
+ gtm4wp_push_ecommerce( 'view_item', [ gtm4wp_map_eec_to_ga4( current_product_detail_data ) ], {
+ 'currency': gtm4wp_currency,
+ 'value': current_product_detail_data.price
});
if ( document.readyState === "interactive" ) {
@@ -1090,7 +1063,7 @@ function gtm4wp_process_woocommerce_pages() {
// this part of the code is deprecated and will be removed in a later version
// therefore jQuery usage will be not rewritten
// turn of the deprecated Google Ads remarketing feature and this code will not execute
- if ( window.gtm4wp_remarketing&& !gtm4wp_is_cart && !gtm4wp_is_checkout ) {
+ if ( window.gtm4wp_remarketing && !gtm4wp_is_cart && !gtm4wp_is_checkout ) {
if ( jQuery( '.gtm4wp_productdata' ).length > 0 ) {
for( var i=0; i