Skip to content

Commit

Permalink
Ensure gtag() can be called from main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
westonruter committed Dec 13, 2024
1 parent eeb4327 commit 6a89d7b
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
9 changes: 6 additions & 3 deletions plugins/web-worker-offloading/third-party/google-site-kit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://github.com/google/site-kit-wp/blob/abbb74ff21f98a8779fbab0eeb9a16279a122bc4/assets/js/consent-mode/consent-mode.js#L61C13-L61C27>.
$configuration['forward'][] = 'dataLayer.push'; // See <https://partytown.builder.io/forwarding-event>.
$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>.
Expand Down
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 6a89d7b

Please sign in to comment.