2019-02-28 03:28:07 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Admin Setup Wizard Tracking
|
|
|
|
*
|
|
|
|
* @package WooCommerce\Tracks
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class adds actions to track usage of the WooCommerce Onboarding Wizard.
|
|
|
|
*/
|
|
|
|
class WC_Admin_Setup_Wizard_Tracking {
|
2019-03-01 02:58:11 +00:00
|
|
|
/**
|
|
|
|
* Steps for the setup wizard
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $steps = array();
|
|
|
|
|
2019-02-28 03:28:07 +00:00
|
|
|
/**
|
|
|
|
* Init tracking.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function init() {
|
2019-02-28 03:28:07 +00:00
|
|
|
if ( empty( $_GET['page'] ) || 'wc-setup' !== $_GET['page'] ) { // WPCS: CSRF ok, input var ok.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
add_filter( 'woocommerce_setup_wizard_steps', array( $this, 'set_obw_steps' ) );
|
|
|
|
add_action( 'shutdown', array( $this, 'track_skip_step' ), 1 );
|
|
|
|
add_action( 'add_option_woocommerce_allow_tracking', array( $this, 'track_start' ), 10, 2 );
|
2019-02-28 09:41:15 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_ready_next_steps' ), 1 );
|
2020-08-25 21:03:03 +00:00
|
|
|
add_action( 'wp_print_scripts', array( $this, 'dequeue_non_allowed_scripts' ) );
|
2019-03-05 00:30:01 +00:00
|
|
|
$this->add_step_save_events();
|
2019-04-17 12:54:59 +00:00
|
|
|
add_action( 'woocommerce_setup_footer', array( $this, 'add_footer_scripts' ) );
|
2019-02-28 03:28:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:54:07 +00:00
|
|
|
/**
|
|
|
|
* Get the name of the current step.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function get_current_step() {
|
2019-12-13 19:58:14 +00:00
|
|
|
return isset( $_GET['step'] ) ? sanitize_key( $_GET['step'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2019-02-28 09:54:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 09:41:15 +00:00
|
|
|
/**
|
2019-04-17 12:54:59 +00:00
|
|
|
* Add footer scripts to OBW via woocommerce_setup_footer
|
2019-02-28 09:41:15 +00:00
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function add_footer_scripts() {
|
2019-04-17 12:56:51 +00:00
|
|
|
wp_print_scripts();
|
2019-03-04 04:20:53 +00:00
|
|
|
WC_Site_Tracking::add_tracking_function();
|
2019-04-25 21:52:38 +00:00
|
|
|
wc_print_js();
|
2019-02-28 09:41:15 +00:00
|
|
|
}
|
|
|
|
|
2019-03-01 03:24:02 +00:00
|
|
|
/**
|
|
|
|
* Dequeue unwanted scripts from OBW footer.
|
|
|
|
*/
|
2020-08-25 21:03:03 +00:00
|
|
|
public function dequeue_non_allowed_scripts() {
|
2019-03-01 03:24:02 +00:00
|
|
|
global $wp_scripts;
|
2020-08-25 21:03:03 +00:00
|
|
|
$allowed = array( 'woo-tracks' );
|
2019-03-01 03:24:02 +00:00
|
|
|
|
|
|
|
foreach ( $wp_scripts->queue as $script ) {
|
2020-08-25 21:03:03 +00:00
|
|
|
if ( in_array( $script, $allowed, true ) ) {
|
2019-03-01 03:24:02 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
wp_dequeue_script( $script );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-01 03:12:30 +00:00
|
|
|
/**
|
|
|
|
* Track when tracking is opted into and OBW has started.
|
|
|
|
*
|
|
|
|
* @param string $option Option name.
|
|
|
|
* @param string $value Option value.
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_start( $option, $value ) {
|
2019-12-13 19:58:14 +00:00
|
|
|
if ( 'yes' !== $value || empty( $_GET['page'] ) || 'wc-setup' !== $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
|
2019-03-01 03:12:30 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'obw_start' );
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:42:38 +00:00
|
|
|
/**
|
|
|
|
* Track the marketing form on submit.
|
|
|
|
*/
|
2019-02-28 09:41:15 +00:00
|
|
|
public function track_ready_next_steps() {
|
2019-03-05 00:30:01 +00:00
|
|
|
if ( 'next_steps' !== $this->get_current_step() ) {
|
2019-02-28 09:54:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-28 09:42:38 +00:00
|
|
|
wc_enqueue_js(
|
|
|
|
"
|
2019-07-02 21:47:41 +00:00
|
|
|
jQuery( '#mc-embedded-subscribe' ).click( function() {
|
2019-02-28 09:42:38 +00:00
|
|
|
window.wcTracks.recordEvent( 'obw_marketing_signup' );
|
|
|
|
} );
|
2019-07-02 21:47:41 +00:00
|
|
|
jQuery( '.wc-setup-content a' ).click( function trackNextScreen( e ) {
|
2019-02-28 09:41:15 +00:00
|
|
|
var properties = {
|
|
|
|
next_url: e.target.href,
|
|
|
|
button: e.target.textContent && e.target.textContent.trim()
|
|
|
|
};
|
|
|
|
window.wcTracks.recordEvent( 'obw_ready_next_step', properties );
|
|
|
|
} );
|
2019-02-28 09:42:38 +00:00
|
|
|
"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-28 03:28:07 +00:00
|
|
|
/**
|
|
|
|
* Track various events when a step is saved.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function add_step_save_events() {
|
2019-07-02 21:56:08 +00:00
|
|
|
// Always record a track on this page view.
|
|
|
|
if ( 'next_steps' === $this->get_current_step() ) {
|
|
|
|
add_action( 'admin_init', array( $this, 'track_next_steps' ), 1 );
|
|
|
|
}
|
|
|
|
|
2019-12-13 19:58:14 +00:00
|
|
|
if ( empty( $_POST['save_step'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing
|
2019-02-28 03:28:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
update_option( 'woocommerce_obw_last_completed_step', $this->get_current_step() );
|
2019-03-01 02:58:11 +00:00
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
switch ( $this->get_current_step() ) {
|
2019-02-28 03:28:07 +00:00
|
|
|
case '':
|
|
|
|
case 'store_setup':
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_store_setup' ), 1 );
|
2019-02-28 03:28:07 +00:00
|
|
|
break;
|
2019-02-28 03:58:08 +00:00
|
|
|
case 'payment':
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_payments' ), 1 );
|
2019-02-28 03:58:08 +00:00
|
|
|
break;
|
2019-02-28 06:22:13 +00:00
|
|
|
case 'shipping':
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_shipping' ), 1 );
|
2019-02-28 06:22:13 +00:00
|
|
|
break;
|
2019-02-28 06:27:05 +00:00
|
|
|
case 'recommended':
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_recommended' ), 1 );
|
2019-02-28 06:27:05 +00:00
|
|
|
break;
|
2019-02-28 06:33:00 +00:00
|
|
|
case 'activate':
|
2019-03-05 00:30:01 +00:00
|
|
|
add_action( 'admin_init', array( $this, 'track_jetpack_activate' ), 1 );
|
2019-02-28 06:33:00 +00:00
|
|
|
break;
|
2019-02-28 03:28:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Track store setup and store properties on save.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_store_setup() {
|
2019-12-13 19:58:14 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput
|
2019-02-28 03:28:07 +00:00
|
|
|
$properties = array(
|
|
|
|
'country' => isset( $_POST['store_country'] ) ? sanitize_text_field( $_POST['store_country'] ) : '',
|
|
|
|
'currency_code' => isset( $_POST['currency_code'] ) ? sanitize_text_field( $_POST['currency_code'] ) : '',
|
|
|
|
'product_type' => isset( $_POST['product_type'] ) ? sanitize_text_field( $_POST['product_type'] ) : '',
|
|
|
|
'sell_in_person' => isset( $_POST['sell_in_person'] ) && ( 'yes' === sanitize_text_field( $_POST['sell_in_person'] ) ),
|
|
|
|
);
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'obw_store_setup', $properties );
|
|
|
|
}
|
2019-02-28 03:58:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Track payment gateways selected.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_payments() {
|
2019-02-28 03:58:08 +00:00
|
|
|
$selected_gateways = array();
|
|
|
|
$created_accounts = array();
|
|
|
|
$wc_admin_setup_wizard = new WC_Admin_Setup_Wizard();
|
|
|
|
$gateways = array_merge( $wc_admin_setup_wizard->get_wizard_in_cart_payment_gateways(), $wc_admin_setup_wizard->get_wizard_manual_payment_gateways() );
|
|
|
|
|
|
|
|
foreach ( $gateways as $gateway_id => $gateway ) {
|
|
|
|
if ( ! empty( $_POST[ 'wc-wizard-service-' . $gateway_id . '-enabled' ] ) ) { // WPCS: CSRF ok, input var ok.
|
|
|
|
$selected_gateways[] = $gateway_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stripe account being created.
|
|
|
|
if (
|
|
|
|
! empty( $_POST['wc-wizard-service-stripe-enabled'] ) && // WPCS: CSRF ok, input var ok.
|
|
|
|
! empty( $_POST['stripe_create_account'] ) // WPCS: CSRF ok, input var ok.
|
|
|
|
) {
|
|
|
|
$created_accounts[] = 'stripe';
|
|
|
|
}
|
|
|
|
// PayPal account being created.
|
|
|
|
if (
|
|
|
|
! empty( $_POST['wc-wizard-service-ppec_paypal-enabled'] ) && // WPCS: CSRF ok, input var ok.
|
|
|
|
! empty( $_POST['ppec_paypal_reroute_requests'] ) // WPCS: CSRF ok, input var ok.
|
|
|
|
) {
|
|
|
|
$created_accounts[] = 'ppec_paypal';
|
|
|
|
}
|
|
|
|
|
|
|
|
$properties = array(
|
|
|
|
'selected_gateways' => implode( ', ', $selected_gateways ),
|
|
|
|
'created_accounts' => implode( ', ', $created_accounts ),
|
|
|
|
);
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'obw_payments', $properties );
|
|
|
|
}
|
2019-02-28 06:22:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Track shipping units and whether or not labels are set.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_shipping() {
|
2019-12-13 19:58:14 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput
|
2019-02-28 06:22:13 +00:00
|
|
|
$properties = array(
|
|
|
|
'weight_unit' => isset( $_POST['weight_unit'] ) ? sanitize_text_field( wp_unslash( $_POST['weight_unit'] ) ) : '',
|
|
|
|
'dimension_unit' => isset( $_POST['dimension_unit'] ) ? sanitize_text_field( wp_unslash( $_POST['dimension_unit'] ) ) : '',
|
|
|
|
'setup_wcs_labels' => isset( $_POST['setup_woocommerce_services'] ) && 'yes' === $_POST['setup_woocommerce_services'],
|
|
|
|
'setup_shipstation' => isset( $_POST['setup_shipstation'] ) && 'yes' === $_POST['setup_shipstation'],
|
|
|
|
);
|
|
|
|
// phpcs:enable
|
|
|
|
|
|
|
|
WC_Tracks::record_event( 'obw_shipping', $properties );
|
|
|
|
}
|
|
|
|
|
2019-02-28 06:27:05 +00:00
|
|
|
/**
|
|
|
|
* Track recommended plugins selected for install.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_recommended() {
|
2019-12-13 19:58:14 +00:00
|
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
2019-02-28 06:27:05 +00:00
|
|
|
$properties = array(
|
|
|
|
'setup_storefront' => isset( $_POST['setup_storefront_theme'] ) && 'yes' === $_POST['setup_storefront_theme'],
|
|
|
|
'setup_automated_tax' => isset( $_POST['setup_automated_taxes'] ) && 'yes' === $_POST['setup_automated_taxes'],
|
|
|
|
'setup_mailchimp' => isset( $_POST['setup_mailchimp'] ) && 'yes' === $_POST['setup_mailchimp'],
|
2019-12-20 16:51:26 +00:00
|
|
|
'setup_facebook' => isset( $_POST['setup_facebook'] ) && 'yes' === $_POST['setup_facebook'],
|
|
|
|
'setup_wc_admin' => isset( $_POST['setup_wc_admin'] ) && 'yes' === $_POST['setup_wc_admin'],
|
2019-02-28 06:27:05 +00:00
|
|
|
);
|
|
|
|
// phpcs:enable
|
2019-02-28 06:22:13 +00:00
|
|
|
|
2019-02-28 06:27:05 +00:00
|
|
|
WC_Tracks::record_event( 'obw_recommended', $properties );
|
|
|
|
}
|
2019-02-28 06:33:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Tracks when Jetpack is activated through the OBW.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_jetpack_activate() {
|
2019-02-28 06:33:00 +00:00
|
|
|
WC_Tracks::record_event( 'obw_activate' );
|
|
|
|
}
|
2019-03-01 02:58:11 +00:00
|
|
|
|
2019-07-02 21:56:08 +00:00
|
|
|
/**
|
|
|
|
* Tracks when last next_steps screen is viewed in the OBW.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function track_next_steps() {
|
|
|
|
WC_Tracks::record_event( 'obw_ready_view' );
|
|
|
|
}
|
|
|
|
|
2019-03-01 02:58:11 +00:00
|
|
|
/**
|
|
|
|
* Track skipped steps.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function track_skip_step() {
|
2019-03-01 02:58:11 +00:00
|
|
|
$previous_step = get_option( 'woocommerce_obw_last_completed_step' );
|
2019-03-05 00:30:01 +00:00
|
|
|
$current_step = $this->get_current_step();
|
2019-03-01 02:58:11 +00:00
|
|
|
if ( ! $previous_step || ! $current_step ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-05 00:30:01 +00:00
|
|
|
$steps = array_keys( $this->steps );
|
|
|
|
$current_step_index = array_search( $current_step, $steps, true );
|
|
|
|
$previous_step_index = array_search( $previous_step, $steps, true );
|
2019-03-01 02:58:11 +00:00
|
|
|
|
|
|
|
// If we're going forward more than 1 completed step.
|
|
|
|
if ( $current_step_index > $previous_step_index + 1 ) {
|
|
|
|
$properties = array(
|
|
|
|
'step' => $steps[ $current_step_index - 1 ],
|
|
|
|
);
|
|
|
|
WC_Tracks::record_event( 'obw_skip_step', $properties );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the OBW steps inside this class instance.
|
|
|
|
*
|
|
|
|
* @param array $steps Array of OBW steps.
|
|
|
|
*/
|
2019-03-05 00:30:01 +00:00
|
|
|
public function set_obw_steps( $steps ) {
|
|
|
|
$this->steps = $steps;
|
2019-03-01 02:58:11 +00:00
|
|
|
|
|
|
|
return $steps;
|
|
|
|
}
|
2019-02-28 03:28:07 +00:00
|
|
|
}
|