-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1686 from WordPress/add/wwo-site-kit-integration
Integrate Web Worker Offloading with Google Site Kit
- Loading branch information
Showing
4 changed files
with
88 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
plugins/web-worker-offloading/third-party/google-site-kit.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters