Tracks: Move tracking script to make available on page load (#22914)

* Filter wc queued js to add tracking script outside of jQuery

* Add tracking script directly to footer

* Move empty tracking function to admin footer hook
This commit is contained in:
Joshua T Flowers 2019-03-04 12:20:53 +08:00 committed by Joshua Flowers
parent 9e6836e3a0
commit 17b786e500
2 changed files with 33 additions and 16 deletions

View File

@ -48,21 +48,43 @@ class WC_Site_Tracking {
// Add w.js to the page.
wp_enqueue_script( 'woo-tracks', 'https://stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
// Expose tracking via a function in the wcTracks global namespace.
wc_enqueue_js(
"
// Expose tracking via a function in the wcTracks global namespace directly before wc_print_js.
add_filter( 'admin_footer', array( __CLASS__, 'add_tracking_function' ), 24 );
}
/**
* Adds the tracking function to the admin footer.
*/
public static function add_tracking_function() {
?>
<!-- WooCommerce Tracks -->
<script type="text/javascript">
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = function( name, properties ) {
var eventName = '" . WC_Tracks::PREFIX . "' + name;
var eventName = '<?php echo esc_attr( WC_Tracks::PREFIX ); ?>' + name;
var eventProperties = properties || {};
eventProperties.url = '" . home_url() . "'
eventProperties.products_count = '" . WC_Tracks::get_products_count() . "'
eventProperties.orders_gross = '" . WC_Tracks::get_total_revenue() . "'
eventProperties.url = '<?php echo esc_html( home_url() ); ?>'
eventProperties.products_count = '<?php echo intval( WC_Tracks::get_products_count() ); ?>';
eventProperties.orders_gross = '<?php echo floatval( WC_Tracks::get_total_revenue() ); ?>';
window._tkq = window._tkq || [];
window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
}
"
);
</script>
<?php
}
/**
* Add empty tracking function to admin footer when tracking is disabled in case
* it's called without checking if it's defined beforehand.
*/
public static function add_empty_tracking_function() {
?>
<script type="text/javascript">
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = function() {};
</script>
<?php
}
/**
@ -73,12 +95,7 @@ class WC_Site_Tracking {
if ( ! self::is_tracking_enabled() ) {
// Define window.wcTracks.recordEvent in case there is an attempt to use it when tracking is turned off.
wc_enqueue_js(
'
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = function() {};
'
);
add_filter( 'admin_footer', array( __CLASS__, 'add_empty_tracking_function' ), 24 );
return;
}

View File

@ -68,7 +68,7 @@ class WC_Admin_Setup_Wizard_Tracking {
*/
public static function add_footer_scripts() {
wp_print_scripts();
wc_print_js();
WC_Site_Tracking::add_tracking_function();
}
/**