Automattic\WooCommerce\Internal\Admin

CustomerEffortScoreTracks::get_script_track_edit_php()privateWC 1.0

Returns a generated script for tracking tags added on edit-tags.php page. CES survey is triggered via direct access to wc/customer-effort-score store via wp.data.dispatch method.

Due to lack of options to directly hook ourselves into the ajax post request initiated by edit-tags.php page, we infer a successful request by observing an increase of the number of rows in tags table

Метод класса: CustomerEffortScoreTracks{}

No Hooks.

Return

String. Generated JavaScript to append to page.

Usage

// private - for code of main (parent) class only
$result = $this->get_script_track_edit_php( $action, $label );
$action(string) (required)
Action name for the survey.
$label(string) (required)
Label for the snackbar.

Code of CustomerEffortScoreTracks::get_script_track_edit_php() WC 7.1.0

private function get_script_track_edit_php( $action, $label ) {
	return sprintf(
		"(function( $ ) {
			'use strict';
			// Hook on submit button and sets a 500ms interval function
			// to determine successful add tag or otherwise.
			$('#addtag #submit').on( 'click', function() {
				const initialCount = $('.tags tbody > tr').length;
				const interval = setInterval( function() {
					if ( $('.tags tbody > tr').length > initialCount ) {
						// New tag detected.
						clearInterval( interval );
						wp.data.dispatch('wc/customer-effort-score').addCesSurvey( '%s', '%s', window.pagenow, window.adminpage, '%s' );
					} else {
						// Form is no longer loading, most likely failed.
						if ( $( '#addtag .submit .spinner.is-active' ).length < 1 ) {
							clearInterval( interval );
						}
					}
				}, 500 );
			});
		})( jQuery );",
		esc_js( $action ),
		esc_js( $label ),
		esc_js( $this->onsubmit_label )
	);
}