Merge pull request #23883 from woocommerce/add/new-edit-order-tracks

Track more events on the edit order screen for future design validation.
This commit is contained in:
Gerhard Potgieter 2019-07-15 09:36:01 +02:00 committed by GitHub
commit 7c0d0251ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 258 additions and 22 deletions

View File

@ -1,6 +1,10 @@
/*global woocommerce_admin_meta_boxes, woocommerce_admin, accounting, woocommerce_admin_meta_boxes_order, wcSetClipboard, wcClearClipboard */
jQuery( function ( $ ) {
// Stand-in wcTracks.recordEvent in case tracks is not available (for any reason).
window.wcTracks = window.wcTracks || {};
window.wcTracks.recordEvent = window.wcTracks.recordEvent || function() { };
/**
* Order Data Panel
*/
@ -121,7 +125,8 @@ jQuery( function ( $ ) {
$edit_address = $wrapper.find( 'div.edit_address' ),
$address = $wrapper.find( 'div.address' ),
$country_input = $edit_address.find( '.js_field-country' ),
$state_input = $edit_address.find( '.js_field-state' );
$state_input = $edit_address.find( '.js_field-state' ),
is_billing = Boolean( $edit_address.find( 'input[name^="_billing_"]' ).length );
$address.hide();
$this.parent().find( 'a' ).toggle();
@ -132,6 +137,12 @@ jQuery( function ( $ ) {
}
$edit_address.show();
var event_name = is_billing ? 'order_edit_billing_address_click' : 'order_edit_shipping_address_click';
window.wcTracks.recordEvent( event_name, {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
},
change_customer_user: function() {
@ -251,6 +262,7 @@ jQuery( function ( $ ) {
.on( 'click', 'a.remove-coupon', this.remove_coupon )
.on( 'click', 'button.refund-items', this.refund_items )
.on( 'click', '.cancel-action', this.cancel )
.on( 'click', '.refund-actions .cancel-action', this.track_cancel )
.on( 'click', 'button.add-order-item', this.add_item )
.on( 'click', 'button.add-order-fee', this.add_fee )
.on( 'click', 'button.add-order-shipping', this.add_shipping )
@ -391,13 +403,29 @@ jQuery( function ( $ ) {
add_line_item: function() {
$( 'div.wc-order-add-item' ).slideDown();
$( 'div.wc-order-data-row-toggle' ).not( 'div.wc-order-add-item' ).slideUp();
window.wcTracks.recordEvent( 'order_edit_add_items_click', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
return false;
},
add_coupon: function() {
window.wcTracks.recordEvent( 'order_edit_add_coupon_click', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
var value = window.prompt( woocommerce_admin_meta_boxes.i18n_apply_coupon );
if ( value != null ) {
if ( null == value ) {
window.wcTracks.recordEvent( 'order_edit_add_coupon_cancel', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
} else {
wc_meta_boxes_order_items.block();
var user_id = $( '#customer_user' ).val();
@ -413,17 +441,28 @@ jQuery( function ( $ ) {
user_email : user_email
} );
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( '#woocommerce-order-items' ).find( '.inside' ).empty();
$( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html );
wc_meta_boxes_order_items.reloaded_items();
wc_meta_boxes_order_items.unblock();
} else {
window.alert( response.data.error );
$.ajax( {
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
if ( response.success ) {
$( '#woocommerce-order-items' ).find( '.inside' ).empty();
$( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html );
wc_meta_boxes_order_items.reloaded_items();
wc_meta_boxes_order_items.unblock();
} else {
window.alert( response.data.error );
}
wc_meta_boxes_order_items.unblock();
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_added_coupon', {
order_id: data.order_id,
status: $( '#order_status' ).val()
} );
}
wc_meta_boxes_order_items.unblock();
});
} );
}
return false;
},
@ -459,6 +498,12 @@ jQuery( function ( $ ) {
$( 'div.wc-order-totals-items' ).slideUp();
$( '#woocommerce-order-items' ).find( 'div.refund' ).show();
$( '.wc-order-edit-line-item .wc-order-edit-line-item-actions' ).hide();
window.wcTracks.recordEvent( 'order_edit_refund_button_click', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
return false;
},
@ -474,9 +519,21 @@ jQuery( function ( $ ) {
wc_meta_boxes_order_items.reload_items();
}
window.wcTracks.recordEvent( 'order_edit_add_items_cancelled', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
return false;
},
track_cancel: function() {
window.wcTracks.recordEvent( 'order_edit_refund_cancel', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
},
add_item: function() {
$( this ).WCBackboneModal({
template: 'wc-modal-add-products'
@ -486,9 +543,19 @@ jQuery( function ( $ ) {
},
add_fee: function() {
window.wcTracks.recordEvent( 'order_edit_add_fee_click', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
var value = window.prompt( woocommerce_admin_meta_boxes.i18n_add_fee );
if ( value != null ) {
if ( null == value ) {
window.wcTracks.recordEvent( 'order_edit_add_fee_cancel', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
} else {
wc_meta_boxes_order_items.block();
var data = $.extend( {}, wc_meta_boxes_order_items.get_taxable_address(), {
@ -505,6 +572,10 @@ jQuery( function ( $ ) {
$( '#woocommerce-order-items' ).find( '.inside' ).append( response.data.html );
wc_meta_boxes_order_items.reloaded_items();
wc_meta_boxes_order_items.unblock();
window.wcTracks.recordEvent( 'order_edit_added_fee', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
} else {
window.alert( response.data.error );
}
@ -527,6 +598,10 @@ jQuery( function ( $ ) {
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( response.success ) {
$( 'table.woocommerce_order_items tbody#order_shipping_line_items' ).append( response.data.html );
window.wcTracks.recordEvent( 'order_edit_add_shipping', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
} else {
window.alert( response.data.error );
}
@ -549,6 +624,10 @@ jQuery( function ( $ ) {
$( this ).hide();
$( 'button.add-line-item' ).click();
$( 'button.cancel-action' ).attr( 'data-reload', true );
window.wcTracks.recordEvent( 'order_edit_edit_item_click', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
return false;
},
@ -594,6 +673,12 @@ jQuery( function ( $ ) {
window.alert( response.data.error );
}
wc_meta_boxes_order_items.unblock();
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_remove_item', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
}
});
}
@ -625,8 +710,19 @@ jQuery( function ( $ ) {
window.alert( response.data.error );
}
wc_meta_boxes_order_items.unblock();
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_delete_tax', {
order_id: data.order_id,
status: $( '#order_status' ).val()
} );
}
});
} else {
window.wcTracks.recordEvent( 'order_edit_delete_tax_cancel', {
order_id: woocommerce_admin_meta_boxes.post_id,
status: $( '#order_status' ).val()
} );
}
return false;
},
@ -686,8 +782,20 @@ jQuery( function ( $ ) {
},
complete: function( response ) {
$( document.body ).trigger( 'order-totals-recalculate-complete', response );
window.wcTracks.recordEvent( 'order_edit_recalc_totals', {
order_id: data.post_id,
OK_cancel: 'OK',
status: $( '#order_status' ).val()
} );
}
});
} else {
window.wcTracks.recordEvent( 'order_edit_recalc_totals', {
order_id: woocommerce_admin_meta_boxes.post_id,
OK_cancel: 'cancel',
status: $( '#order_status' ).val()
} );
}
return false;
@ -724,6 +832,12 @@ jQuery( function ( $ ) {
wc_meta_boxes_order_items.unblock();
window.alert( response.data.error );
}
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_save_line_items', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
}
});
@ -787,16 +901,30 @@ jQuery( function ( $ ) {
security : woocommerce_admin_meta_boxes.order_item_nonce
};
$.post( woocommerce_admin_meta_boxes.ajax_url, data, function( response ) {
if ( true === response.success ) {
// Redirect to same page for show the refunded status
window.location.reload();
} else {
window.alert( response.data.error );
wc_meta_boxes_order_items.reload_items();
wc_meta_boxes_order_items.unblock();
$.ajax( {
url: woocommerce_admin_meta_boxes.ajax_url,
data: data,
type: 'POST',
success: function( response ) {
if ( true === response.success ) {
// Redirect to same page for show the refunded status
window.location.reload();
} else {
window.alert( response.data.error );
wc_meta_boxes_order_items.reload_items();
wc_meta_boxes_order_items.unblock();
}
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_refunded', {
order_id: data.order_id,
status: $( '#order_status' ).val(),
api_refund: data.api_refund,
has_reason: Boolean( data.refund_reason.length ),
restock: 'true' === data.restock_refunded_items
} );
}
});
} );
} else {
wc_meta_boxes_order_items.unblock();
}
@ -1032,6 +1160,12 @@ jQuery( function ( $ ) {
window.alert( response.data.error );
}
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_add_products', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
},
dataType: 'json'
});
},
@ -1074,6 +1208,12 @@ jQuery( function ( $ ) {
window.alert( response.data.error );
}
wc_meta_boxes_order_items.unblock();
},
complete: function() {
window.wcTracks.recordEvent( 'order_edit_add_tax', {
order_id: data.post_id,
status: $( '#order_status' ).val()
} );
}
});
} else {
@ -1134,6 +1274,11 @@ jQuery( function ( $ ) {
$( 'ul.order_notes' ).prepend( response );
$( '#woocommerce-order-notes' ).unblock();
$( '#add_order_note' ).val( '' );
window.wcTracks.recordEvent( 'order_edit_add_order_note', {
order_id: data.post_id,
note_type: data.note_type || 'private',
status: $( '#order_status' ).val()
} );
});
return false;

View File

@ -17,6 +17,10 @@ class WC_Orders_Tracking {
public function init() {
add_action( 'woocommerce_order_status_changed', array( $this, 'track_order_status_change' ), 10, 3 );
add_action( 'load-edit.php', array( $this, 'track_orders_view' ), 10 );
add_action( 'pre_post_update', array( $this, 'track_created_date_change' ), 10 );
// WC_Meta_Box_Order_Actions::save() hooks in at priority 50.
add_action( 'woocommerce_process_shop_order_meta', array( $this, 'track_order_action' ), 51 );
add_action( 'load-post-new.php', array( $this, 'track_add_order_from_edit' ), 10 );
}
/**
@ -51,8 +55,95 @@ class WC_Orders_Tracking {
'next_status' => $next_status,
'previous_status' => $previous_status,
'date_created' => $date->date( 'Y-m-d' ),
'payment_method' => $order->get_payment_method(),
);
WC_Tracks::record_event( 'orders_edit_status_change', $properties );
}
/**
* Send a Tracks event when an order date is changed.
*
* @param int $id Order id.
*/
public function track_created_date_change( $id ) {
$post_type = get_post_type( $id );
if ( 'shop_order' !== $post_type ) {
return;
}
$order = wc_get_order( $id );
$date_created = $order->get_date_created()->date( 'Y-m-d H:i:s' );
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
$new_date = sprintf(
'%s %2d:%2d:%2d',
isset( $_POST['order_date'] ) ? wc_clean( wp_unslash( $_POST['order_date'] ) ) : '',
isset( $_POST['order_date_hour'] ) ? wc_clean( wp_unslash( $_POST['order_date_hour'] ) ) : '',
isset( $_POST['order_date_minute'] ) ? wc_clean( wp_unslash( $_POST['order_date_minute'] ) ) : '',
isset( $_POST['order_date_second'] ) ? wc_clean( wp_unslash( $_POST['order_date_second'] ) ) : ''
);
// phpcs:enable
if ( $new_date !== $date_created ) {
$properties = array(
'order_id' => $id,
'status' => $order->get_status(),
);
WC_Tracks::record_event( 'order_edit_date_created', $properties );
}
}
/**
* Track order actions taken.
*
* @param int $order_id Order ID.
*/
public function track_order_action( $order_id ) {
// phpcs:disable WordPress.Security.NonceVerification.NoNonceVerification
if ( ! empty( $_POST['wc_order_action'] ) ) {
$order = wc_get_order( $order_id );
$action = wc_clean( wp_unslash( $_POST['wc_order_action'] ) );
$properties = array(
'order_id' => $order_id,
'status' => $order->get_status(),
'action' => $action,
);
WC_Tracks::record_event( 'order_edit_order_action', $properties );
}
// phpcs:enable
}
/**
* Track "add order" button on the Edit Order screen.
*/
public function track_add_order_from_edit() {
// phpcs:ignore WordPress.Security.NonceVerification.NoNonceVerification, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
if ( isset( $_GET['post_type'] ) && 'shop_order' === wp_unslash( $_GET['post_type'] ) ) {
$referer = wp_get_referer();
if ( $referer ) {
$referring_page = parse_url( $referer );
$referring_args = array();
$post_edit_page = parse_url( admin_url( 'post.php' ) );
if ( ! empty( $referring_page['query'] ) ) {
parse_str( $referring_page['query'], $referring_args );
}
// Determine if we arrived from an Order Edit screen.
if (
$post_edit_page['path'] === $referring_page['path'] &&
isset( $referring_args['action'] ) &&
'edit' === $referring_args['action'] &&
isset( $referring_args['post'] ) &&
'shop_order' === get_post_type( $referring_args['post'] )
) {
WC_Tracks::record_event( 'order_edit_add_order' );
}
}
}
}
}