Skip to content

Commit

Permalink
Merge pull request #287 from woocommerce/fix/async-attribute-tags
Browse files Browse the repository at this point in the history
Fix script tag replacer
  • Loading branch information
puntope authored Aug 8, 2023
2 parents 730da93 + e3dd72c commit 4696a3c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion includes/class-wc-google-analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,13 @@ public function async_script_loader_tags( $tag, $handle, $src ) {
return $tag;
}

return str_replace( '<script src', '<script async src', $tag );
// Check if the script has the async attribute already. If so, don't add it again.
$has_async_tag = preg_match( '/\basync\b/', $tag );
if ( ! empty( $has_async_tag ) ) {
return $tag;
}

// Add the async attribute
return str_replace( ' src', ' async src', $tag );
}
}

0 comments on commit 4696a3c

Please sign in to comment.