|
| 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' ); |
0 commit comments