Track when a coupon is created/updated

This commit is contained in:
Rebecca Scott 2020-03-24 11:18:46 +10:00
parent 9bdeddfffc
commit d465ae0a79
2 changed files with 40 additions and 0 deletions

View File

@ -114,6 +114,7 @@ class WC_Site_Tracking {
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',
@ -125,6 +126,7 @@ class WC_Site_Tracking {
'WC_Status_Tracking',
'WC_Coupons_Tracking',
'WC_Order_Tracking',
'WC_Coupon_Tracking',
);
foreach ( $tracking_classes as $tracking_class ) {

View File

@ -0,0 +1,38 @@
<?php
/**
* WooCommerce Coupon Tracking
*
* @package WooCommerce\Tracks
*/
/**
* This class adds actions to track usage of a WooCommerce Coupon.
*/
class WC_Coupon_Tracking {
/**
* Init
*/
public function init() {
add_action( 'woocommerce_coupon_object_updated_props', array( $this, 'track_coupon_created' ), 10, 3 );
}
/**
* Send a Tracks event when a coupon is updated.
*
* @param WC_Coupon $coupon The coupon that has been updated.
* @param Array $updated_props The props of the coupon that have been updated.
*/
public function track_coupon_updated( $coupon, $updated_props ) {
$properties = array(
'discount_code' => $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_add', $properties );
}
}