Skip to content

Commit

Permalink
Woo Analytics: use core WP function to enqueue script (#13173)
Browse files Browse the repository at this point in the history
* Woo Analytics: use core WP function to enqueue script

Related: #13039

For third-parties to be able to interact with our tracking script, it is easier if it is registered using core WP functions instead of just added to wp_head.

Using wp_enqueue_script makes it easier to dequeue the file if needed, or modify its output with the script_loader_tag filter (one could add an async parameter for example, or add extra data attributes like the Cookiebot plugin.

* Add utility allowing us to load some of our scripts asynchronously

See #13173 (comment)
  • Loading branch information
jeherve authored and dereksmart committed Aug 15, 2019
1 parent 77c3100 commit d7ddbf3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
32 changes: 32 additions & 0 deletions class.jetpack.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ class Jetpack {
'jetpack-widget-social-icons-styles',
);

/**
* The handles of scripts that can be loaded asynchronously.
*
* @var array
*/
public $async_script_handles = array(
'woocommerce-analytics',
);

/**
* Contains all assets that have had their URL rewritten to minified versions.
*
Expand Down Expand Up @@ -699,6 +708,11 @@ private function __construct() {
if ( ! has_action( 'shutdown', array( $this, 'push_stats' ) ) ) {
add_action( 'shutdown', array( $this, 'push_stats' ) );
}

/*
* Load some scripts asynchronously.
*/
add_action( 'script_loader_tag', array( $this, 'script_add_async' ), 10, 3 );
}

/**
Expand Down Expand Up @@ -6347,6 +6361,24 @@ function concat_remove_style_loader_tag( $tag, $handle ) {
return $tag;
}

/**
* Add an async attribute to scripts that can be loaded asynchronously.
* https://www.w3schools.com/tags/att_script_async.asp
*
* @since 7.7.0
*
* @param string $tag The <script> tag for the enqueued script.
* @param string $handle The script's registered handle.
* @param string $src The script's source URL.
*/
public function script_add_async( $tag, $handle, $src ) {
if ( in_array( $handle, $this->async_script_handles, true ) ) {
return preg_replace( '/^<script /i', '<script async ', $tag );
}

return $tag;
}

/*
* Check the heartbeat data
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public function __construct() {
// add to carts from non-product pages or lists (search, store etc.)
add_action( 'wp_head', array( $this, 'loop_session_events' ), 2 );

// loading s.js
add_action( 'wp_head', array( $this, 'wp_head_bottom' ), 999999 );
// loading s.js.
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_tracking_script' ) );

// Capture cart events
add_action( 'woocommerce_add_to_cart', array( $this, 'capture_add_to_cart' ), 10, 6 );
Expand Down Expand Up @@ -64,12 +64,16 @@ public function wp_head_top() {


/**
* Place script to call s.js, Store Analytics
* Place script to call s.js, Store Analytics.
*/
public function wp_head_bottom() {
$filename = 's-' . gmdate( 'YW' ) . '.js';
$async_code = "<script async src='https://stats.wp.com/" . $filename . "'></script>";
echo "$async_code\r\n";
public function enqueue_tracking_script() {
$filename = sprintf(
'https://stats.wp.com/s-%d.js',
gmdate( 'YW' )
);

// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
wp_enqueue_script( 'woocommerce-analytics', esc_url( $filename ), array(), null, false );
}

/**
Expand Down

0 comments on commit d7ddbf3

Please sign in to comment.