Load google analytics gtag script asynchronously in WooCommerce Blocks (#43040)

Co-authored-by: github-actions <github-actions@github.com>
This commit is contained in:
Thomas Roberts 2023-12-22 08:42:11 -08:00 committed by GitHub
parent 0c5d01a6ac
commit db17f27f02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 1 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: performance
Load the gtag script used by Google Analytics asynchronously in WooCommerce Blocks.

View File

@ -69,8 +69,19 @@ class GoogleAnalytics {
}
if ( ! wp_script_is( 'google-tag-manager', 'registered' ) ) {
// Using an array with strategies as the final argument to wp_register_script was introduced in WP 6.3.
// WC requires at least 6.3 at the point of adding this, so it's safe to leave in without version checks.
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_register_script( 'google-tag-manager', 'https://www.googletagmanager.com/gtag/js?id=' . $settings['ga_id'], [], null, false );
wp_register_script(
'google-tag-manager',
'https://www.googletagmanager.com/gtag/js?id=' . $settings['ga_id'],
[],
null,
[
'in_footer' => false,
'strategy' => 'async',
]
);
wp_add_inline_script(
'google-tag-manager',
"