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..535c9cd92
--- /dev/null
+++ b/plugins/web-worker-offloading/third-party/google-site-kit.php
@@ -0,0 +1,71 @@
+|mixed $configuration Configuration.
+ * @return array Configuration.
+ */
+function plwwo_google_site_kit_configure( $configuration ): array {
+ $configuration = (array) $configuration;
+
+ $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 .
+ $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;
+}
+add_filter( 'plwwo_configuration', 'plwwo_google_site_kit_configure' );
+
+plwwo_mark_scripts_for_offloading(
+ array(
+ 'google_gtagjs',
+ 'googlesitekit-consent-mode',
+ )
+);
+
+/**
+ * Filters inline script attributes to offload Google Site Kit's GTag script tag to Partytown.
+ *
+ * @since n.e.x.t
+ * @access private
+ * @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.
+ */
+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' );
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' );