diff --git a/dist/js/gtm4wp-woocommerce.js b/dist/js/gtm4wp-woocommerce.js index 6557738f..72b5ac4c 100644 --- a/dist/js/gtm4wp-woocommerce.js +++ b/dist/js/gtm4wp-woocommerce.js @@ -1 +1 @@ -"use strict";var gtm4wp_last_selected_product_variation;function gtm4wp_woocommerce_handle_cart_qty_change(){document.querySelectorAll(".product-quantity input.qty").forEach(function(t){var e=t.defaultValue,o=parseInt(t.value);if(e!=(o=isNaN(o)?e:o)){var t=t.closest(".cart_item"),t=t&&t.querySelector(".remove");if(t)return!(t=gtm4wp_read_json_from_node(t,"gtm4wp_product_data"))||void(ediv:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.widget-product-item,.woocommerce-grouped-product-list-item__label a")){if("undefined"==typeof google_tag_manager)return!0;c=t.target,_=c.closest(".products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.products>div:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.widget-product-item,.woocommerce-grouped-product-list-item__label a");if(!_)return!0;var d,r=c.closest(".product,.wc-block-grid__product"),o=(r=(r=r||((r=c.closest(".products li"))||c.closest(".products>div")))||c.closest(".woocommerce-grouped-product-list-item__label"))?r.querySelector(".gtm4wp_productdata"):c,e=gtm4wp_read_json_from_node(o,"gtm4wp_product_data",["internal_id"]);if(!e)return!0;if(e.productlink!=_.getAttribute("href"))return!0;for(d in window.google_tag_manager)if("gtm-"==d.substring(0,4).toLowerCase()){window.gtm4wp_first_container_id=d;break}if(""===window.gtm4wp_first_container_id)return!0;var i=t.ctrlKey||t.metaKey,u="_blank"===_.target,p=t.defaultPrevented,m=(p||t.preventDefault(),(i||u)&&(window.productpage_window=window.open("about:blank","_blank")),e.productlink);delete e.productlink,gtm4wp_push_ecommerce("select_item",[e],{currency:gtm4wp_currency},function(t){if(void 0!==t&&window.gtm4wp_first_container_id!=t)return!0;p||((u||i)&&productpage_window?productpage_window.location.href=m:document.location.href=m)})}}),jQuery(document).on("found_variation",function(t,e){if(void 0!==e&&("interactive"!==document.readyState||!gtm4wp_view_item_fired_during_pageload)){t=t.target;if(!t)return!0;var o,t=t.querySelector("[name=gtm4wp_product_data]");if(!t)return!0;try{o=JSON.parse(t.value)}catch(t){return console&&console.error&&console.error(t.message),!0}o.price=gtm4wp_make_sure_is_float(o.price),o.id=e.variation_id,gtm4wp_use_sku_instead&&e.sku&&""!==e.sku&&(o.id=e.sku),o.price=gtm4wp_make_sure_is_float(e.display_price);var r,c=[];for(r in e.attributes)c.push(e.attributes[r]);o.variant=c.join(","),gtm4wp_last_selected_product_variation=o,gtm4wp_push_ecommerce("view_item",[o],{currency:gtm4wp_currency,value:o.price}),"interactive"===document.readyState&&(gtm4wp_view_item_fired_during_pageload=!0)}}),jQuery(".variations select").trigger("change"),jQuery(document).ajaxSuccess(function(t,e,o){void 0!==o&&-1div:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.widget-product-item,.woocommerce-grouped-product-list-item__label a")){if("undefined"==typeof google_tag_manager)return!0;c=t.target,_=c.closest(".products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.wc-block-grid__products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.products>div:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),.widget-product-item,.woocommerce-grouped-product-list-item__label a");if(!_)return!0;var i,r=c.closest(".product,.wc-block-grid__product"),o=(r=(r=r||((r=c.closest(".products li"))||c.closest(".products>div")))||c.closest(".woocommerce-grouped-product-list-item__label"))?r.querySelector(".gtm4wp_productdata"):c,e=gtm4wp_read_json_from_node(o,"gtm4wp_product_data",["internal_id"]);if(!e)return!0;if(e.productlink!=_.getAttribute("href"))return!0;for(i in window.google_tag_manager)if("gtm-"==i.substring(0,4).toLowerCase()){window.gtm4wp_first_container_id=i;break}if(""===window.gtm4wp_first_container_id)return!0;var d=t.ctrlKey||t.metaKey,u="_blank"===_.target,p=t.defaultPrevented,m=(p||t.preventDefault(),(d||u)&&(window.productpage_window=window.open("about:blank","_blank")),e.productlink);delete e.productlink,gtm4wp_push_ecommerce("select_item",[e],{currency:gtm4wp_currency},function(t){if(void 0!==t&&window.gtm4wp_first_container_id!=t)return!0;p||((u||d)&&productpage_window?productpage_window.location.href=m:document.location.href=m)})}}),jQuery(document).on("found_variation",function(t,e){if(void 0!==e&&("interactive"!==document.readyState||!gtm4wp_view_item_fired_during_pageload)){t=t.target;if(!t)return!0;var o,t=t.querySelector("[name=gtm4wp_product_data]");if(!t)return!0;try{o=JSON.parse(t.value)}catch(t){return console&&console.error&&console.error(t.message),!0}o.price=gtm4wp_make_sure_is_float(o.price),o.id=e.variation_id,gtm4wp_use_sku_instead&&e.sku&&""!==e.sku&&(o.id=e.sku),o.price=gtm4wp_make_sure_is_float(e.display_price);var r,c=[];for(r in e.attributes)c.push(e.attributes[r]);o.variant=c.join(","),gtm4wp_last_selected_product_variation=o,gtm4wp_push_ecommerce("view_item",[o],{currency:gtm4wp_currency,value:o.price}),"interactive"===document.readyState&&(gtm4wp_view_item_fired_during_pageload=!0)}}),jQuery(".variations select").trigger("change"),jQuery(document).ajaxSuccess(function(t,e,o){void 0!==o&&-1 $order_item->get_quantity(), 'price' => $product_price, + 'item' => $order_item, ), 'purchase' ); @@ -466,7 +467,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $data_layer ) { $eec_product_array = gtm4wp_woocommerce_process_product( $product, array( - 'quantity' => $cart_item_data['quantity'], + 'quantity' => $cart_item_data['quantity'], + 'cart_item' => $cart_item_data, ), 'cart' ); @@ -556,7 +558,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $data_layer ) { $eec_product_array = gtm4wp_woocommerce_process_product( $product, array( - 'quantity' => $cart_item_data['quantity'], + 'quantity' => $cart_item_data['quantity'], + 'cart_item' => $cart_item_data, ), 'cart' ); @@ -753,7 +756,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $data_layer ) { $eec_product_array = gtm4wp_woocommerce_process_product( $product, array( - 'quantity' => $cart_item_data['quantity'], + 'quantity' => $cart_item_data['quantity'], + 'cart_item' => $cart_item_data, ), 'checkout' ); @@ -796,7 +800,8 @@ function gtm4wp_woocommerce_datalayer_filter_items( $data_layer ) { $eec_product_array = gtm4wp_woocommerce_process_product( $product, array( - 'quantity' => $cart_item['quantity'], + 'quantity' => $cart_item['quantity'], + 'cart_item' => $cart_item, ), 'readdedtocart' ); diff --git a/js/gtm4wp-woocommerce.js b/js/gtm4wp-woocommerce.js index 70a18801..ed7c2099 100644 --- a/js/gtm4wp-woocommerce.js +++ b/js/gtm4wp-woocommerce.js @@ -205,12 +205,12 @@ function gtm4wp_woocommerce_process_pages() { // manage events related to user clicks document.addEventListener( 'click', function( e ) { let event_target_element = e.target; - + if ( !event_target_element ) { // for some reason event target is not specificed return true; } - + // track add to cart events for simple products in product lists if ( event_target_element.closest( '.add_to_cart_button:not(.product_type_variable, .product_type_grouped, .single_add_to_cart_button)' ) ) { const product_el = event_target_element.closest( '.product,.wc-block-grid__product' ); @@ -224,7 +224,7 @@ function gtm4wp_woocommerce_process_pages() { if ( !productdata ) { return true; } - + if ( productdata.productlink ) { delete productdata.productlink; } @@ -236,22 +236,22 @@ function gtm4wp_woocommerce_process_pages() { 'value': productdata.price }); } - + // track add to cart events for products on product detail pages if ( event_target_element.closest( '.single_add_to_cart_button:not(.disabled)' ) ) { const product_form = event_target_element.closest( 'form.cart' ); if ( !product_form ) { return true; } - + let product_variant_id = product_form.querySelectorAll( '[name=variation_id]' ); let product_is_grouped = product_form.classList && product_form.classList.contains( 'grouped_form' ); - + if ( product_variant_id.length > 0 ) { if ( gtm4wp_last_selected_product_variation ) { const qty_el = product_form.querySelector( '[name=quantity]' ); gtm4wp_last_selected_product_variation.quantity = (qty_el && qty_el.value) || 1; - + gtm4wp_push_ecommerce( 'add_to_cart', [ gtm4wp_last_selected_product_variation ], { 'currency': gtm4wp_currency, 'value': (gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity).toFixed(2) @@ -261,35 +261,35 @@ function gtm4wp_woocommerce_process_pages() { const products_in_group = document.querySelectorAll( '.grouped_form .gtm4wp_productdata' ); let products = []; let sum_value = 0; - + products_in_group.forEach( function( product_data_el ) { const productdata = gtm4wp_read_json_from_node(product_data_el, 'gtm4wp_product_data', ['productlink']); if ( !productdata ) { return true; } - + const product_qty_input = document.querySelectorAll( 'input[name=quantity\\[' + productdata.internal_id + '\\]]' ); if ( product_qty_input.length > 0 ) { product_qty = (product_qty_input[0] && product_qty_input[0].value) || 1; } else { return true; } - + if ( 0 == product_qty ) { return true; } productdata.quantity = product_qty; delete productdata.internal_id; - + products.push( productdata ); sum_value += productdata.price * productdata.quantity; }); - + if ( 0 == products.length ) { return true; } - + gtm4wp_push_ecommerce( 'add_to_cart', products, { 'currency': gtm4wp_currency, 'value': sum_value.toFixed(2) @@ -302,21 +302,21 @@ function gtm4wp_woocommerce_process_pages() { let productdata = gtm4wp_read_from_json( product_data_el.value ); productdata.quantity = product_form.querySelector( '[name=quantity]' ) && product_form.querySelector( '[name=quantity]' ).value; - if ( isNaN( productdata.quantity ) ) { + if ( isNaN( productdata.quantity ) || ( productdata.quantity !== 0 && !productdata.quantity ) ) { productdata.quantity = 1; } - + gtm4wp_push_ecommerce( 'add_to_cart', [ productdata ], { 'currency': gtm4wp_currency, 'value': productdata.price * productdata.quantity }); } } - + // track remove links in mini cart widget and on cart page if ( event_target_element.closest( '.mini_cart_item a.remove,.product-remove a.remove' ) ) { const click_el = event_target_element; - + const productdata_el = click_el && click_el.closest( '.mini_cart_item a.remove,.product-remove a.remove' ); if ( !productdata_el ) { return true; @@ -335,7 +335,7 @@ function gtm4wp_woocommerce_process_pages() { qty_element = mini_cart_item_el && mini_cart_item_el.querySelectorAll( '.quantity' ); if ( qty_element && ( qty_element.length > 0 ) ) { qty = parseInt( qty_element[0].textContent ); - + if ( Number.isNaN( qty ) ) { qty = 0; } @@ -343,19 +343,19 @@ function gtm4wp_woocommerce_process_pages() { } else { qty = qty_element[0].value; } - + if ( 0 === qty ) { return true; } productdata.quantity = qty; - + gtm4wp_push_ecommerce( 'remove_from_cart', [ productdata ], { 'currency': gtm4wp_currency, 'value': productdata.price * productdata.quantity }); } - + // track clicks in product lists if ( event_target_element.closest( '.products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),' @@ -368,7 +368,7 @@ function gtm4wp_woocommerce_process_pages() { if ( 'undefined' == typeof google_tag_manager ) { return true; } - + const event_target_element = e.target; const matching_link_element = event_target_element.closest( '.products li:not(.product-category) a:not(.add_to_cart_button):not(.quick-view-button),' @@ -377,32 +377,32 @@ function gtm4wp_woocommerce_process_pages() { +'.widget-product-item,' +'.woocommerce-grouped-product-list-item__label a' ); - + if ( !matching_link_element ) { return true; } - + let temp_selector = event_target_element.closest( '.product,.wc-block-grid__product' ); let productdata_el; - + if ( temp_selector ) { productdata_el = temp_selector.querySelector( '.gtm4wp_productdata' ); - + } else { temp_selector = event_target_element.closest( '.products li' ); - + if ( temp_selector ) { productdata_el = temp_selector.querySelector( '.gtm4wp_productdata' ); - + } else { temp_selector = event_target_element.closest( '.products>div' ); - + if ( temp_selector ) { productdata_el = temp_selector.querySelector( '.gtm4wp_productdata' ); - + } else { temp_selector = event_target_element.closest( '.woocommerce-grouped-product-list-item__label' ); - + if ( temp_selector ) { productdata_el = temp_selector.querySelector( '.gtm4wp_productdata' ); } else { @@ -411,17 +411,17 @@ function gtm4wp_woocommerce_process_pages() { } } } - + const productdata = gtm4wp_read_json_from_node( productdata_el, 'gtm4wp_product_data', ['internal_id'] ); if ( !productdata ) { return true; } - + // only act on links pointing to the product detail page if ( productdata.productlink != matching_link_element.getAttribute( 'href' ) ) { return true; } - + // Look at first GTM container ID in case there are multiple GTM containers live on the page // since eventCallback is called on every container and we only need this executed once in this case. for (let i in window.google_tag_manager) { @@ -430,22 +430,22 @@ function gtm4wp_woocommerce_process_pages() { break; } } - + // do not do anything if GTM was not loaded // and window.google_tag_manager is for some reason initialized (GA4 only setup?) if ( "" === window.gtm4wp_first_container_id ) { return true; } - + const ctrl_key_pressed = e.ctrlKey || e.metaKey; const target_new_tab = ( '_blank' === matching_link_element.target ); - + // save this info to prevent redirection if another plugin already prevented to event for some reason let event_already_prevented = e.defaultPrevented; if ( !event_already_prevented ) { e.preventDefault(); } - + if ( ctrl_key_pressed || target_new_tab ) { // we need to open the new tab/page here so that popup blocker of the browser doesn't block our code window.productpage_window = window.open( 'about:blank', '_blank' ); @@ -453,7 +453,7 @@ function gtm4wp_woocommerce_process_pages() { const productlink_to_redirect = productdata.productlink; delete productdata.productlink; - + // fire ga4 version gtm4wp_push_ecommerce( 'select_item', [ productdata ], { 'currency': gtm4wp_currency