diff --git a/assets/js/src/admin-ga-settings.js b/assets/js/src/admin-ga-settings.js
deleted file mode 100644
index 41fc7d55..00000000
--- a/assets/js/src/admin-ga-settings.js
+++ /dev/null
@@ -1,44 +0,0 @@
-jQuery( document ).ready( function ( $ ) {
- const ecCheckbox = $(
- '#woocommerce_google_analytics_ga_enhanced_ecommerce_tracking_enabled'
- );
- const uaCheckbox = $(
- '#woocommerce_google_analytics_ga_use_universal_analytics'
- );
- const gtagCheckbox = $( '#woocommerce_google_analytics_ga_gtag_enabled' );
-
- updateToggles();
-
- ecCheckbox.change( updateToggles );
- uaCheckbox.change( updateToggles );
- gtagCheckbox.change( updateToggles );
-
- function updateToggles() {
- const isEnhancedEcommerce = ecCheckbox.is( ':checked' );
- const isUniversalAnalytics = uaCheckbox.is( ':checked' );
- const isGtag = gtagCheckbox.is( ':checked' );
-
- // Legacy: gtag NO
- toggleCheckboxRow( $( '.legacy-setting' ), ! isGtag );
-
- // Enhanced settings: Enhanced YES + universal YES or gtag YES
- toggleCheckboxRow(
- $( '.enhanced-setting' ),
- isEnhancedEcommerce && ( isUniversalAnalytics || isGtag )
- );
-
- // Enhanced toggle: universal YES or gtag YES
- toggleCheckboxRow( ecCheckbox, isUniversalAnalytics || isGtag );
-
- // Universal toggle: gtag NO
- toggleCheckboxRow( uaCheckbox, ! isGtag );
- }
-
- function toggleCheckboxRow( checkbox, isVisible ) {
- if ( isVisible ) {
- checkbox.closest( 'tr' ).show();
- } else {
- checkbox.closest( 'tr' ).hide();
- }
- }
-} );
diff --git a/includes/class-wc-abstract-google-analytics-js.php b/includes/class-wc-abstract-google-analytics-js.php
index b8472bb4..00aae30f 100644
--- a/includes/class-wc-abstract-google-analytics-js.php
+++ b/includes/class-wc-abstract-google-analytics-js.php
@@ -93,11 +93,7 @@ abstract public static function load_analytics( $order = false );
* @param WC_Order $order WC_Order Object
*/
public function add_transaction( $order ) {
- if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) || 'yes' === self::get( 'ga_gtag_enabled' ) ) {
- wc_enqueue_js( static::add_transaction_enhanced( $order ) );
- } else {
- wc_enqueue_js( self::add_transaction_universal( $order ) );
- }
+ wc_enqueue_js( static::add_transaction_enhanced( $order ) );
}
/**
@@ -128,55 +124,6 @@ public static function get_product_identifier( $product ) {
return apply_filters( 'woocommerce_ga_product_identifier', $identifier, $product );
}
- /**
- * Generate Universal Analytics add item tracking code
- *
- * @param WC_Order $order WC_Order Object
- * @param WC_Order_Item $item The item to add to a transaction/order
- * @return string
- */
- protected function add_item_universal( $order, $item ) {
- $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
-
- $code = "ga('ecommerce:addItem', {";
- $code .= "'id': '" . esc_js( $order->get_order_number() ) . "',";
- $code .= "'name': '" . esc_js( $item['name'] ) . "',";
- $code .= "'sku': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
- $code .= "'category': " . self::product_get_category_line( $_product );
- $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
- $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
- $code .= '});';
-
- return $code;
- }
-
- /**
- * Generate Universal Analytics transaction tracking code
- *
- * @param WC_Order $order WC_Order object
- * @return string Add Transaction tracking code
- */
- protected function add_transaction_universal( $order ) {
- $code = "ga('ecommerce:addTransaction', {
- 'id': '" . esc_js( $order->get_order_number() ) . "', // Transaction ID. Required
- 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "', // Affiliation or store name
- 'revenue': '" . esc_js( $order->get_total() ) . "', // Grand Total
- 'shipping': '" . esc_js( $order->get_total_shipping() ) . "', // Shipping
- 'tax': '" . esc_js( $order->get_total_tax() ) . "', // Tax
- 'currency': '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' // Currency
- });";
-
- // Order items
- if ( $order->get_items() ) {
- foreach ( $order->get_items() as $item ) {
- $code .= self::add_item_universal( $order, $item );
- }
- }
-
- $code .= "ga('ecommerce:send');";
- return $code;
- }
-
/**
* Returns a 'category' JSON line based on $product
*
@@ -226,23 +173,11 @@ abstract public function remove_from_cart();
/**
* Enqueue JavaScript to track a product detail view
- *
- * @param WC_Product $product
*/
- abstract public function product_detail( $product );
+ abstract public function product_detail();
/**
* Enqueue JS to track when the checkout process is started
- *
- * @param array $cart items/contents of the cart
- */
- abstract public function checkout_process( $cart );
-
- /**
- * Enqueue JavaScript for Add to cart tracking
- *
- * @param array $parameters associative array of _trackEvent parameters
- * @param string $selector jQuery selector for binding click event
*/
- abstract public function event_tracking_code( $parameters, $selector );
+ abstract public function checkout_process();
}
diff --git a/includes/class-wc-google-analytics-js.php b/includes/class-wc-google-analytics-js.php
deleted file mode 100644
index 5012bdb6..00000000
--- a/includes/class-wc-google-analytics-js.php
+++ /dev/null
@@ -1,543 +0,0 @@
-get_order_currency() : $order->get_currency() ) . "']";
- }
-
- $code .= ');';
-
- self::load_analytics_code_in_header( apply_filters( 'woocommerce_ga_classic_snippet_output', $code ) );
- }
-
- /**
- * Enqueues JavaScript to build the addImpression object
- *
- * @param WC_Product $product
- */
- public static function listing_impression( $product ) {
- if ( is_search() ) {
- $list = 'Search Results';
- } else {
- $list = 'Product List';
- }
-
- wc_enqueue_js(
- self::tracker_var() . "( 'ec:addImpression', {
- 'id': '" . esc_js( $product->get_id() ) . "',
- 'name': '" . esc_js( $product->get_title() ) . "',
- 'category': " . self::product_get_category_line( $product ) . "
- 'list': '" . esc_js( $list ) . "'
- } );
- "
- );
- }
-
- /**
- * Enqueues JavaScript to build an addProduct and click object
- *
- * @param WC_Product $product
- */
- public static function listing_click( $product ) {
- if ( is_search() ) {
- $list = 'Search Results';
- } else {
- $list = 'Product List';
- }
-
- wc_enqueue_js(
- "
- $( '.product.post-" . esc_js( $product->get_id() ) . ' a , .product.post-' . esc_js( $product->get_id() ) . " button' ).on('click', function() {
- if ( false === $(this).hasClass( 'product_type_variable' ) && false === $(this).hasClass( 'product_type_grouped' ) ) {
- " . self::tracker_var() . "( 'ec:addProduct', {
- 'id': '" . esc_js( $product->get_id() ) . "',
- 'name': '" . esc_js( $product->get_title() ) . "',
- 'category': " . self::product_get_category_line( $product ) . '
- });
- }
- ' . self::tracker_var() . "( 'ec:setAction', 'click', { list: '" . esc_js( $list ) . "' });
- " . self::tracker_var() . "( 'send', 'event', 'UX', 'click', ' " . esc_js( $list ) . "' );
- });
- "
- );
- }
-
- /**
- * Loads in the footer
- *
- * @see wp_footer
- */
- public static function classic_analytics_footer() {
- if ( 'yes' === self::get( 'ga_support_display_advertising' ) ) {
- $ga_url = "('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js'";
- } else {
- $ga_url = "('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'";
- }
-
- $code = "(function() {
- var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
- ga.src = " . $ga_url . ";
- var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
- })();";
-
- wc_enqueue_js( $code );
- }
-
- /**
- * Enqueues JavaScript to send the pageview last thing (needed for things like addImpression)
- */
- public static function load_page_view_footer() {
- if ( apply_filters( 'wc_google_analytics_send_pageview', true ) ) {
- wc_enqueue_js( self::tracker_var() . "( 'send', 'pageview' ); " );
- }
- }
-
- /**
- * This was created to fix public facing api typo in a filter name
- * and inform about the deprecation.
- *
- * @param boolean $send_pageview
- */
- public static function universal_analytics_footer_filter( $send_pageview ) {
- return apply_filters_deprecated( 'wc_goole_analytics_send_pageview', array( $send_pageview ), '1.4.20', 'wc_google_analytics_send_pageview' );
- }
-
- /**
- * Loads the universal analytics code
- *
- * @param string $logged_in 'yes' if the user is logged in, no if not (this is a string so we can pass it to GA)
- */
- protected static function load_analytics_universal( $logged_in ) {
- $domainname = self::get( 'ga_set_domain_name' );
-
- if ( ! empty( $domainname ) ) {
- $set_domain_name = esc_js( self::get( 'ga_set_domain_name' ) );
- } else {
- $set_domain_name = 'auto';
- }
-
- $support_display_advertising = '';
- if ( 'yes' === self::get( 'ga_support_display_advertising' ) ) {
- $support_display_advertising = self::tracker_var() . "( 'require', 'displayfeatures' );";
- }
-
- $support_enhanced_link_attribution = '';
- if ( 'yes' === self::get( 'ga_support_enhanced_link_attribution' ) ) {
- $support_enhanced_link_attribution = self::tracker_var() . "( 'require', 'linkid' );";
- }
-
- $anonymize_enabled = '';
- if ( 'yes' === self::get( 'ga_anonymize_enabled' ) ) {
- $anonymize_enabled = self::tracker_var() . "( 'set', 'anonymizeIp', true );";
- }
-
- $track_404_enabled = '';
- if ( 'yes' === self::get( 'ga_404_tracking_enabled' ) && is_404() ) {
- // See https://developers.google.com/analytics/devguides/collection/analyticsjs/events for reference
- $track_404_enabled = self::tracker_var() . "( 'send', 'event', 'Error', '404 Not Found', 'page: ' + document.location.pathname + document.location.search + ' referrer: ' + document.referrer );";
- }
-
- $src = apply_filters( 'woocommerce_google_analytics_script_src', '//www.google-analytics.com/analytics.js' );
-
- $ga_snippet_head = "(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
- (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
- m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
- })(window,document,'script', '{$src}','" . self::tracker_var() . "');";
-
- $ga_id = self::get( 'ga_id' );
-
- if ( 'yes' === self::get( 'ga_linker_allow_incoming_enabled' ) ) {
- $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "', { allowLinker: true });";
- } else {
- $ga_snippet_create = self::tracker_var() . "( 'create', '" . esc_js( $ga_id ) . "', '" . $set_domain_name . "' );";
- }
-
- if ( ! empty( self::DEVELOPER_ID ) ) {
- $ga_snippet_developer_id = "(window.gaDevIds=window.gaDevIds||[]).push('" . self::DEVELOPER_ID . "');";
- } else {
- $ga_snippet_developer_id = '';
- }
-
- $ga_snippet_require = $support_display_advertising .
- $support_enhanced_link_attribution .
- $anonymize_enabled .
- $track_404_enabled . '
- ' . self::tracker_var() . "( 'set', 'dimension1', '" . $logged_in . "' );\n";
-
- if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
- $ga_snippet_require .= self::tracker_var() . "( 'require', 'ec' );";
- } else {
- $ga_snippet_require .= self::tracker_var() . "( 'require', 'ecommerce', 'ecommerce.js');";
- }
-
- $ga_cross_domains = ! empty( self::get( 'ga_linker_cross_domains' ) ) ? array_map( 'esc_js', explode( ',', self::get( 'ga_linker_cross_domains' ) ) ) : false;
-
- if ( $ga_cross_domains ) {
- $ga_snippet_require .= self::tracker_var() . "( 'require', 'linker' );";
- $ga_snippet_require .= self::tracker_var() . "( 'linker:autoLink', " . wp_json_encode( $ga_cross_domains ) . ');';
- }
-
- $ga_snippet_head = apply_filters( 'woocommerce_ga_snippet_head', $ga_snippet_head );
- $ga_snippet_create = apply_filters( 'woocommerce_ga_snippet_create', $ga_snippet_create, $ga_id );
- $ga_snippet_developer_id = apply_filters( 'woocommerce_ga_snippet_developer_id', $ga_snippet_developer_id );
- $ga_snippet_require = apply_filters( 'woocommerce_ga_snippet_require', $ga_snippet_require );
-
- $code = $ga_snippet_head . $ga_snippet_create . $ga_snippet_developer_id . $ga_snippet_require;
-
- self::load_analytics_code_in_header( apply_filters( 'woocommerce_ga_snippet_output', $code ) );
- }
-
- /**
- * Generate code used to pass transaction data to Google Analytics.
- *
- * @param WC_Order $order WC_Order Object.
- */
- public function add_transaction( $order ) {
- if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
- if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
- $transaction_code = self::add_transaction_enhanced( $order );
- } else {
- $transaction_code = self::add_transaction_universal( $order );
- }
- } else {
- $transaction_code = self::add_transaction_classic( $order );
- }
-
- // Check localStorage to avoid duplicate transactions if page is reloaded without hitting server.
- $code = "
- var ga_orders = [];
- try {
- ga_orders = localStorage.getItem( 'ga_orders' );
- ga_orders = ga_orders ? JSON.parse( ga_orders ) : [];
- } catch {}
- if ( -1 === ga_orders.indexOf( '" . esc_js( $order->get_order_number() ) . "' ) ) {
- " . $transaction_code . "
- try {
- ga_orders.push( '" . esc_js( $order->get_order_number() ) . "' );
- localStorage.setItem( 'ga_orders', JSON.stringify( ga_orders ) );
- } catch {}
- }";
-
- wc_enqueue_js( $code );
- }
-
- /**
- * Transaction tracking for ga.js (classic)
- *
- * @param WC_Order $order WC_Order Object
- * @return string Add Transaction Code
- */
- protected function add_transaction_classic( $order ) {
- $code = "_gaq.push(['_addTrans',
- '" . esc_js( $order->get_order_number() ) . "', // order ID - required
- '" . esc_js( get_bloginfo( 'name' ) ) . "', // affiliation or store name
- '" . esc_js( $order->get_total() ) . "', // total - required
- '" . esc_js( $order->get_total_tax() ) . "', // tax
- '" . esc_js( $order->get_total_shipping() ) . "', // shipping
- '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->billing_city : $order->get_billing_city() ) . "', // city
- '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->billing_state : $order->get_billing_state() ) . "', // state or province
- '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->billing_country : $order->get_billing_country() ) . "' // country
- ]);";
-
- // Order items
- if ( $order->get_items() ) {
- foreach ( $order->get_items() as $item ) {
- $code .= self::add_item_classic( $order, $item );
- }
- }
-
- $code .= "_gaq.push(['_trackTrans']);";
- return $code;
- }
-
- /**
- * Generate Universal Analytics Enhanced Ecommerce transaction tracking code
- *
- * @param WC_Order $order
- * @return string
- */
- protected function add_transaction_enhanced( $order ) {
- $code = self::tracker_var() . "( 'set', '&cu', '" . esc_js( version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_order_currency() : $order->get_currency() ) . "' );";
-
- // Order items
- if ( $order->get_items() ) {
- foreach ( $order->get_items() as $item ) {
- $code .= self::add_item_enhanced( $order, $item );
- }
- }
-
- $code .= self::tracker_var() . "( 'ec:setAction', 'purchase', {
- 'id': '" . esc_js( $order->get_order_number() ) . "',
- 'affiliation': '" . esc_js( get_bloginfo( 'name' ) ) . "',
- 'revenue': '" . esc_js( $order->get_total() ) . "',
- 'tax': '" . esc_js( $order->get_total_tax() ) . "',
- 'shipping': '" . esc_js( $order->get_total_shipping() ) . "'
- } );";
-
- return $code;
- }
-
- /**
- * Add Item (Classic)
- *
- * @param WC_Order $order WC_Order Object
- * @param array $item The item to add to a transaction/order
- * @return string
- */
- protected function add_item_classic( $order, $item ) {
- $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
-
- $code = "_gaq.push(['_addItem',";
- $code .= "'" . esc_js( $order->get_order_number() ) . "',";
- $code .= "'" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
- $code .= "'" . esc_js( $item['name'] ) . "',";
- $code .= self::product_get_category_line( $_product );
- $code .= "'" . esc_js( $order->get_item_total( $item ) ) . "',";
- $code .= "'" . esc_js( $item['qty'] ) . "'";
- $code .= ']);';
-
- return $code;
- }
-
- /**
- * Add Item (Enhanced, Universal)
- *
- * @param WC_Order $order WC_Order Object
- * @param WC_Order_Item $item The item to add to a transaction/order
- * @return string
- */
- protected function add_item_enhanced( $order, $item ) {
- $_product = version_compare( WC_VERSION, '3.0', '<' ) ? $order->get_product_from_item( $item ) : $item->get_product();
- $variant = self::product_get_variant_line( $_product );
-
- $code = self::tracker_var() . "( 'ec:addProduct', {";
- $code .= "'id': '" . esc_js( $_product->get_sku() ? $_product->get_sku() : $_product->get_id() ) . "',";
- $code .= "'name': '" . esc_js( $item['name'] ) . "',";
- $code .= "'category': " . self::product_get_category_line( $_product );
-
- if ( '' !== $variant ) {
- $code .= "'variant': " . $variant;
- }
-
- $code .= "'price': '" . esc_js( $order->get_item_total( $item ) ) . "',";
- $code .= "'quantity': '" . esc_js( $item['qty'] ) . "'";
- $code .= '});';
-
- return $code;
- }
-
- /**
- * Output JavaScript to track an enhanced ecommerce remove from cart action
- */
- public function remove_from_cart() {
- echo( "
-
- " );
- }
-
- /**
- * Enqueue JavaScript to track a product detail view
- *
- * @param WC_Product $product
- */
- public function product_detail( $product ) {
- if ( empty( $product ) ) {
- return;
- }
-
- wc_enqueue_js(
- self::tracker_var() . "( 'ec:addProduct', {
- 'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',
- 'name': '" . esc_js( $product->get_title() ) . "',
- 'category': " . self::product_get_category_line( $product ) . "
- 'price': '" . esc_js( $product->get_price() ) . "',
- } );
-
- " . self::tracker_var() . "( 'ec:setAction', 'detail' );"
- );
- }
-
- /**
- * Enqueue JS to track when the checkout process is started
- *
- * @param array $cart items/contents of the cart
- */
- public function checkout_process( $cart ) {
- $code = '';
-
- foreach ( $cart as $cart_item_key => $cart_item ) {
- $product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
- $variant = self::product_get_variant_line( $product );
- $code .= self::tracker_var() . "( 'ec:addProduct', {
- 'id': '" . esc_js( $product->get_sku() ? $product->get_sku() : ( '#' . $product->get_id() ) ) . "',
- 'name': '" . esc_js( $product->get_title() ) . "',
- 'category': " . self::product_get_category_line( $product );
-
- if ( '' !== $variant ) {
- $code .= "'variant': " . $variant;
- }
-
- $code .= "'price': '" . esc_js( $product->get_price() ) . "',
- 'quantity': '" . esc_js( $cart_item['quantity'] ) . "'
- } );";
- }
-
- $code .= self::tracker_var() . "( 'ec:setAction','checkout' );";
- wc_enqueue_js( $code );
- }
-
- /**
- * Enqueue JavaScript for Add to cart tracking
- *
- * @param array $parameters associative array of _trackEvent parameters
- * @param string $selector jQuery selector for binding click event
- */
- public function event_tracking_code( $parameters, $selector ) {
- $parameters = apply_filters( 'woocommerce_ga_event_tracking_parameters', $parameters );
-
- if ( 'yes' === self::get( 'ga_use_universal_analytics' ) ) {
- if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
- wc_enqueue_js(
- "
- $( '" . $selector . "' ).on( 'click', function() {
- " . $parameters['enhanced'] . '
- ' . self::tracker_var() . "( 'ec:setAction', 'add' );
- " . self::tracker_var() . "( 'send', 'event', 'UX', 'click', 'add to cart' );
- });
- "
- );
- return;
- } else {
- $track_event = self::tracker_var() . "('send', 'event', %s, %s, %s);";
- }
- } else {
- $track_event = "_gaq.push(['_trackEvent', %s, %s, %s]);";
- }
-
- wc_enqueue_js(
- "
- $( '" . $selector . "' ).on( 'click', function() {
- " . sprintf( $track_event, $parameters['category'], $parameters['action'], $parameters['label'] ) . '
- });
- '
- );
- }
-
- /**
- * Loads a code using the google-analytics handler in the head.
- *
- * @param string $code The code to add attached to the google-analytics handler
- */
- protected static function load_analytics_code_in_header( $code ) {
- wp_register_script( 'google-analytics', '', array( 'google-analytics-opt-out' ), null, false );
- wp_add_inline_script( 'google-analytics', $code );
- wp_enqueue_script( 'google-analytics' );
- }
-
-}
diff --git a/includes/class-wc-google-analytics.php b/includes/class-wc-google-analytics.php
index 8d746732..72965e3b 100644
--- a/includes/class-wc-google-analytics.php
+++ b/includes/class-wc-google-analytics.php
@@ -4,7 +4,6 @@
exit; // Exit if accessed directly
}
-use WC_Google_Analytics_Integration as Plugin;
use Automattic\WooCommerce\Admin\Features\OnboardingTasks\TaskLists;
/**
@@ -20,15 +19,6 @@ class WC_Google_Analytics extends WC_Integration {
/** @var string $ga_id Google Analytics Tracking ID */
public $ga_id;
- /** @var string $ga_set_domain_name Domain Name (legacy setting) */
- public $ga_set_domain_name;
-
- /** @var string $ga_use_universal_analytics Use Legacy Universal Analytics (yes|no) */
- public $ga_use_universal_analytics;
-
- /** @var string $ga_gtag_enable Is GA4 enabled (yes|no) */
- public $ga_gtag_enabled;
-
/** @var string $ga_standard_tracking_enabled Is standard tracking enabled (yes|no) */
public $ga_standard_tracking_enabled;
@@ -47,9 +37,6 @@ class WC_Google_Analytics extends WC_Integration {
/** @var string $ga_ecommerce_tracking_enabled Purchase transactions (yes|no) */
public $ga_ecommerce_tracking_enabled;
- /** @var string $ga_enhanced_ecommerce_tracking_enabled Enhanced ecommerce tracking (yes|no) */
- public $ga_enhanced_ecommerce_tracking_enabled;
-
/** @var string $ga_enhanced_remove_from_cart_enabled Track remove from cart events (yes|no) */
public $ga_enhanced_remove_from_cart_enabled;
@@ -86,11 +73,7 @@ class WC_Google_Analytics extends WC_Integration {
* @return WC_Abstract_Google_Analytics_JS
*/
protected function get_tracking_instance( $options = array() ) {
- if ( 'yes' === $this->ga_gtag_enabled ) {
- return WC_Google_Gtag_JS::get_instance( $options );
- }
-
- return WC_Google_Analytics_JS::get_instance( $options );
+ return WC_Google_Gtag_JS::get_instance( $options );
}
/**
@@ -109,7 +92,6 @@ public function __construct() {
// Contains snippets/JS tracking code
include_once 'class-wc-abstract-google-analytics-js.php';
- include_once 'class-wc-google-analytics-js.php';
include_once 'class-wc-google-gtag-js.php';
$this->get_tracking_instance( $constructor );
@@ -119,23 +101,12 @@ public function __construct() {
add_filter( 'woocommerce_tracker_data', array( $this, 'track_options' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_update_options_integration_google_analytics', array( $this, 'show_options_info' ) );
- add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_assets' ) );
add_action( 'admin_init', array( $this, 'privacy_policy' ) );
// Tracking code
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_code' ), 9 );
add_filter( 'script_loader_tag', array( $this, 'async_script_loader_tags' ), 10, 3 );
- // Event tracking code
- add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
- add_action( 'wp_footer', array( $this, 'loop_add_to_cart' ) );
- add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
- add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
- add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
- add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'track_product' ), 10, 2 );
- add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) );
- add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
-
// utm_nooverride parameter for Google AdWords
add_filter( 'woocommerce_get_return_url', array( $this, 'utm_nooverride' ) );
}
@@ -149,16 +120,11 @@ public function init_options() {
$options = array(
'ga_product_identifier' => 'product_sku',
'ga_id' => null,
- 'ga_set_domain_name' => null,
- 'ga_gtag_enabled' => null,
'ga_standard_tracking_enabled' => null,
'ga_support_display_advertising' => null,
'ga_support_enhanced_link_attribution' => null,
- 'ga_use_universal_analytics' => null,
'ga_anonymize_enabled' => null,
'ga_404_tracking_enabled' => null,
- 'ga_ecommerce_tracking_enabled' => null,
- 'ga_enhanced_ecommerce_tracking_enabled' => null,
'ga_enhanced_remove_from_cart_enabled' => null,
'ga_enhanced_product_impression_enabled' => null,
'ga_enhanced_product_click_enabled' => null,
@@ -181,14 +147,6 @@ public function init_options() {
* Tells WooCommerce which settings to display under the "integration" tab
*/
public function init_form_fields() {
- // backwards_compatibility
- if ( get_option( 'woocommerce_ga_use_universal_analytics' ) ) {
- $ua_default_value = get_option( 'woocommerce_ga_use_universal_analytics' );
- } else {
- // don't enable for extension updates, only default to enabled on new installs
- $ua_default_value = get_option( $this->get_option_key() ) ? 'no' : 'yes';
- }
-
$this->form_fields = array(
'ga_product_identifier' => array(
'title' => __( 'Product Identification', 'woocommerce-google-analytics-integration' ),
@@ -208,34 +166,6 @@ public function init_form_fields() {
'placeholder' => 'GT-XXXXX',
'default' => get_option( 'woocommerce_ga_id' ), // Backwards compat
),
- 'ga_set_domain_name' => array(
- 'title' => __( 'Set Domain Name', 'woocommerce-google-analytics-integration' ),
- /* translators: Read more link */
- 'description' => sprintf( __( '(Optional) Sets the _setDomainName
variable. %1$sSee here for more information%2$s.', 'woocommerce-google-analytics-integration' ), '', '' ),
- 'type' => 'text',
- 'default' => '',
- 'class' => 'legacy-setting',
- ),
-
- 'ga_gtag_enabled' => array(
- 'title' => __( 'Tracking Options', 'woocommerce-google-analytics-integration' ),
- 'label' => __( 'Use Global Site Tag', 'woocommerce-google-analytics-integration' ),
- /* translators: Read more link */
- 'description' => sprintf( __( 'The Global Site Tag provides streamlined tagging across Google’s site measurement, conversion tracking, and remarketing products. This must be enabled to use a Google Analytics 4 Measurement ID (e.g., G-XXXXX
or GT-XXXXX
). %1$sSee here for more information%2$s.', 'woocommerce-google-analytics-integration' ), '', '' ),
- 'type' => 'checkbox',
- 'checkboxgroup' => '',
- 'default' => get_option( $this->get_option_key() ) ? 'no' : 'yes', // don't enable on updates, only default on new installs
- ),
-
- 'ga_use_universal_analytics' => array(
- 'label' => __( 'Enable Universal Analytics', 'woocommerce-google-analytics-integration' ),
- /* translators: Read more start and end links */
- 'description' => sprintf( __( 'Uses Universal Analytics instead of Classic Google Analytics. If you have not previously used Google Analytics on this site, check this box. Otherwise, %1$sfollow step 1 of the Universal Analytics upgrade guide.%2$s Enabling this setting will take care of step 2. %3$sRead more about Universal Analytics%4$s. Universal Analytics or Global Site Tag must be enabled to enable enhanced eCommerce.', 'woocommerce-google-analytics-integration' ), '', '', '', '' ),
- 'type' => 'checkbox',
- 'checkboxgroup' => '',
- 'default' => $ua_default_value,
- 'class' => 'legacy-setting',
- ),
'ga_standard_tracking_enabled' => array(
'label' => __( 'Enable Standard Tracking', 'woocommerce-google-analytics-integration' ),
'description' => __( 'This tracks session data such as demographics, system, etc. You don\'t need to enable this if you are using a 3rd party Google analytics plugin.', 'woocommerce-google-analytics-integration' ),
@@ -275,7 +205,15 @@ public function init_form_fields() {
'checkboxgroup' => '',
'default' => 'yes',
),
+ 'ga_linker_allow_incoming_enabled' => array(
+ 'label' => __( 'Accept Incoming Linker Parameters', 'woocommerce-google-analytics-integration' ),
+ 'description' => __( 'Enabling this option will allow incoming linker parameters from other websites.', 'woocommerce-google-analytics-integration' ),
+ 'type' => 'checkbox',
+ 'checkboxgroup' => '',
+ 'default' => 'no',
+ ),
'ga_ecommerce_tracking_enabled' => array(
+ 'title' => __( 'Event Tracking', 'woocommerce-google-analytics-integration' ),
'label' => __( 'Purchase Transactions', 'woocommerce-google-analytics-integration' ),
'description' => __( 'This requires a payment gateway that redirects to the thank you/order received page after payment. Orders paid with gateways which do not do this will not be tracked.', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
@@ -288,40 +226,11 @@ public function init_form_fields() {
'checkboxgroup' => '',
'default' => 'yes',
),
- 'ga_linker_cross_domains' => array(
- 'title' => __( 'Cross Domain Tracking', 'woocommerce-google-analytics-integration' ),
- /* translators: Read more link */
- 'description' => sprintf( __( 'Add a comma separated list of domains for automatic linking. %1$sRead more about Cross Domain Measurement%2$s', 'woocommerce-google-analytics-integration' ), '', '' ),
- 'type' => 'text',
- 'placeholder' => 'example.com, example.net',
- 'default' => '',
- ),
- 'ga_linker_allow_incoming_enabled' => array(
- 'label' => __( 'Accept Incoming Linker Parameters', 'woocommerce-google-analytics-integration' ),
- 'description' => __( 'Enabling this option will allow incoming linker parameters from other websites.', 'woocommerce-google-analytics-integration' ),
- 'type' => 'checkbox',
- 'checkboxgroup' => '',
- 'default' => 'no',
- ),
- 'ga_enhanced_ecommerce_tracking_enabled' => array(
- 'title' => __( 'Enhanced eCommerce', 'woocommerce-google-analytics-integration' ),
- 'label' => __( 'Enable Enhanced eCommerce ', 'woocommerce-google-analytics-integration' ),
- /* translators: Read more link */
- 'description' => sprintf( __( 'Enhanced eCommerce allows you to measure more user interactions with your store, including: product impressions, product detail views, starting the checkout process, adding cart items, and removing cart items. Universal Analytics or Global Site Tag must be enabled for Enhanced eCommerce to work. If using Universal Analytics, turn on Enhanced eCommerce in your Google Analytics dashboard before enabling this setting. %1$sSee here for more information%2$s.', 'woocommerce-google-analytics-integration' ), '', '' ),
- 'type' => 'checkbox',
- 'checkboxgroup' => '',
- 'default' => 'no',
- 'class' => 'legacy-setting',
- ),
-
- // Enhanced eCommerce Sub-Settings
-
'ga_enhanced_remove_from_cart_enabled' => array(
'label' => __( 'Remove from Cart Events', 'woocommerce-google-analytics-integration' ),
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
- 'class' => 'enhanced-setting',
),
'ga_enhanced_product_impression_enabled' => array(
@@ -329,7 +238,6 @@ public function init_form_fields() {
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
- 'class' => 'enhanced-setting',
),
'ga_enhanced_product_click_enabled' => array(
@@ -337,7 +245,6 @@ public function init_form_fields() {
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
- 'class' => 'enhanced-setting',
),
'ga_enhanced_product_detail_view_enabled' => array(
@@ -345,7 +252,6 @@ public function init_form_fields() {
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
- 'class' => 'enhanced-setting',
),
'ga_enhanced_checkout_process_enabled' => array(
@@ -353,7 +259,14 @@ public function init_form_fields() {
'type' => 'checkbox',
'checkboxgroup' => '',
'default' => 'yes',
- 'class' => 'enhanced-setting',
+ ),
+ 'ga_linker_cross_domains' => array(
+ 'title' => __( 'Cross Domain Tracking', 'woocommerce-google-analytics-integration' ),
+ /* translators: Read more link */
+ 'description' => sprintf( __( 'Add a comma separated list of domains for automatic linking. %1$sRead more about Cross Domain Measurement%2$s', 'woocommerce-google-analytics-integration' ), '', '' ),
+ 'type' => 'text',
+ 'placeholder' => 'example.com, example.net',
+ 'default' => '',
),
);
}
@@ -380,20 +293,16 @@ public function show_options_info() {
*/
public function track_options( $data ) {
$data['wc-google-analytics'] = array(
- 'standard_tracking_enabled' => $this->ga_standard_tracking_enabled,
- 'support_display_advertising' => $this->ga_support_display_advertising,
- 'support_enhanced_link_attribution' => $this->ga_support_enhanced_link_attribution,
- 'use_universal_analytics' => $this->ga_use_universal_analytics,
- 'anonymize_enabled' => $this->ga_anonymize_enabled,
- 'ga_404_tracking_enabled' => $this->ga_404_tracking_enabled,
- 'ecommerce_tracking_enabled' => $this->ga_ecommerce_tracking_enabled,
- 'event_tracking_enabled' => $this->ga_event_tracking_enabled,
- 'gtag_enabled' => $this->ga_gtag_enabled,
- 'set_domain_name' => empty( $this->ga_set_domain_name ) ? 'no' : 'yes',
- 'plugin_version' => WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION,
- 'enhanced_ecommerce_tracking_enabled' => $this->ga_enhanced_ecommerce_tracking_enabled,
- 'linker_allow_incoming_enabled' => empty( $this->ga_linker_allow_incoming_enabled ) ? 'no' : 'yes',
- 'linker_cross_domains' => $this->ga_linker_cross_domains,
+ 'standard_tracking_enabled' => $this->ga_standard_tracking_enabled,
+ 'support_display_advertising' => $this->ga_support_display_advertising,
+ 'support_enhanced_link_attribution' => $this->ga_support_enhanced_link_attribution,
+ 'anonymize_enabled' => $this->ga_anonymize_enabled,
+ 'ga_404_tracking_enabled' => $this->ga_404_tracking_enabled,
+ 'ecommerce_tracking_enabled' => $this->ga_ecommerce_tracking_enabled,
+ 'event_tracking_enabled' => $this->ga_event_tracking_enabled,
+ 'plugin_version' => WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION,
+ 'linker_allow_incoming_enabled' => empty( $this->ga_linker_allow_incoming_enabled ) ? 'no' : 'yes',
+ 'linker_cross_domains' => $this->ga_linker_cross_domains,
);
// ID prefix, blank, or X for unknown
@@ -407,34 +316,6 @@ public function track_options( $data ) {
return $data;
}
- /**
- * Enqueue the admin JavaScript
- */
- public function load_admin_assets() {
- $screen = get_current_screen();
- if ( 'woocommerce_page_wc-settings' !== $screen->id ) {
- return;
- }
-
- // phpcs:disable WordPress.Security.NonceVerification.Recommended
- if ( empty( $_GET['tab'] ) ) {
- return;
- }
-
- // phpcs:disable WordPress.Security.NonceVerification.Recommended
- if ( 'integration' !== $_GET['tab'] ) {
- return;
- }
-
- wp_enqueue_script(
- 'wc-google-analytics-admin-enhanced-settings',
- Plugin::get_instance()->get_js_asset_url( 'admin-ga-settings.js' ),
- Plugin::get_instance()->get_js_asset_dependencies( 'admin-ga-settings', [ 'jquery' ] ),
- Plugin::get_instance()->get_js_asset_version( 'admin-ga-settings' ),
- true
- );
- }
-
/**
* Add suggested privacy policy content
*
@@ -469,7 +350,7 @@ public function enqueue_tracking_code() {
}
// Check if is order received page and stop when the products and not tracked
- if ( is_order_received_page() && 'yes' === $this->ga_ecommerce_tracking_enabled ) {
+ if ( is_order_received_page() ) {
$order_id = isset( $wp->query_vars['order-received'] ) ? $wp->query_vars['order-received'] : 0;
$order = wc_get_order( $order_id );
if ( $order && ! (bool) $order->get_meta( '_ga_tracked' ) ) {
@@ -536,193 +417,6 @@ private function disable_tracking( $type ) {
return is_admin() || current_user_can( 'manage_options' ) || ( ! $this->ga_id ) || 'no' === $type || apply_filters( 'woocommerce_ga_disable_tracking', false, $type );
}
- /**
- * Google Analytics event tracking for single product add to cart
- */
- public function add_to_cart() {
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) ) {
- return;
- }
- if ( ! is_single() ) {
- return;
- }
-
- global $product;
-
- if ( 'yes' === $this->ga_gtag_enabled ) {
- $this->get_tracking_instance()->add_to_cart( $product );
- return;
- }
-
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
- $parameters = array();
- $parameters['category'] = "'" . __( 'Products', 'woocommerce-google-analytics-integration' ) . "'";
- $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce-google-analytics-integration' ) . "'";
- $parameters['label'] = "'" . esc_js( $product->get_sku() ? __( 'ID:', 'woocommerce-google-analytics-integration' ) . ' ' . $product->get_sku() : '#' . $product->get_id() ) . "'";
-
- if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
-
- $item = '{';
-
- if ( $product->is_type( 'variable' ) ) {
- $item .= "'id': google_analytics_integration_product_data[ $('input[name=\"variation_id\"]').val() ] !== undefined ? google_analytics_integration_product_data[ $('input[name=\"variation_id\"]').val() ].id : false,";
- $item .= "'variant': google_analytics_integration_product_data[ $('input[name=\"variation_id\"]').val() ] !== undefined ? google_analytics_integration_product_data[ $('input[name=\"variation_id\"]').val() ].variant : false,";
- } else {
- $item .= "'id': '" . $this->get_tracking_instance()->get_product_identifier( $product ) . "',";
- }
-
- $item .= "'name': '" . esc_js( $product->get_title() ) . "',";
- $item .= "'category': " . $this->get_tracking_instance()->product_get_category_line( $product );
- $item .= "'quantity': $( 'input.qty' ).val() ? $( 'input.qty' ).val() : '1'";
- $item .= '}';
-
- $parameters['item'] = $item;
-
- $code = $this->get_tracking_instance()->tracker_var() . "( 'ec:addProduct', {$item} );";
- $parameters['enhanced'] = $code;
- }
-
- $this->get_tracking_instance()->event_tracking_code( $parameters, '.single_add_to_cart_button' );
- }
-
- /**
- * Enhanced Analytics event tracking for removing a product from the cart
- */
- public function remove_from_cart() {
- if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_remove_from_cart_enabled ) ) {
- return;
- }
-
- $this->get_tracking_instance()->remove_from_cart();
- }
-
- /**
- * Adds the product ID and SKU to the remove product link if not present
- *
- * @param string $url
- * @param string $key
- * @return string
- */
- public function remove_from_cart_attributes( $url, $key ) {
- if ( strpos( $url, 'data-product_id' ) !== false ) {
- return $url;
- }
-
- if ( ! is_object( WC()->cart ) ) {
- return $url;
- }
-
- $item = WC()->cart->get_cart_item( $key );
- $product = $item['data'];
-
- if ( ! is_object( $product ) ) {
- return $url;
- }
-
- $url = str_replace( 'href=', 'data-product_id="' . esc_attr( $product->get_id() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" href=', $url );
- return $url;
- }
-
- /**
- * Google Analytics event tracking for loop add to cart
- */
- public function loop_add_to_cart() {
- if ( $this->disable_tracking( $this->ga_event_tracking_enabled ) || 'yes' === $this->ga_gtag_enabled ) {
- return;
- }
-
- // Add single quotes to allow jQuery to be substituted into _trackEvent parameters
- $parameters = array();
- $parameters['category'] = "'" . __( 'Products', 'woocommerce-google-analytics-integration' ) . "'";
- $parameters['action'] = "'" . __( 'Add to Cart', 'woocommerce-google-analytics-integration' ) . "'";
- $parameters['label'] = "($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id'))"; // Product SKU or ID
-
- if ( ! $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
- $item = '{';
- $item .= "'id': ($(this).data('product_sku')) ? ($(this).data('product_sku')) : ('#' + $(this).data('product_id')),";
- $item .= "'quantity': $(this).data('quantity')";
- $item .= '}';
- $parameters['item'] = $item;
-
- $code = $this->get_tracking_instance()->tracker_var() . "( 'ec:addProduct', " . $item . ' );';
- $parameters['enhanced'] = $code;
- }
-
- $this->get_tracking_instance()->event_tracking_code( $parameters, '.add_to_cart_button:not(.product_type_variable, .product_type_grouped)' );
- }
-
- /**
- * Determine if the conditions are met for enhanced ecommerce interactions to be displayed.
- * Currently checks if Global Tags OR Universal Analytics are enabled, plus Enhanced eCommerce.
- *
- * @param array $extra_checks Any extra option values that should be 'yes' to proceed
- * @return bool Whether enhanced ecommerce transactions can be displayed.
- */
- protected function enhanced_ecommerce_enabled( $extra_checks = [] ) {
- if ( ! is_array( $extra_checks ) ) {
- $extra_checks = [ $extra_checks ];
- }
-
- // False if gtag and UA are disabled.
- if ( $this->disable_tracking( $this->ga_use_universal_analytics ) && $this->disable_tracking( $this->ga_gtag_enabled ) ) {
- return false;
- }
-
- // False if gtag or UA is enabled, but enhanced ecommerce is disabled.
- if ( $this->disable_tracking( $this->ga_enhanced_ecommerce_tracking_enabled ) ) {
- return false;
- }
-
- // False if any specified interaction-level checks are disabled.
- foreach ( $extra_checks as $option_value ) {
- if ( $this->disable_tracking( $option_value ) ) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
- * Measure a product click and impression from a Product list
- *
- * @param string $link The Add To Cart Link
- * @param WC_Product $product The Product
- */
- public function track_product( $link, $product ) {
- if ( $this->enhanced_ecommerce_enabled( $this->ga_enhanced_product_click_enabled ) ) {
- $this->get_tracking_instance()->listing_impression( $product );
- $this->get_tracking_instance()->listing_click( $product );
- }
-
- return $link;
- }
-
- /**
- * Measure a product detail view
- */
- public function product_detail() {
- if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_product_detail_view_enabled ) ) {
- return;
- }
-
- global $product;
- $this->get_tracking_instance()->product_detail( $product );
- }
-
- /**
- * Tracks when the checkout form is loaded
- *
- * @param mixed $checkout (unused)
- */
- public function checkout_process( $checkout ) {
- if ( ! $this->enhanced_ecommerce_enabled( $this->ga_enhanced_checkout_process_enabled ) ) {
- return;
- }
-
- $this->get_tracking_instance()->checkout_process( WC()->cart->get_cart() );
- }
-
/**
* Add the utm_nooverride parameter to any return urls. This makes sure Google Adwords doesn't mistake the offsite gateway as the referrer.
*
diff --git a/includes/class-wc-google-gtag-js.php b/includes/class-wc-google-gtag-js.php
index fc4606de..7303c86d 100644
--- a/includes/class-wc-google-gtag-js.php
+++ b/includes/class-wc-google-gtag-js.php
@@ -41,6 +41,15 @@ public function __construct( $options = array() ) {
// Setup frontend scripts
add_action( 'wp_enqueue_scripts', array( $this, 'register_scripts' ) );
add_action( 'woocommerce_before_single_product', array( $this, 'setup_frontend_scripts' ) );
+
+ // Event tracking code
+ add_action( 'woocommerce_after_add_to_cart_button', array( $this, 'add_to_cart' ) );
+ add_action( 'woocommerce_after_cart', array( $this, 'remove_from_cart' ) );
+ add_action( 'woocommerce_after_mini_cart', array( $this, 'remove_from_cart' ) );
+ add_filter( 'woocommerce_cart_item_remove_link', array( $this, 'remove_from_cart_attributes' ), 10, 2 );
+ add_filter( 'woocommerce_loop_add_to_cart_link', array( $this, 'track_product' ), 10, 2 );
+ add_action( 'woocommerce_after_single_product', array( $this, 'product_detail' ) );
+ add_action( 'woocommerce_after_checkout_form', array( $this, 'checkout_process' ) );
}
/**
@@ -234,10 +243,14 @@ public static function listing_click( $product ) {
/**
* Output Javascript to track add_to_cart event on single product page
- *
- * @param WC_Product $product The product currently being viewed
*/
- public static function add_to_cart( WC_Product $product ) {
+ public static function add_to_cart() {
+ if ( 'yes' !== self::get( 'ga_event_tracking_enabled' ) || ! is_single() ) {
+ return;
+ }
+
+ global $product;
+
$items = array(
'id' => self::get_product_identifier( $product ),
'name' => $product->get_title(),
@@ -382,6 +395,10 @@ public function add_item( $order, $item ) {
* Output JavaScript to track an enhanced ecommerce remove from cart action
*/
public function remove_from_cart() {
+ if ( 'yes' !== self::get( 'ga_enhanced_remove_from_cart_enabled' ) ) {
+ return;
+ }
+
$event_code = self::get_event_code(
'remove_from_cart',
'{"items": [{
@@ -401,16 +418,62 @@ public function remove_from_cart() {
);
}
+
/**
- * Enqueue JavaScript to track a product detail view
+ * Adds the product ID and SKU to the remove product link if not present
*
- * @param WC_Product $product
+ * @param string $url
+ * @param string $key
+ * @return string
*/
- public function product_detail( $product ) {
- if ( empty( $product ) ) {
+ public function remove_from_cart_attributes( $url, $key ) {
+ if ( strpos( $url, 'data-product_id' ) !== false ) {
+ return $url;
+ }
+
+ if ( ! is_object( WC()->cart ) ) {
+ return $url;
+ }
+
+ $item = WC()->cart->get_cart_item( $key );
+ $product = $item['data'];
+
+ if ( ! is_object( $product ) ) {
+ return $url;
+ }
+
+ $url = str_replace( 'href=', 'data-product_id="' . esc_attr( $product->get_id() ) . '" data-product_sku="' . esc_attr( $product->get_sku() ) . '" href=', $url );
+ return $url;
+ }
+
+ /**
+ * Measure a product click and impression from a Product list
+ *
+ * @param string $link The Add To Cart Link
+ * @param WC_Product $product The Product
+ */
+ public function track_product( $link, $product ) {
+ if ( 'yes' === self::get( 'ga_enhanced_product_impression_enabled' ) ) {
+ self::listing_impression( $product );
+ }
+
+ if ( 'yes' === self::get( 'ga_enhanced_product_click_enabled' ) ) {
+ self::listing_click( $product );
+ }
+
+ return $link;
+ }
+
+ /**
+ * Enqueue JavaScript to track a product detail view
+ */
+ public function product_detail() {
+ if ( 'yes' !== self::get( 'ga_enhanced_product_detail_view_enabled' ) ) {
return;
}
+ global $product;
+
$event_code = self::get_event_code(
'view_item',
array(
@@ -430,10 +493,14 @@ public function product_detail( $product ) {
/**
* Enqueue JS to track when the checkout process is started
- *
- * @param array $cart items/contents of the cart
*/
- public function checkout_process( $cart ) {
+ public function checkout_process() {
+ if ( 'yes' !== self::get( 'ga_enhanced_checkout_process_enabled' ) ) {
+ return;
+ }
+
+ $cart = WC()->cart->get_cart();
+
$items = array();
foreach ( $cart as $cart_item_key => $cart_item ) {
$product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
@@ -464,47 +531,4 @@ public function checkout_process( $cart ) {
wc_enqueue_js( $event_code );
}
- /**
- * @deprecated 1.6.0
- *
- * Enqueue JavaScript for Add to cart tracking
- *
- * @param array $parameters Associative array of _trackEvent parameters
- * @param string $selector jQuery selector for binding click event
- */
- public function event_tracking_code( $parameters, $selector ) {
- wc_deprecated_function( 'event_tracking_code', '1.6.0', 'get_event_code' );
-
- // Called with invalid 'Add to Cart' action, update to sync with Default Google Analytics Event 'add_to_cart'
- $parameters['action'] = '\'add_to_cart\'';
- $parameters['category'] = '\'ecommerce\'';
-
- $parameters = apply_filters( 'woocommerce_gtag_event_tracking_parameters', $parameters );
-
- if ( 'yes' === self::get( 'ga_enhanced_ecommerce_tracking_enabled' ) ) {
- $track_event = sprintf(
- self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s, 'items': [ %s ] } );",
- $parameters['action'],
- $parameters['category'],
- $parameters['label'],
- $parameters['item']
- );
- } else {
- $track_event = sprintf(
- self::tracker_var() . "( 'event', %s, { 'event_category': %s, 'event_label': %s } );",
- $parameters['action'],
- $parameters['category'],
- $parameters['label']
- );
- }
-
- wc_enqueue_js(
- "
- $( '" . $selector . "' ).on( 'click', function() {
- " . $track_event . '
- });
- '
- );
- }
-
}
diff --git a/tests/unit-tests/CheckoutProcess.php b/tests/unit-tests/CheckoutProcess.php
index 63c0f957..55ad729a 100644
--- a/tests/unit-tests/CheckoutProcess.php
+++ b/tests/unit-tests/CheckoutProcess.php
@@ -25,7 +25,13 @@ public function test_begin_checkout_event() {
$cart = WC()->cart;
$add_to = $cart->add_to_cart( $product->get_id() );
- ( new WC_Google_Gtag_JS() )->checkout_process( $cart->get_cart() );
+
+ $mock = $this->getMockBuilder( WC_Google_Gtag_JS::class )
+ ->setMethods( array( '__construct' ) )
+ ->setConstructorArgs( array( array( 'ga_enhanced_checkout_process_enabled' => 'yes' ) ) )
+ ->getMock();
+
+ $mock->checkout_process( $cart->get_cart() );
// Confirm woocommerce_gtag_event_data is called by checkout_process().
$this->assertEquals( 1, $this->get_event_data_filter_call_count(), 'woocommerce_gtag_event_data filter was not called for begin_checkout (checkout_process()) event' );
diff --git a/tests/unit-tests/ProductDetail.php b/tests/unit-tests/ProductDetail.php
index f73723aa..e6b34e2d 100644
--- a/tests/unit-tests/ProductDetail.php
+++ b/tests/unit-tests/ProductDetail.php
@@ -19,9 +19,15 @@ class ProductDetail extends EventsDataTest {
* @return void
*/
public function test_view_item_event() {
+ global $product;
$product = $this->get_product();
- ( new WC_Google_Gtag_JS() )->product_detail( $product );
+ $mock = $this->getMockBuilder( WC_Google_Gtag_JS::class )
+ ->setMethods( array( '__construct' ) )
+ ->setConstructorArgs( array( array( 'ga_enhanced_product_detail_view_enabled' => 'yes' ) ) )
+ ->getMock();
+
+ $mock->product_detail();
// Confirm woocommerce_gtag_event_data is called by product_detail().
$this->assertEquals( 1, $this->get_event_data_filter_call_count(), 'woocommerce_gtag_event_data filter was not called for view_item (product_detail()) event' );
diff --git a/webpack.config.js b/webpack.config.js
index 33baae69..2c96cf2a 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -5,11 +5,6 @@ const webpackConfig = {
...defaultConfig,
entry: {
actions: path.resolve( process.cwd(), 'assets/js/src', 'actions.js' ),
- 'admin-ga-settings': path.resolve(
- process.cwd(),
- 'assets/js/src',
- 'admin-ga-settings.js'
- ),
'ga-integration': path.resolve(
process.cwd(),
'assets/js/src',