diff --git a/admin/admin.php b/admin/admin.php index 3a3ae31c..6342e934 100644 --- a/admin/admin.php +++ b/admin/admin.php @@ -1006,16 +1006,19 @@ function gtm4wp_show_admin_page() { /** * Hook function for admin_menu. Adds the plugin options page into the Settings menu of the WordPress admin. + * The capability can be changed with the gtm4wp_admin_page_capability filter. * * @see https://developer.wordpress.org/reference/hooks/admin_menu/ * * @return void */ function gtm4wp_add_admin_page() { + $capability = apply_filters( 'gtm4wp_admin_page_capability', 'manage_options' ); + add_options_page( esc_html__( 'Google Tag Manager for WordPress settings', 'duracelltomi-google-tag-manager' ), esc_html__( 'Google Tag Manager', 'duracelltomi-google-tag-manager' ), - 'manage_options', + $capability, GTM4WP_ADMINSLUG, 'gtm4wp_show_admin_page' ); diff --git a/integration/woocommerce.php b/integration/woocommerce.php index bd9954c0..aa36ea3f 100755 --- a/integration/woocommerce.php +++ b/integration/woocommerce.php @@ -214,7 +214,7 @@ function gtm4wp_process_product( $product, $additional_product_attributes, $attr 'name' => $product->get_title(), 'sku' => $product_sku ? $product_sku : $product_id, 'category' => $product_cat, - 'price' => round( (float) wc_get_price_to_display( $product ), 2 ), + 'price' => round( (float) wc_get_price_to_display( $product ), 2 ), // Unfortunately this does not force a .00 postfix for integers. 'stocklevel' => $product->get_stock_quantity(), ); diff --git a/js/gtm4wp-woocommerce-enhanced.js b/js/gtm4wp-woocommerce-enhanced.js index 4d28af37..aaad6ede 100644 --- a/js/gtm4wp-woocommerce-enhanced.js +++ b/js/gtm4wp-woocommerce-enhanced.js @@ -518,9 +518,10 @@ function gtm4wp_process_woocommerce_pages() { }); // fire ga4 version + const variant_value = gtm4wp_last_selected_product_variation.price * gtm4wp_last_selected_product_variation.quantity; 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 + 'value': variant_value.toFixed(2) }); } } else if ( product_is_grouped ) { @@ -541,10 +542,15 @@ function gtm4wp_process_woocommerce_pages() { return true; } + let group_product_price = parseFloat(dom_productdata.getAttribute( 'data-gtm4wp_product_price' )); + if ( isNaN(group_product_price) ) { + group_product_price = 0; + } + const product_data = { 'id': gtm4wp_use_sku_instead ? dom_productdata.getAttribute( 'data-gtm4wp_product_sku' ) : dom_productdata.getAttribute( 'data-gtm4wp_product_id' ), 'name': dom_productdata.getAttribute( 'data-gtm4wp_product_name' ), - 'price': dom_productdata.getAttribute( 'data-gtm4wp_product_price' ), + 'price': group_product_price.toFixed(2), 'category': dom_productdata.getAttribute( 'data-gtm4wp_product_cat' ), 'quantity': product_qty, 'stocklevel': dom_productdata.getAttribute( 'data-gtm4wp_product_stocklevel' ), @@ -574,14 +580,23 @@ function gtm4wp_process_woocommerce_pages() { // fire ga4 version gtm4wp_push_ecommerce( 'add_to_cart', ga4_products, { 'currency': gtm4wp_currency, - 'value': sum_value + 'value': sum_value.toFixed(2) }); } else { const product_id_el = gtm4wp_use_sku_instead ? product_form.querySelector( '[name=gtm4wp_sku]' ) : product_form.querySelector( '[name=gtm4wp_id]' ); + + let product_price = 0; + if ( product_form.querySelector( '[name=gtm4wp_price]' ) ) { + product_price = parseFloat(product_form.querySelector( '[name=gtm4wp_price]' ).value); + if ( isNaN(product_price) ) { + product_price = 0; + } + } + const product_data = { 'id': product_id_el && product_id_el.value, 'name': product_form.querySelector( '[name=gtm4wp_name]' ) && product_form.querySelector( '[name=gtm4wp_name]' ).value, - 'price': product_form.querySelector( '[name=gtm4wp_price]' ) && product_form.querySelector( '[name=gtm4wp_price]' ).value, + 'price': product_price.toFixed(2), 'category': product_form.querySelector( '[name=gtm4wp_category]' ) && product_form.querySelector( '[name=gtm4wp_category]' ).value, 'quantity': product_form.querySelector( '[name=quantity]' ) && product_form.querySelector( '[name=quantity]' ).value, 'stocklevel': product_form.querySelector( '[name=gtm4wp_stocklevel]' ) && product_form.querySelector( '[name=gtm4wp_stocklevel]' ).value, @@ -836,7 +851,13 @@ function gtm4wp_process_woocommerce_pages() { if ( gtm4wp_use_sku_instead && product_variation.sku && ('' !== product_variation.sku) ) { current_product_detail_data.id = product_variation.sku; } - current_product_detail_data.price = product_variation.display_price; + const variant_price = parsefloat( product_variation.display_price ); + if ( isNaN( variant_price ) ) { + current_product_detail_data.price = 0; + } else { + current_product_detail_data.price = variant_price.toFixed(2); + } + let product_variation_attribute_values = []; for( let attrib_key in product_variation.attributes ) { diff --git a/readme.txt b/readme.txt index 6ee5bc59..5daf96b6 100644 --- a/readme.txt +++ b/readme.txt @@ -250,6 +250,8 @@ Instead of the above events, you can use the new GA4 ecommerce events (add_to_ca In the Universal Analytics GTM tag now you have an option to use the GA4 ecommerce data in the data layer. If you are on GA360 and still collecting ecommerce data, you need to update your GTM setup to use that option firing on the GA4 ecommerce events. +* Updated: use the gtm4wp_admin_page_capability filter to modify the capability needed to use the admin page (thanks [emreerkan](https://github.com/emreerkan)) +* Fixed: price did not include the .00 postfix for integers in the add_to_cart event * Added: if needed, you can turn on clearing the ecommerce object before a new GA4 event is being pushed. = 1.18.1 =