rebase on oct-10-2023
This commit is contained in:
@@ -71,10 +71,11 @@ class WC_Importer_Tracking {
|
||||
}
|
||||
|
||||
$properties = array(
|
||||
'imported' => isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0,
|
||||
'updated' => isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0,
|
||||
'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0,
|
||||
'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0,
|
||||
'imported' => isset( $_GET['products-imported'] ) ? absint( $_GET['products-imported'] ) : 0,
|
||||
'imported_variations' => isset( $_GET['products-imported-variations'] ) ? absint( $_GET['products-imported-variations'] ) : 0,
|
||||
'updated' => isset( $_GET['products-updated'] ) ? absint( $_GET['products-updated'] ) : 0,
|
||||
'failed' => isset( $_GET['products-failed'] ) ? absint( $_GET['products-failed'] ) : 0,
|
||||
'skipped' => isset( $_GET['products-skipped'] ) ? absint( $_GET['products-skipped'] ) : 0,
|
||||
);
|
||||
// phpcs:enable
|
||||
|
||||
|
||||
@@ -10,6 +10,8 @@ use Automattic\WooCommerce\Internal\Admin\WCAdminAssets;
|
||||
|
||||
defined( 'ABSPATH' ) || exit;
|
||||
|
||||
require_once WC_ABSPATH . 'includes/admin/wc-admin-functions.php';
|
||||
|
||||
/**
|
||||
* This class adds actions to track usage of WooCommerce Products.
|
||||
*/
|
||||
@@ -151,6 +153,27 @@ class WC_Products_Tracking {
|
||||
var tagsText = '';
|
||||
var currentStockValue = $( '#_stock' ).val();
|
||||
|
||||
function getProductTypeOptions() {
|
||||
const productTypeOptionsCheckboxes = $( 'input[type=\"checkbox\"][data-product-type-option-id]' );
|
||||
const productTypeOptions = productTypeOptionsCheckboxes.map( function() {
|
||||
return {
|
||||
id: $( this ).data( 'product-type-option-id' ),
|
||||
isEnabled: $( this ).is( ':checked' ),
|
||||
};
|
||||
} ).get();
|
||||
return productTypeOptions;
|
||||
}
|
||||
|
||||
function getProductTypeOptionsString( productTypeOptions ) {
|
||||
return productTypeOptions
|
||||
.filter( productTypeOption => productTypeOption.isEnabled )
|
||||
.map( productTypeOption => productTypeOption.id )
|
||||
.join( ', ' );
|
||||
}
|
||||
|
||||
const productTypeOptions = getProductTypeOptions();
|
||||
const productTypeOptionsString = getProductTypeOptionsString( productTypeOptions );
|
||||
|
||||
if ( ! isBlockEditor ) {
|
||||
tagsText = $( '[name=\"tax_input[product_tag]\"]' ).val();
|
||||
if ( $( '#content' ).is( ':visible' ) ) {
|
||||
@@ -174,26 +197,27 @@ class WC_Products_Tracking {
|
||||
} ).length;
|
||||
|
||||
var properties = {
|
||||
attributes: numberOfAttributes,
|
||||
categories: $( '[name=\"tax_input[product_cat][]\"]:checked' ).length,
|
||||
cross_sells: $( '#crosssell_ids option' ).length ? 'Yes' : 'No',
|
||||
description: description_value.trim() !== '' ? 'Yes' : 'No',
|
||||
enable_reviews: $( '#comment_status' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
is_virtual: $( '#_virtual' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
is_block_editor: isBlockEditor,
|
||||
is_downloadable: $( '#_downloadable' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
manage_stock: $( '#_manage_stock' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
menu_order: parseInt( $( '#menu_order' ).val(), 10 ) !== 0 ? 'Yes' : 'No',
|
||||
product_gallery: $( '#product_images_container .product_images > li' ).length,
|
||||
product_image: $( '#_thumbnail_id' ).val() > 0 ? 'Yes' : 'No',
|
||||
product_type: $( '#product-type' ).val(),
|
||||
purchase_note: $( '#_purchase_note' ).val().length ? 'yes' : 'no',
|
||||
sale_price: $( '#_sale_price' ).val() ? 'yes' : 'no',
|
||||
short_description: $( '#excerpt' ).val().length ? 'yes' : 'no',
|
||||
stock_quantity_update: ( initialStockValue != currentStockValue ) ? 'Yes' : 'No',
|
||||
tags: tagsText.length > 0 ? tagsText.split( ',' ).length : 0,
|
||||
upsells: $( '#upsell_ids option' ).length ? 'Yes' : 'No',
|
||||
weight: $( '#_weight' ).val() ? 'Yes' : 'No',
|
||||
attributes: numberOfAttributes,
|
||||
categories: $( '[name=\"tax_input[product_cat][]\"]:checked' ).length,
|
||||
cross_sells: $( '#crosssell_ids option' ).length ? 'Yes' : 'No',
|
||||
description: description_value.trim() !== '' ? 'Yes' : 'No',
|
||||
enable_reviews: $( '#comment_status' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
is_virtual: $( '#_virtual' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
is_block_editor: isBlockEditor,
|
||||
is_downloadable: $( '#_downloadable' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
manage_stock: $( '#_manage_stock' ).is( ':checked' ) ? 'Yes' : 'No',
|
||||
menu_order: parseInt( $( '#menu_order' ).val(), 10 ) !== 0 ? 'Yes' : 'No',
|
||||
product_gallery: $( '#product_images_container .product_images > li' ).length,
|
||||
product_image: $( '#_thumbnail_id' ).val() > 0 ? 'Yes' : 'No',
|
||||
product_type: $( '#product-type' ).val(),
|
||||
product_type_options_string: productTypeOptionsString,
|
||||
purchase_note: $( '#_purchase_note' ).val().length ? 'yes' : 'no',
|
||||
sale_price: $( '#_sale_price' ).val() ? 'yes' : 'no',
|
||||
short_description: $( '#excerpt' ).val().length ? 'yes' : 'no',
|
||||
stock_quantity_update: ( initialStockValue != currentStockValue ) ? 'Yes' : 'No',
|
||||
tags: tagsText.length > 0 ? tagsText.split( ',' ).length : 0,
|
||||
upsells: $( '#upsell_ids option' ).length ? 'Yes' : 'No',
|
||||
weight: $( '#_weight' ).val() ? 'Yes' : 'No',
|
||||
};
|
||||
window.wcTracks.recordEvent( 'product_update', properties );
|
||||
} );
|
||||
@@ -202,6 +226,70 @@ class WC_Products_Tracking {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the IDs of the possible product type options.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_possible_product_type_options_ids() {
|
||||
$product_type_options_ids =
|
||||
array_values(
|
||||
array_map(
|
||||
function ( $product_type_option ) {
|
||||
return $product_type_option['id'];
|
||||
},
|
||||
/* phpcs:disable WooCommerce.Commenting.CommentHooks.MissingHookComment */
|
||||
apply_filters(
|
||||
'product_type_options',
|
||||
wc_get_default_product_type_options(),
|
||||
)
|
||||
/* phpcs: enable */
|
||||
)
|
||||
);
|
||||
|
||||
return $product_type_options_ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the product type options for a product.
|
||||
*
|
||||
* @param int $post_id The ID of the product.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function get_product_type_options( $post_id ) {
|
||||
$possible_product_type_options_ids = self::get_possible_product_type_options_ids();
|
||||
$post_meta = get_post_meta( $post_id );
|
||||
$product_type_options = array();
|
||||
|
||||
foreach ( $possible_product_type_options_ids as $product_type_option_id ) {
|
||||
$product_type_options[ $product_type_option_id ] = isset( $post_meta[ $product_type_option_id ] ) ? $post_meta[ $product_type_option_id ][0] : 'no';
|
||||
}
|
||||
|
||||
return $product_type_options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a comma-separated string of the product type options that are enabled.
|
||||
*
|
||||
* @param array $product_type_options The product type options.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private static function get_product_type_options_string( $product_type_options ) {
|
||||
return implode(
|
||||
', ',
|
||||
array_keys(
|
||||
array_filter(
|
||||
$product_type_options,
|
||||
function ( $is_enabled ) {
|
||||
return 'yes' === $is_enabled;
|
||||
}
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a Tracks event when a product is published.
|
||||
*
|
||||
@@ -222,27 +310,32 @@ class WC_Products_Tracking {
|
||||
|
||||
$product = wc_get_product( $post_id );
|
||||
|
||||
$product_type_options = self::get_product_type_options( $post_id );
|
||||
$product_type_options_string = self::get_product_type_options_string( $product_type_options );
|
||||
|
||||
$properties = array(
|
||||
'attributes' => count( $product->get_attributes() ),
|
||||
'categories' => count( $product->get_category_ids() ),
|
||||
'cross_sells' => ! empty( $product->get_cross_sell_ids() ) ? 'yes' : 'no',
|
||||
'description' => $product->get_description() ? 'yes' : 'no',
|
||||
'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ) !== 'N/A' ? 'yes' : 'no',
|
||||
'enable_reviews' => $product->get_reviews_allowed() ? 'yes' : 'no',
|
||||
'is_downloadable' => $product->is_downloadable() ? 'yes' : 'no',
|
||||
'is_virtual' => $product->is_virtual() ? 'yes' : 'no',
|
||||
'manage_stock' => $product->get_manage_stock() ? 'yes' : 'no',
|
||||
'menu_order' => $product->get_menu_order() ? 'yes' : 'no',
|
||||
'product_id' => $post_id,
|
||||
'product_gallery' => count( $product->get_gallery_image_ids() ),
|
||||
'product_image' => $product->get_image_id() ? 'yes' : 'no',
|
||||
'product_type' => $product->get_type(),
|
||||
'purchase_note' => $product->get_purchase_note() ? 'yes' : 'no',
|
||||
'sale_price' => $product->get_sale_price() ? 'yes' : 'no',
|
||||
'short_description' => $product->get_short_description() ? 'yes' : 'no',
|
||||
'tags' => count( $product->get_tag_ids() ),
|
||||
'upsells' => ! empty( $product->get_upsell_ids() ) ? 'yes' : 'no',
|
||||
'weight' => $product->get_weight() ? 'yes' : 'no',
|
||||
'attributes' => count( $product->get_attributes() ),
|
||||
'categories' => count( $product->get_category_ids() ),
|
||||
'cross_sells' => ! empty( $product->get_cross_sell_ids() ) ? 'yes' : 'no',
|
||||
'description' => $product->get_description() ? 'yes' : 'no',
|
||||
'dimensions' => wc_format_dimensions( $product->get_dimensions( false ) ) !== 'N/A' ? 'yes' : 'no',
|
||||
'enable_reviews' => $product->get_reviews_allowed() ? 'yes' : 'no',
|
||||
'is_downloadable' => $product->is_downloadable() ? 'yes' : 'no',
|
||||
'is_virtual' => $product->is_virtual() ? 'yes' : 'no',
|
||||
'manage_stock' => $product->get_manage_stock() ? 'yes' : 'no',
|
||||
'menu_order' => $product->get_menu_order() ? 'yes' : 'no',
|
||||
'product_id' => $post_id,
|
||||
'product_gallery' => count( $product->get_gallery_image_ids() ),
|
||||
'product_image' => $product->get_image_id() ? 'yes' : 'no',
|
||||
'product_type' => $product->get_type(),
|
||||
'product_type_options' => $product_type_options_string,
|
||||
'purchase_note' => $product->get_purchase_note() ? 'yes' : 'no',
|
||||
'sale_price' => $product->get_sale_price() ? 'yes' : 'no',
|
||||
'source' => apply_filters( 'woocommerce_product_source', '' ),
|
||||
'short_description' => $product->get_short_description() ? 'yes' : 'no',
|
||||
'tags' => count( $product->get_tag_ids() ),
|
||||
'upsells' => ! empty( $product->get_upsell_ids() ) ? 'yes' : 'no',
|
||||
'weight' => $product->get_weight() ? 'yes' : 'no',
|
||||
);
|
||||
|
||||
WC_Tracks::record_event( 'product_add_publish', $properties );
|
||||
|
||||
@@ -28,6 +28,36 @@ class WC_Settings_Tracking {
|
||||
*/
|
||||
protected $updated_options = array();
|
||||
|
||||
/**
|
||||
* List of option names that are dropdown menus.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $dropdown_menu_options = array();
|
||||
|
||||
|
||||
/**
|
||||
* List of options that have been modified.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $modified_options = array();
|
||||
|
||||
/**
|
||||
* List of options that have been deleted.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $deleted_options = array();
|
||||
|
||||
/**
|
||||
* List of options that have been added.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $added_options = array();
|
||||
|
||||
|
||||
/**
|
||||
* Toggled options.
|
||||
*
|
||||
@@ -44,10 +74,42 @@ class WC_Settings_Tracking {
|
||||
public function init() {
|
||||
add_action( 'woocommerce_settings_page_init', array( $this, 'track_settings_page_view' ) );
|
||||
add_action( 'woocommerce_update_option', array( $this, 'add_option_to_list' ) );
|
||||
add_action( 'woocommerce_update_non_option_setting', array( $this, 'add_option_to_list_and_track_setting_change' ) );
|
||||
add_action( 'woocommerce_update_options', array( $this, 'send_settings_change_event' ) );
|
||||
add_action( 'admin_enqueue_scripts', array( $this, 'possibly_add_settings_tracking_scripts' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the option to the allowed and updated options directly.
|
||||
* Currently used for settings that don't use update_option.
|
||||
*
|
||||
* @param array $option WooCommerce option that should be updated.
|
||||
*/
|
||||
public function add_option_to_list_and_track_setting_change( $option ) {
|
||||
if ( ! in_array( $option['id'], $this->allowed_options, true ) ) {
|
||||
$this->allowed_options[] = $option['id'];
|
||||
}
|
||||
if ( isset( $option['action'] ) ) {
|
||||
if ( 'add' === $option['action'] ) {
|
||||
$this->added_options[] = $option['id'];
|
||||
} elseif ( 'delete' === $option['action'] ) {
|
||||
$this->deleted_options[] = $option['id'];
|
||||
} elseif ( ! in_array( $option['id'], $this->updated_options, true ) ) {
|
||||
$this->updated_options[] = $option['id'];
|
||||
}
|
||||
} elseif ( isset( $option['value'] ) ) {
|
||||
if ( 'select' === $option['type'] ) {
|
||||
$this->modified_options[ $option['id'] ] = $option['value'];
|
||||
|
||||
} elseif ( 'checkbox' === $option['type'] ) {
|
||||
$option_state = 'yes' === $option['value'] ? 'enabled' : 'disabled';
|
||||
$this->toggled_options[ $option_state ][] = $option['id'];
|
||||
}
|
||||
$this->updated_options[] = $option['id'];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a WooCommerce option name to our allowed options list and attach
|
||||
* the `update_option` hook. Rather than inspecting every updated
|
||||
@@ -61,6 +123,10 @@ class WC_Settings_Tracking {
|
||||
public function add_option_to_list( $option ) {
|
||||
$this->allowed_options[] = $option['id'];
|
||||
|
||||
if ( isset( $option['options'] ) ) {
|
||||
$this->dropdown_menu_options[] = $option['id'];
|
||||
}
|
||||
|
||||
// Delay attaching this action since it could get fired a lot.
|
||||
if ( false === has_action( 'update_option', array( $this, 'track_setting_change' ) ) ) {
|
||||
add_action( 'update_option', array( $this, 'track_setting_change' ), 10, 3 );
|
||||
@@ -91,8 +157,10 @@ class WC_Settings_Tracking {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check and save toggled options.
|
||||
if ( in_array( $new_value, array( 'yes', 'no' ), true ) && in_array( $old_value, array( 'yes', 'no' ), true ) ) {
|
||||
if ( in_array( $option_name, $this->dropdown_menu_options, true ) ) {
|
||||
$this->modified_options[ $option_name ] = $new_value;
|
||||
} elseif ( in_array( $new_value, array( 'yes', 'no' ), true ) && in_array( $old_value, array( 'yes', 'no' ), true ) ) {
|
||||
// Save toggled options.
|
||||
$option_state = 'yes' === $new_value ? 'enabled' : 'disabled';
|
||||
$this->toggled_options[ $option_state ][] = $option_name;
|
||||
}
|
||||
@@ -106,13 +174,23 @@ class WC_Settings_Tracking {
|
||||
public function send_settings_change_event() {
|
||||
global $current_tab, $current_section;
|
||||
|
||||
if ( empty( $this->updated_options ) ) {
|
||||
if ( empty( $this->updated_options ) && empty( $this->deleted_options ) && empty( $this->added_options ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$properties = array(
|
||||
'settings' => implode( ',', $this->updated_options ),
|
||||
);
|
||||
$properties = array();
|
||||
|
||||
if ( ! empty( $this->updated_options ) ) {
|
||||
$properties['settings'] = implode( ',', $this->updated_options );
|
||||
}
|
||||
|
||||
if ( ! empty( $this->deleted_options ) ) {
|
||||
$properties['deleted'] = implode( ',', $this->deleted_options );
|
||||
}
|
||||
|
||||
if ( ! empty( $this->added_options ) ) {
|
||||
$properties['added'] = implode( ',', $this->added_options );
|
||||
}
|
||||
|
||||
foreach ( $this->toggled_options as $state => $options ) {
|
||||
if ( ! empty( $options ) ) {
|
||||
@@ -120,6 +198,12 @@ class WC_Settings_Tracking {
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $this->modified_options ) ) {
|
||||
foreach ( $this->modified_options as $option_name => $selected_option ) {
|
||||
$properties[ $option_name ] = $selected_option ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
$properties['tab'] = $current_tab ?? '';
|
||||
$properties['section'] = $current_section ?? '';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user