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

Method of the class: 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, $title, $first_question, $second_question );
$action(string) (required)
Action name for the survey.
$title(string) (required)
Title for the snackbar.
$first_question(string) (required)
The text for the first question.
$second_question(string) (required)
The text for the second question.

CustomerEffortScoreTracks::get_script_track_edit_php() code WC 8.6.1

private function get_script_track_edit_php( $action, $title, $first_question, $second_question ) {
	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({ action: '%s', title: '%s', firstQuestion: '%s', secondQuestion: '%s', onsubmitLabel: '%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( $title ),
		esc_js( $first_question ),
		esc_js( $second_question ),
		esc_js( $this->onsubmit_label )
	);
}