From 5670047da880a7488a55afa10ba08ef69da30361 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 20 Nov 2024 17:58:49 -0800 Subject: [PATCH 1/9] Integrate WWO with Site Kit --- plugins/web-worker-offloading/third-party.php | 10 +++-- .../third-party/google-site-kit.php | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 plugins/web-worker-offloading/third-party/google-site-kit.php diff --git a/plugins/web-worker-offloading/third-party.php b/plugins/web-worker-offloading/third-party.php index d1830d81c..51b7ab0a0 100644 --- a/plugins/web-worker-offloading/third-party.php +++ b/plugins/web-worker-offloading/third-party.php @@ -39,14 +39,16 @@ static function ( $to_do ) use ( $script_handles ) { */ function plwwo_load_third_party_integrations(): void { $plugins_with_integrations = array( - // TODO: google-site-kit. - 'woocommerce' => static function (): bool { - // See . - return class_exists( 'WooCommerce' ); + 'google-site-kit' => static function (): bool { + return defined( 'GOOGLESITEKIT_VERSION' ); }, 'seo-by-rank-math' => static function (): bool { return class_exists( 'RankMath' ); }, + 'woocommerce' => static function (): bool { + // See . + return class_exists( 'WooCommerce' ); + }, ); foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) { diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php new file mode 100644 index 000000000..e9099a82c --- /dev/null +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -0,0 +1,37 @@ +|mixed $configuration Configuration. + * @return array Configuration. + */ +function plwwo_google_site_kit_configure( $configuration ): array { + $configuration = (array) $configuration; + + $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. + $configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config. + + return $configuration; +} +add_filter( 'plwwo_configuration', 'plwwo_google_site_kit_configure' ); + +plwwo_mark_scripts_for_offloading( + array( + 'google_gtagjs', + ) +); From c62e6a49adbc8c27141697df9c87c8e8754f2f71 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 20 Nov 2024 18:03:02 -0800 Subject: [PATCH 2/9] Integrate with Site Kit's consent mode --- .../third-party/google-site-kit.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index e9099a82c..2f4098259 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -26,6 +26,10 @@ function plwwo_google_site_kit_configure( $configuration ): array { $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. $configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config. + // See . + $configuration['mainWindowAccessors'][] = '_googlesitekitConsentCategoryMap'; + $configuration['mainWindowAccessors'][] = '_googlesitekitConsents'; + return $configuration; } add_filter( 'plwwo_configuration', 'plwwo_google_site_kit_configure' ); @@ -35,3 +39,23 @@ function plwwo_google_site_kit_configure( $configuration ): array { 'google_gtagjs', ) ); + +/** + * Filters inline script attributes to offload Rank Math's GTag script tag to Partytown. + * + * @since n.e.x.t + * @access private + * @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L169-L174 + * + * @param array|mixed $attributes Script attributes. + * @return array|mixed Filtered inline script attributes. + */ +function plwwo_google_site_kit_filter_inline_script_attributes( $attributes ) { + if ( isset( $attributes['id'] ) && 'google_gtagjs-js-consent-mode-data-layer' === $attributes['id'] ) { + wp_enqueue_script( 'web-worker-offloading' ); + $attributes['type'] = 'text/partytown'; + } + return $attributes; +} + +add_filter( 'wp_inline_script_attributes', 'plwwo_google_site_kit_filter_inline_script_attributes' ); From 0e3e00d58eead47858a3d227e437e712d71c9710 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Wed, 20 Nov 2024 18:32:47 -0800 Subject: [PATCH 3/9] Offload googlesitekit-consent-mode to worker as well --- plugins/web-worker-offloading/third-party/google-site-kit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 2f4098259..5b95aab28 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -37,6 +37,7 @@ function plwwo_google_site_kit_configure( $configuration ): array { plwwo_mark_scripts_for_offloading( array( 'google_gtagjs', + 'googlesitekit-consent-mode', ) ); From c0c4f2781993aadf8b290b42695cd1652203effb Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 10:42:16 -0800 Subject: [PATCH 4/9] Clarify why forward config is used --- plugins/web-worker-offloading/third-party/google-site-kit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 5b95aab28..7c29bec85 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -24,7 +24,7 @@ function plwwo_google_site_kit_configure( $configuration ): array { $configuration = (array) $configuration; $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. - $configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config. + $configuration['forward'][] = 'dataLayer.push'; // See . // See . $configuration['mainWindowAccessors'][] = '_googlesitekitConsentCategoryMap'; From c2a80b015b7c8285d28febe6b73aa255413426f5 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:03:03 -0800 Subject: [PATCH 5/9] Update permalink to Consent_Mode.php --- plugins/web-worker-offloading/third-party/google-site-kit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 7c29bec85..7d100f4b3 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -26,7 +26,7 @@ function plwwo_google_site_kit_configure( $configuration ): array { $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. $configuration['forward'][] = 'dataLayer.push'; // See . - // See . + // See . $configuration['mainWindowAccessors'][] = '_googlesitekitConsentCategoryMap'; $configuration['mainWindowAccessors'][] = '_googlesitekitConsents'; From 0162d90a0fe8b9b1e914ba32f31ce1907abfca47 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 11:16:31 -0800 Subject: [PATCH 6/9] Add missing WP Consent API globals --- .../web-worker-offloading/third-party/google-site-kit.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 7d100f4b3..45b0fb377 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -24,11 +24,17 @@ function plwwo_google_site_kit_configure( $configuration ): array { $configuration = (array) $configuration; $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. + $configuration['globalFns'][] = 'wp_has_consent'; // See . $configuration['forward'][] = 'dataLayer.push'; // See . - // See . + // See , + // and . $configuration['mainWindowAccessors'][] = '_googlesitekitConsentCategoryMap'; $configuration['mainWindowAccessors'][] = '_googlesitekitConsents'; + $configuration['mainWindowAccessors'][] = 'wp_consent_type'; + $configuration['mainWindowAccessors'][] = 'wp_fallback_consent_type'; + $configuration['mainWindowAccessors'][] = 'wp_has_consent'; + $configuration['mainWindowAccessors'][] = 'waitfor_consent_hook'; return $configuration; } From dc7ef36a01f254ca58dfa6528ceaf892170634ad Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 13:20:58 -0800 Subject: [PATCH 7/9] Fix plugin name reference Co-authored-by: Adam Silverstein --- plugins/web-worker-offloading/third-party/google-site-kit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 45b0fb377..52ca540ad 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -48,7 +48,7 @@ function plwwo_google_site_kit_configure( $configuration ): array { ); /** - * Filters inline script attributes to offload Rank Math's GTag script tag to Partytown. + * Filters inline script attributes to offload Google Site Kit's GTag script tag to Partytown. * * @since n.e.x.t * @access private From eeb4327c279c42939a7271eb47a1d5985db43125 Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 13:26:24 -0800 Subject: [PATCH 8/9] Fix permalink to Site Kit on GitHub --- plugins/web-worker-offloading/third-party/google-site-kit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index 52ca540ad..a10b4d903 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -52,7 +52,7 @@ function plwwo_google_site_kit_configure( $configuration ): array { * * @since n.e.x.t * @access private - * @link https://github.com/rankmath/seo-by-rank-math/blob/c78adba6f78079f27ff1430fabb75c6ac3916240/includes/modules/analytics/class-gtag.php#L169-L174 + * @link https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/includes/Core/Consent_Mode/Consent_Mode.php#L244-L259 * * @param array|mixed $attributes Script attributes. * @return array|mixed Filtered inline script attributes. From 6a89d7bda2871ff8a202abd10f5165d7fd7243cd Mon Sep 17 00:00:00 2001 From: Weston Ruter Date: Fri, 13 Dec 2024 14:25:29 -0800 Subject: [PATCH 9/9] Ensure gtag() can be called from main thread --- .../third-party/google-site-kit.php | 9 ++++++--- .../third-party/seo-by-rank-math.php | 5 ++++- .../web-worker-offloading/third-party/woocommerce.php | 9 +++++++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/plugins/web-worker-offloading/third-party/google-site-kit.php b/plugins/web-worker-offloading/third-party/google-site-kit.php index a10b4d903..535c9cd92 100644 --- a/plugins/web-worker-offloading/third-party/google-site-kit.php +++ b/plugins/web-worker-offloading/third-party/google-site-kit.php @@ -23,9 +23,12 @@ function plwwo_google_site_kit_configure( $configuration ): array { $configuration = (array) $configuration; - $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. - $configuration['globalFns'][] = 'wp_has_consent'; // See . - $configuration['forward'][] = 'dataLayer.push'; // See . + $configuration['globalFns'][] = 'gtag'; // Allow calling from other Partytown scripts. + $configuration['globalFns'][] = 'wp_has_consent'; // Allow calling function from main thread. See . + + // Expose on the main tread. See . + $configuration['forward'][] = 'dataLayer.push'; + $configuration['forward'][] = 'gtag'; // See , // and . diff --git a/plugins/web-worker-offloading/third-party/seo-by-rank-math.php b/plugins/web-worker-offloading/third-party/seo-by-rank-math.php index 0ad2bebd2..7436e924b 100644 --- a/plugins/web-worker-offloading/third-party/seo-by-rank-math.php +++ b/plugins/web-worker-offloading/third-party/seo-by-rank-math.php @@ -24,7 +24,10 @@ function plwwo_rank_math_configure( $configuration ): array { $configuration = (array) $configuration; $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. - $configuration['forward'][] = 'dataLayer.push'; // See . + + // Expose on the main tread. See . + $configuration['forward'][] = 'dataLayer.push'; + $configuration['forward'][] = 'gtag'; return $configuration; } add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' ); diff --git a/plugins/web-worker-offloading/third-party/woocommerce.php b/plugins/web-worker-offloading/third-party/woocommerce.php index d94dbb7f6..749d44d4a 100644 --- a/plugins/web-worker-offloading/third-party/woocommerce.php +++ b/plugins/web-worker-offloading/third-party/woocommerce.php @@ -23,10 +23,15 @@ function plwwo_woocommerce_configure( $configuration ): array { $configuration = (array) $configuration; + $configuration['globalFns'][] = 'gtag'; // Allow calling from other Partytown scripts. + + // Expose on the main tread. See . + $configuration['forward'][] = 'dataLayer.push'; + $configuration['forward'][] = 'gtag'; + $configuration['mainWindowAccessors'][] = 'wp'; // Because woocommerce-google-analytics-integration needs to access wp.i18n. $configuration['mainWindowAccessors'][] = 'ga4w'; // Because woocommerce-google-analytics-integration needs to access window.ga4w. - $configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another. - $configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config. + return $configuration; } add_filter( 'plwwo_configuration', 'plwwo_woocommerce_configure' );