CheckoutFieldsSchema
11 months ago
Email
1 year ago
CheckoutFields.php
11 months ago
CheckoutFieldsAdmin.php
2 years ago
CheckoutFieldsFrontend.php
11 months ago
CheckoutLink.php
11 months ago
CreateAccount.php
1 year ago
DraftOrders.php
2 months ago
FeatureGating.php
1 year ago
GoogleAnalytics.php
2 years ago
Hydration.php
5 months ago
Notices.php
1 year ago
functions.php
2 years ago
GoogleAnalytics.php
131 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Blocks\Domain\Services; |
| 3 | |
| 4 | use Automattic\WooCommerce\Blocks\Package; |
| 5 | use Automattic\WooCommerce\Blocks\Assets\Api as AssetApi; |
| 6 | |
| 7 | /** |
| 8 | * Service class to integrate Blocks with the Google Analytics extension, |
| 9 | */ |
| 10 | class GoogleAnalytics { |
| 11 | /** |
| 12 | * Instance of the asset API. |
| 13 | * |
| 14 | * @var AssetApi |
| 15 | */ |
| 16 | protected $asset_api; |
| 17 | |
| 18 | /** |
| 19 | * Constructor. |
| 20 | * |
| 21 | * @param AssetApi $asset_api Instance of the asset API. |
| 22 | */ |
| 23 | public function __construct( AssetApi $asset_api ) { |
| 24 | $this->asset_api = $asset_api; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Hook into WP. |
| 29 | */ |
| 30 | public function init() { |
| 31 | // Require Google Analytics Integration to be activated. |
| 32 | if ( ! class_exists( 'WC_Google_Analytics_Integration', false ) ) { |
| 33 | return; |
| 34 | } |
| 35 | add_action( 'init', array( $this, 'register_assets' ) ); |
| 36 | add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); |
| 37 | add_filter( 'script_loader_tag', array( $this, 'async_script_loader_tags' ), 10, 3 ); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Register scripts. |
| 42 | */ |
| 43 | public function register_assets() { |
| 44 | $this->asset_api->register_script( 'wc-blocks-google-analytics', 'assets/client/blocks/wc-blocks-google-analytics.js', [ 'google-tag-manager' ] ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Enqueue the Google Tag Manager script if prerequisites are met. |
| 49 | */ |
| 50 | public function enqueue_scripts() { |
| 51 | $settings = $this->get_google_analytics_settings(); |
| 52 | $prefix = strstr( strtoupper( $settings['ga_id'] ), '-', true ); |
| 53 | |
| 54 | // Require tracking to be enabled with a valid GA ID. |
| 55 | if ( ! in_array( $prefix, [ 'G', 'GT' ], true ) ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Filter to disable Google Analytics tracking. |
| 61 | * |
| 62 | * @internal Matches filter name in GA extension. |
| 63 | * @since 4.9.0 |
| 64 | * |
| 65 | * @param boolean $disable_tracking If true, tracking will be disabled. |
| 66 | */ |
| 67 | if ( apply_filters( 'woocommerce_ga_disable_tracking', ! wc_string_to_bool( $settings['ga_event_tracking_enabled'] ) ) ) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | if ( ! wp_script_is( 'google-tag-manager', 'registered' ) ) { |
| 72 | // Using an array with strategies as the final argument to wp_register_script was introduced in WP 6.3. |
| 73 | // WC requires at least 6.3 at the point of adding this, so it's safe to leave in without version checks. |
| 74 | // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion |
| 75 | wp_register_script( |
| 76 | 'google-tag-manager', |
| 77 | 'https://www.googletagmanager.com/gtag/js?id=' . $settings['ga_id'], |
| 78 | [], |
| 79 | null, |
| 80 | [ |
| 81 | 'in_footer' => false, |
| 82 | 'strategy' => 'async', |
| 83 | ] |
| 84 | ); |
| 85 | wp_add_inline_script( |
| 86 | 'google-tag-manager', |
| 87 | " |
| 88 | window.dataLayer = window.dataLayer || []; |
| 89 | function gtag(){dataLayer.push(arguments);} |
| 90 | gtag('js', new Date()); |
| 91 | gtag('config', '" . esc_js( $settings['ga_id'] ) . "', { 'send_page_view': false });" |
| 92 | ); |
| 93 | } |
| 94 | wp_enqueue_script( 'wc-blocks-google-analytics' ); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Get settings from the GA integration extension. |
| 99 | * |
| 100 | * @return array |
| 101 | */ |
| 102 | private function get_google_analytics_settings() { |
| 103 | return wp_parse_args( |
| 104 | get_option( 'woocommerce_google_analytics_settings' ), |
| 105 | [ |
| 106 | 'ga_id' => '', |
| 107 | 'ga_event_tracking_enabled' => 'no', |
| 108 | ] |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Add async to script tags with defined handles. |
| 114 | * |
| 115 | * @param string $tag HTML for the script tag. |
| 116 | * @param string $handle Handle of script. |
| 117 | * @param string $src Src of script. |
| 118 | * @return string |
| 119 | */ |
| 120 | public function async_script_loader_tags( $tag, $handle, $src ) { |
| 121 | if ( ! in_array( $handle, array( 'google-tag-manager' ), true ) ) { |
| 122 | return $tag; |
| 123 | } |
| 124 | // If script was output manually in wp_head, abort. |
| 125 | if ( did_action( 'woocommerce_gtag_snippet' ) ) { |
| 126 | return ''; |
| 127 | } |
| 128 | return str_replace( '<script src', '<script async src', $tag ); |
| 129 | } |
| 130 | } |
| 131 |