Merge pull request #22903 from woocommerce/add/tracks-obw-marketing
Tracks: Add marketing signup tracking in OBW
This commit is contained in:
commit
c04dcb25b5
|
@ -331,6 +331,7 @@ class WC_Admin_Setup_Wizard {
|
||||||
<?php elseif ( 'recommended' === $this->step || 'activate' === $this->step ) : ?>
|
<?php elseif ( 'recommended' === $this->step || 'activate' === $this->step ) : ?>
|
||||||
<a class="wc-setup-footer-links" href="<?php echo esc_url( $this->get_next_step_link() ); ?>"><?php esc_html_e( 'Skip this step', 'woocommerce' ); ?></a>
|
<a class="wc-setup-footer-links" href="<?php echo esc_url( $this->get_next_step_link() ); ?>"><?php esc_html_e( 'Skip this step', 'woocommerce' ); ?></a>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
<?php do_action( 'woocommerce_setup_footer' ); ?>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
<?php
|
<?php
|
||||||
|
|
|
@ -48,21 +48,43 @@ class WC_Site_Tracking {
|
||||||
// Add w.js to the page.
|
// Add w.js to the page.
|
||||||
wp_enqueue_script( 'woo-tracks', 'https://stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
|
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.
|
// Expose tracking via a function in the wcTracks global namespace directly before wc_print_js.
|
||||||
wc_enqueue_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 = window.wcTracks || {};
|
||||||
window.wcTracks.recordEvent = function( name, properties ) {
|
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 || {};
|
var eventProperties = properties || {};
|
||||||
eventProperties.url = '" . home_url() . "'
|
eventProperties.url = '<?php echo esc_html( home_url() ); ?>'
|
||||||
eventProperties.products_count = '" . WC_Tracks::get_products_count() . "'
|
eventProperties.products_count = '<?php echo intval( WC_Tracks::get_products_count() ); ?>';
|
||||||
eventProperties.orders_gross = '" . WC_Tracks::get_total_revenue() . "'
|
eventProperties.orders_gross = '<?php echo floatval( WC_Tracks::get_total_revenue() ); ?>';
|
||||||
window._tkq = window._tkq || [];
|
window._tkq = window._tkq || [];
|
||||||
window._tkq.push( [ 'recordEvent', eventName, eventProperties ] );
|
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() ) {
|
if ( ! self::is_tracking_enabled() ) {
|
||||||
|
|
||||||
// Define window.wcTracks.recordEvent in case there is an attempt to use it when tracking is turned off.
|
// Define window.wcTracks.recordEvent in case there is an attempt to use it when tracking is turned off.
|
||||||
wc_enqueue_js(
|
add_filter( 'admin_footer', array( __CLASS__, 'add_empty_tracking_function' ), 24 );
|
||||||
'
|
|
||||||
window.wcTracks = window.wcTracks || {};
|
|
||||||
window.wcTracks.recordEvent = function() {};
|
|
||||||
'
|
|
||||||
);
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,12 +44,48 @@ class WC_Admin_Setup_Wizard_Tracking {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
add_action( 'wc_setup_footer', array( __CLASS__, 'add_footer_scripts' ) );
|
||||||
add_filter( 'woocommerce_setup_wizard_steps', array( __CLASS__, 'set_obw_steps' ) );
|
add_filter( 'woocommerce_setup_wizard_steps', array( __CLASS__, 'set_obw_steps' ) );
|
||||||
add_action( 'shutdown', array( __CLASS__, 'track_skip_step' ), 1 );
|
add_action( 'shutdown', array( __CLASS__, 'track_skip_step' ), 1 );
|
||||||
add_action( 'add_option_woocommerce_allow_tracking', array( __CLASS__, 'track_start' ), 10, 2 );
|
add_action( 'add_option_woocommerce_allow_tracking', array( __CLASS__, 'track_start' ), 10, 2 );
|
||||||
|
add_action( 'admin_init', array( __CLASS__, 'track_marketing_signup' ), 1 );
|
||||||
|
add_action( 'wp_print_scripts', array( __CLASS__, 'dequeue_non_whitelisted_scripts' ) );
|
||||||
self::add_step_save_events();
|
self::add_step_save_events();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the name of the current step.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function get_current_step() {
|
||||||
|
return isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add footer scripts to OBW since it does not contain hooks for
|
||||||
|
* wp_footer to allow the default methods of enqueuing scripts.
|
||||||
|
*/
|
||||||
|
public static function add_footer_scripts() {
|
||||||
|
wp_print_scripts();
|
||||||
|
WC_Site_Tracking::add_tracking_function();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dequeue unwanted scripts from OBW footer.
|
||||||
|
*/
|
||||||
|
public static function dequeue_non_whitelisted_scripts() {
|
||||||
|
global $wp_scripts;
|
||||||
|
$whitelist = array( 'woo-tracks' );
|
||||||
|
|
||||||
|
foreach ( $wp_scripts->queue as $script ) {
|
||||||
|
if ( in_array( $script, $whitelist, true ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
wp_dequeue_script( $script );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Track when tracking is opted into and OBW has started.
|
* Track when tracking is opted into and OBW has started.
|
||||||
*
|
*
|
||||||
|
@ -65,6 +101,24 @@ class WC_Admin_Setup_Wizard_Tracking {
|
||||||
WC_Tracks::record_event( 'obw_start' );
|
WC_Tracks::record_event( 'obw_start' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Track the marketing form on submit.
|
||||||
|
*/
|
||||||
|
public static function track_marketing_signup() {
|
||||||
|
if ( 'next_steps' !== self::get_current_step() ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
wc_enqueue_js(
|
||||||
|
"
|
||||||
|
var form = $( '.newsletter-form-email' ).closest( 'form' );
|
||||||
|
$( document ).on( 'submit', form, function() {
|
||||||
|
window.wcTracks.recordEvent( 'obw_marketing_signup' );
|
||||||
|
} );
|
||||||
|
"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Track various events when a step is saved.
|
* Track various events when a step is saved.
|
||||||
*/
|
*/
|
||||||
|
@ -73,10 +127,9 @@ class WC_Admin_Setup_Wizard_Tracking {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$current_step = isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification
|
update_option( 'woocommerce_obw_last_completed_step', self::get_current_step() );
|
||||||
update_option( 'woocommerce_obw_last_completed_step', $current_step );
|
|
||||||
|
|
||||||
switch ( $current_step ) {
|
switch ( self::get_current_step() ) {
|
||||||
case '':
|
case '':
|
||||||
case 'store_setup':
|
case 'store_setup':
|
||||||
add_action( 'admin_init', array( __CLASS__, 'track_store_setup' ), 1 );
|
add_action( 'admin_init', array( __CLASS__, 'track_store_setup' ), 1 );
|
||||||
|
@ -207,7 +260,7 @@ class WC_Admin_Setup_Wizard_Tracking {
|
||||||
*/
|
*/
|
||||||
public static function track_skip_step() {
|
public static function track_skip_step() {
|
||||||
$previous_step = get_option( 'woocommerce_obw_last_completed_step' );
|
$previous_step = get_option( 'woocommerce_obw_last_completed_step' );
|
||||||
$current_step = isset( $_GET['step'] ) ? sanitize_text_field( $_GET['step'] ) : false; // phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput
|
$current_step = self::get_current_step();
|
||||||
if ( ! $previous_step || ! $current_step ) {
|
if ( ! $previous_step || ! $current_step ) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue