Skip to content

Commit

Permalink
Merge pull request #1686 from WordPress/add/wwo-site-kit-integration
Browse files Browse the repository at this point in the history
Integrate Web Worker Offloading with Google Site Kit
  • Loading branch information
westonruter authored Dec 14, 2024
2 parents 094ef41 + 6a89d7b commit fb9769e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
10 changes: 6 additions & 4 deletions plugins/web-worker-offloading/third-party.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
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 <https://woocommerce.com/document/query-whether-woocommerce-is-activated/>.
return class_exists( 'WooCommerce' );
},
);

foreach ( $plugins_with_integrations as $plugin_slug => $active_callback ) {
Expand Down
71 changes: 71 additions & 0 deletions plugins/web-worker-offloading/third-party/google-site-kit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Web Worker Offloading integration with Site Kit by Google.
*
* @since n.e.x.t
* @package web-worker-offloading
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}

/**
* Configures WWO for Site Kit and Google Analytics.
*
* @since n.e.x.t
* @access private
* @link https://partytown.builder.io/google-tag-manager#forward-events
*
* @param array<string, mixed>|mixed $configuration Configuration.
* @return array<string, mixed> 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 <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/assets/js/consent-mode/consent-mode.js#L61C13-L61C27>.

// Expose on the main tread. See <https://partytown.builder.io/forwarding-event>.
$configuration['forward'][] = 'dataLayer.push';
$configuration['forward'][] = 'gtag';

// See <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/includes/Core/Consent_Mode/Consent_Mode.php#L244-L259>,
// and <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/assets/js/consent-mode/consent-mode.js>.
$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' );
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://partytown.builder.io/forwarding-event>.

// Expose on the main tread. See <https://partytown.builder.io/forwarding-event>.
$configuration['forward'][] = 'dataLayer.push';
$configuration['forward'][] = 'gtag';
return $configuration;
}
add_filter( 'plwwo_configuration', 'plwwo_rank_math_configure' );
Expand Down
9 changes: 7 additions & 2 deletions plugins/web-worker-offloading/third-party/woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://partytown.builder.io/forwarding-event>.
$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' );
Expand Down

0 comments on commit fb9769e

Please sign in to comment.