Skip to content

Commit

Permalink
Woo Analytics: use core WP function to enqueue script
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeherve committed Aug 2, 2019
1 parent 90c52c5 commit 2a82d5f
Showing 1 changed file with 11 additions and 7 deletions.
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 2a82d5f

Please sign in to comment.