Skip to content

Commit fb9769e

Browse files
authored
Merge pull request #1686 from WordPress/add/wwo-site-kit-integration
Integrate Web Worker Offloading with Google Site Kit
2 parents 094ef41 + 6a89d7b commit fb9769e

File tree

4 files changed

+88
-7
lines changed

4 files changed

+88
-7
lines changed

plugins/web-worker-offloading/third-party.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,16 @@ static function ( $to_do ) use ( $script_handles ) {
3939
*/
4040
function plwwo_load_third_party_integrations(): void {
4141
$plugins_with_integrations = array(
42-
// TODO: google-site-kit.
43-
'woocommerce' => static function (): bool {
44-
// See <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
45-
return class_exists( 'WooCommerce' );
42+
'google-site-kit' => static function (): bool {
43+
return defined( 'GOOGLESITEKIT_VERSION' );
4644
},
4745
'seo-by-rank-math' => static function (): bool {
4846
return class_exists( 'RankMath' );
4947
},
48+
'woocommerce' => static function (): bool {
49+
// See <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
50+
return class_exists( 'WooCommerce' );
51+
},
5052
);
5153

5254
foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) {
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Web Worker Offloading integration with Site Kit by Google.
4+
*
5+
* @since n.e.x.t
6+
* @package web-worker-offloading
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit; // Exit if accessed directly.
11+
}
12+
13+
/**
14+
* Configures WWO for Site Kit and Google Analytics.
15+
*
16+
* @since n.e.x.t
17+
* @access private
18+
* @link https://partytown.builder.io/google-tag-manager#forward-events
19+
*
20+
* @param array<string, mixed>|mixed $configuration Configuration.
21+
* @return array<string, mixed> Configuration.
22+
*/
23+
function plwwo_google_site_kit_configure( $configuration ): array {
24+
$configuration = (array) $configuration;
25+
26+
$configuration['globalFns'][] = 'gtag'; // Allow calling from other Partytown scripts.
27+
$configuration['globalFns'][] = 'wp_has_consent'; // Allow calling function from main thread. See <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/assets/js/consent-mode/consent-mode.js#L61C13-L61C27>.
28+
29+
// Expose on the main tread. See <https://partytown.builder.io/forwarding-event>.
30+
$configuration['forward'][] = 'dataLayer.push';
31+
$configuration['forward'][] = 'gtag';
32+
33+
// See <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/includes/Core/Consent_Mode/Consent_Mode.php#L244-L259>,
34+
// and <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/assets/js/consent-mode/consent-mode.js>.
35+
$configuration['mainWindowAccessors'][] = '_googlesitekitConsentCategoryMap';
36+
$configuration['mainWindowAccessors'][] = '_googlesitekitConsents';
37+
$configuration['mainWindowAccessors'][] = 'wp_consent_type';
38+
$configuration['mainWindowAccessors'][] = 'wp_fallback_consent_type';
39+
$configuration['mainWindowAccessors'][] = 'wp_has_consent';
40+
$configuration['mainWindowAccessors'][] = 'waitfor_consent_hook';
41+
42+
return $configuration;
43+
}
44+
add_filter( 'plwwo_configuration', 'plwwo_google_site_kit_configure' );
45+
46+
plwwo_mark_scripts_for_offloading(
47+
array(
48+
'google_gtagjs',
49+
'googlesitekit-consent-mode',
50+
)
51+
);
52+
53+
/**
54+
* Filters inline script attributes to offload Google Site Kit's GTag script tag to Partytown.
55+
*
56+
* @since n.e.x.t
57+
* @access private
58+
* @link https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/includes/Core/Consent_Mode/Consent_Mode.php#L244-L259
59+
*
60+
* @param array|mixed $attributes Script attributes.
61+
* @return array|mixed Filtered inline script attributes.
62+
*/
63+
function plwwo_google_site_kit_filter_inline_script_attributes( $attributes ) {
64+
if ( isset( $attributes['id'] ) && 'google_gtagjs-js-consent-mode-data-layer' === $attributes['id'] ) {
65+
wp_enqueue_script( 'web-worker-offloading' );
66+
$attributes['type'] = 'text/partytown';
67+
}
68+
return $attributes;
69+
}
70+
71+
add_filter( 'wp_inline_script_attributes', 'plwwo_google_site_kit_filter_inline_script_attributes' );

plugins/web-worker-offloading/third-party/seo-by-rank-math.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ function plwwo_rank_math_configure( $configuration ): array {
2424
$configuration = (array) $configuration;
2525

2626
$configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another.
27-
$configuration['forward'][] = 'dataLayer.push'; // See <https://partytown.builder.io/forwarding-event>.
27+
28+
// Expose on the main tread. See <https://partytown.builder.io/forwarding-event>.
29+
$configuration['forward'][] = 'dataLayer.push';
30+
$configuration['forward'][] = 'gtag';
2831
return $configuration;
2932
}
3033
add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' );

plugins/web-worker-offloading/third-party/woocommerce.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@
2323
function plwwo_woocommerce_configure( $configuration ): array {
2424
$configuration = (array) $configuration;
2525

26+
$configuration['globalFns'][] = 'gtag'; // Allow calling from other Partytown scripts.
27+
28+
// Expose on the main tread. See <https://partytown.builder.io/forwarding-event>.
29+
$configuration['forward'][] = 'dataLayer.push';
30+
$configuration['forward'][] = 'gtag';
31+
2632
$configuration['mainWindowAccessors'][] = 'wp'; // Because woocommerce-google-analytics-integration needs to access wp.i18n.
2733
$configuration['mainWindowAccessors'][] = 'ga4w'; // Because woocommerce-google-analytics-integration needs to access window.ga4w.
28-
$configuration['globalFns'][] = 'gtag'; // Because gtag() is defined in one script and called in another.
29-
$configuration['forward'][] = 'dataLayer.push'; // Because the Partytown integration has this in its example config.
34+
3035
return $configuration;
3136
}
3237
add_filter( 'plwwo_configuration', 'plwwo_woocommerce_configure' );

0 commit comments

Comments
 (0)