diff --git a/includes/tracks/class-wc-site-tracking.php b/includes/tracks/class-wc-site-tracking.php index aa53aa2d2f8..02464ad2232 100644 --- a/includes/tracks/class-wc-site-tracking.php +++ b/includes/tracks/class-wc-site-tracking.php @@ -113,6 +113,8 @@ class WC_Site_Tracking { include_once WC_ABSPATH . 'includes/tracks/events/class-wc-settings-tracking.php'; include_once WC_ABSPATH . 'includes/tracks/events/class-wc-status-tracking.php'; include_once WC_ABSPATH . 'includes/tracks/events/class-wc-coupons-tracking.php'; + include_once WC_ABSPATH . 'includes/tracks/events/class-wc-order-tracking.php'; + include_once WC_ABSPATH . 'includes/tracks/events/class-wc-coupon-tracking.php'; $tracking_classes = array( 'WC_Admin_Setup_Wizard_Tracking', @@ -123,6 +125,8 @@ class WC_Site_Tracking { 'WC_Settings_Tracking', 'WC_Status_Tracking', 'WC_Coupons_Tracking', + 'WC_Order_Tracking', + 'WC_Coupon_Tracking', ); foreach ( $tracking_classes as $tracking_class ) { diff --git a/includes/tracks/events/class-wc-coupon-tracking.php b/includes/tracks/events/class-wc-coupon-tracking.php new file mode 100644 index 00000000000..28ed1fa1b7e --- /dev/null +++ b/includes/tracks/events/class-wc-coupon-tracking.php @@ -0,0 +1,38 @@ + $coupon->get_code(), + 'free_shipping' => $coupon->get_free_shipping(), + 'individual_use' => $coupon->get_individual_use(), + 'exclude_sale_items' => $coupon->get_exclude_sale_items(), + 'usage_limits_applied' => 0 < intval( $coupon->get_usage_limit() ) + || 0 < intval( $coupon->get_usage_limit_per_user() ) + || 0 < intval( $coupon->get_limit_usage_to_x_items() ), + ); + + WC_Tracks::record_event( 'coupon_updated', $properties ); + } +} diff --git a/includes/tracks/events/class-wc-order-tracking.php b/includes/tracks/events/class-wc-order-tracking.php new file mode 100644 index 00000000000..7e02a398989 --- /dev/null +++ b/includes/tracks/events/class-wc-order-tracking.php @@ -0,0 +1,36 @@ + $order->get_status(), + 'date_created' => $order->get_date_created()->format( DateTime::ATOM ), + 'payment_method' => $order->get_payment_method(), + ); + + WC_Tracks::record_event( 'single_order_view', $properties ); + } +} + diff --git a/includes/tracks/events/class-wc-products-tracking.php b/includes/tracks/events/class-wc-products-tracking.php index 07545e4d433..bb9639ba58e 100644 --- a/includes/tracks/events/class-wc-products-tracking.php +++ b/includes/tracks/events/class-wc-products-tracking.php @@ -20,13 +20,14 @@ class WC_Products_Tracking { add_action( 'edit_post', array( $this, 'track_product_updated' ), 10, 2 ); add_action( 'transition_post_status', array( $this, 'track_product_published' ), 10, 3 ); add_action( 'created_product_cat', array( $this, 'track_product_category_created' ) ); + add_action( 'add_meta_boxes_product', array( $this, 'track_product_updated_client_side' ), 10, 1 ); } /** * Send a Tracks event when a product is updated. * * @param int $product_id Product id. - * @param object $post WordPress post. + * @param object $post WordPress post. */ public function track_product_updated( $product_id, $post ) { if ( 'product' !== $post->post_type ) { @@ -40,6 +41,42 @@ class WC_Products_Tracking { WC_Tracks::record_event( 'product_edit', $properties ); } + /** + * Track the Update button being clicked on the client side. + * This is needed because `track_product_updated` (using the `edit_post` + * hook) is called in response to a number of other triggers. + * + * @param WP_Post $post The post, not used. + */ + public function track_product_updated_client_side( $post ) { + wc_enqueue_js( + " + if ( $( 'h1.wp-heading-inline' ).text().trim() === 'Edit product') { + var initialStockValue = $( '#_stock' ).val(); + var hasRecordedEvent = false; + + $( '#publish' ).click( function() { + if ( hasRecordedEvent ) { + return; + } + + var currentStockValue = $( '#_stock' ).val(); + var properties = { + product_type: $( '#product-type' ).val(), + is_virtual: $( '#_virtual' ).is( ':checked' ) ? 'Y' : 'N', + is_downloadable: $( '#_downloadable' ).is( ':checked' ) ? 'Y' : 'N', + manage_stock: $( '#_manage_stock' ).is( ':checked' ) ? 'Y' : 'N', + stock_quantity_update: ( initialStockValue != currentStockValue ) ? 'Y' : 'N', + }; + + window.wcTracks.recordEvent( 'product_update', properties ); + hasRecordedEvent = true; + } ); + } + " + ); + } + /** * Send a Tracks event when a product is published. *