From a12cef3761b28f1a9018be118dcedd0b7009fced Mon Sep 17 00:00:00 2001 From: martynmjones Date: Thu, 29 Feb 2024 17:48:37 +0000 Subject: [PATCH 1/5] Add support for Google Analytics for WooCommerce version 2.0.0 --- src/Google/GlobalSiteTag.php | 74 +++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index c3348242c5..610b7472d4 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -217,16 +217,24 @@ function () use ( $gtag_events ) { */ public function activate_global_site_tag( string $ads_conversion_id ) { if ( $this->gtag_js->is_adding_framework() ) { - add_filter( - 'woocommerce_gtag_snippet', - function ( $gtag_snippet ) use ( $ads_conversion_id ) { - return preg_replace( - '~(\s)~', - "\tgtag('config', '" . $ads_conversion_id . "', { 'groups': 'GLA', 'send_page_view': false });\n$1", - $gtag_snippet - ); - } - ); + if ( version_compare( \WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, '2.0.0', '>=' ) ) { + wp_add_inline_script( + 'woocommerce-google-analytics-integration', + $this->get_gtag_config( $ads_conversion_id ) + ); + } else { + // Legacy code to support Google Analytics for WooCommerce version < 2.0.0. + add_filter( + 'woocommerce_gtag_snippet', + function ( $gtag_snippet ) use ( $ads_conversion_id ) { + return preg_replace( + '~(\s)~', + "\tgtag('config', '" . $ads_conversion_id . "', { 'groups': 'GLA', 'send_page_view': false });\n$1", + $gtag_snippet + ); + } + ); + } } else { $this->display_global_site_tag( $ads_conversion_id ); } @@ -249,16 +257,44 @@ function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('set', 'developer_id.', true); - gtag('config', '', { - 'groups': 'GLA', - 'send_page_view': false - }); + get_gtag_config( $ads_conversion_id ); ?> get_currency() ), esc_js( $order->get_id() ), ); - wp_print_inline_script_tag( $conversion_gtag_info ); + $this->add_inline_event_script( $conversion_gtag_info ); // Get the item info in the order $item_info = []; @@ -355,7 +391,7 @@ public function maybe_display_conversion_and_purchase_event_snippets( string $ad esc_js( $language ), join( ',', $item_info ), ); - wp_print_inline_script_tag( $purchase_page_gtag ); + $this->add_inline_event_script( $purchase_page_gtag ); } /** @@ -387,7 +423,7 @@ private function display_view_item_event_snippet(): void { esc_js( $product->get_name() ), esc_js( join( ' & ', $this->product_helper->get_categories( $product ) ) ), ); - wp_print_inline_script_tag( $view_item_gtag ); + $this->add_inline_event_script( $view_item_gtag ); } /** @@ -395,7 +431,7 @@ private function display_view_item_event_snippet(): void { */ private function display_page_view_event_snippet(): void { if ( ! is_cart() ) { - wp_print_inline_script_tag( + $this->add_inline_event_script( 'gtag("event", "page_view", {send_to: "GLA"});' ); return; @@ -438,7 +474,7 @@ private function display_page_view_event_snippet(): void { $value, join( ',', $item_info ), ); - wp_print_inline_script_tag( $page_view_gtag ); + $this->add_inline_event_script( $page_view_gtag ); } /** From 3a423a2d5cfca64486c74cf41905d01db1cc4c52 Mon Sep 17 00:00:00 2001 From: martynmjones Date: Thu, 29 Feb 2024 18:03:05 +0000 Subject: [PATCH 2/5] phpcs --- src/Google/GlobalSiteTag.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index 610b7472d4..4ffaa857d6 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -257,7 +257,7 @@ function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('set', 'developer_id.', true); - get_gtag_config( $ads_conversion_id ); ?> + get_gtag_config( $ads_conversion_id ) ); ?> Date: Fri, 1 Mar 2024 18:57:30 +0100 Subject: [PATCH 3/5] Consider `ga_gtag_enabled=yes` for WCGAI >= 2 Addresses https://github.com/woocommerce/google-listings-and-ads/pull/2287#discussion_r1508145827 --- src/Proxies/GoogleGtagJs.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Proxies/GoogleGtagJs.php b/src/Proxies/GoogleGtagJs.php index 3b6f88726a..80d667a3b8 100644 --- a/src/Proxies/GoogleGtagJs.php +++ b/src/Proxies/GoogleGtagJs.php @@ -22,7 +22,9 @@ public function __construct() { $this->wcga_settings = get_option( 'woocommerce_google_analytics_settings', [] ); // Prime some values. - if ( empty( $this->wcga_settings['ga_gtag_enabled'] ) ) { + if ( defined( '\WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION' ) && version_compare( \WC_GOOGLE_ANALYTICS_INTEGRATION_VERSION, '2.0.0', '>=' ) ) { + $this->wcga_settings['ga_gtag_enabled'] = 'yes'; + } elseif ( empty( $this->wcga_settings['ga_gtag_enabled'] ) ) { $this->wcga_settings['ga_gtag_enabled'] = 'no'; } if ( empty( $this->wcga_settings['ga_standard_tracking_enabled'] ) ) { From a8d5400beac68bf0fb38c5a96b2f67ac6cb33228 Mon Sep 17 00:00:00 2001 From: martynmjones <40762232+martynmjones@users.noreply.github.com> Date: Fri, 1 Mar 2024 18:32:09 +0000 Subject: [PATCH 4/5] Remove esc_js from gtag config MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tomek Wytrębowicz --- src/Google/GlobalSiteTag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index 4ffaa857d6..9cc8d4a0e4 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -257,7 +257,7 @@ function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('set', 'developer_id.', true); - get_gtag_config( $ads_conversion_id ) ); ?> + get_gtag_config( $ads_conversion_id ); ?> Date: Fri, 1 Mar 2024 18:37:51 +0000 Subject: [PATCH 5/5] Ignore OutputNotEscaped warning for gtag config output --- src/Google/GlobalSiteTag.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Google/GlobalSiteTag.php b/src/Google/GlobalSiteTag.php index 9cc8d4a0e4..20d7fc192c 100644 --- a/src/Google/GlobalSiteTag.php +++ b/src/Google/GlobalSiteTag.php @@ -257,7 +257,10 @@ function gtag() { dataLayer.push(arguments); } gtag('js', new Date()); gtag('set', 'developer_id.', true); - get_gtag_config( $ads_conversion_id ); ?> + get_gtag_config( $ads_conversion_id ); + ?>