@@ -65,6 +65,7 @@ function ( Pointer $pointer ) use ( $hook_suffix ) {
6565 // Dashboard styles are required where pointers are used to ensure proper styling.
6666 wp_enqueue_style ( 'googlesitekit-wp-dashboard-css ' );
6767 wp_enqueue_script ( 'wp-pointer ' );
68+ wp_enqueue_script ( 'googlesitekit-admin-pointers-tracking ' );
6869
6970 add_action (
7071 'admin_print_footer_scripts ' ,
@@ -121,7 +122,10 @@ private function print_pointer_script( $pointer ) {
121122 $ content .= '<div class="googlesitekit-pointer-buttons"> ' . $ buttons . '</div> ' ;
122123 }
123124
124- $ class = array ( 'wp-pointer ' );
125+ $ class = array ( 'wp-pointer ' );
126+ $ slug_class = sanitize_html_class ( $ pointer ->get_slug () );
127+ $ class [] = $ slug_class ;
128+
125129 if ( $ pointer ->get_class () ) {
126130 $ class [] = $ pointer ->get_class ();
127131 }
@@ -156,10 +160,28 @@ private function print_pointer_script( $pointer ) {
156160 'div ' => array ( 'class ' => array () ),
157161 );
158162
163+ $ tracking = $ pointer ->get_tracking ();
164+
165+ $ data = array (
166+ 'data-slug ' => $ pointer ->get_slug (),
167+ 'data-class ' => implode ( ' ' , $ class ),
168+ 'data-target-id ' => $ pointer ->get_target_id (),
169+ 'data-title ' => wp_kses ( $ pointer ->get_title (), $ kses_title ),
170+ 'data-content ' => wp_kses ( $ content , $ kses_content ),
171+ 'data-position ' => wp_json_encode ( $ pointer ->get_position () ),
172+ );
173+
174+ if ( ! empty ( $ tracking ) ) {
175+ $ data ['data-tracking ' ] = wp_json_encode ( $ tracking );
176+ }
177+
159178 BC_Functions::wp_print_inline_script_tag (
160179 <<<'JS'
161180 (
162181 function ( $, wp, config ) {
182+ const tracking = config.tracking ? JSON.parse( config.tracking ) : null;
183+ let trackingHandlers = null;
184+
163185 function initPointer() {
164186 const options = {
165187 content: '<h3>' + config.title + '</h3>' + config.content,
@@ -168,6 +190,9 @@ function initPointer() {
168190 pointerClass: config.class,
169191 close: function() {
170192 wp.ajax.post( 'dismiss-wp-pointer', { pointer: config.slug } );
193+ if ( trackingHandlers && trackingHandlers.onDismiss ) {
194+ trackingHandlers.onDismiss();
195+ }
171196 },
172197 buttons: function( event, container ) {
173198 container.pointer.on( 'click', '[data-action="dismiss"]', function() {
@@ -176,22 +201,27 @@ function initPointer() {
176201 }
177202 };
178203
179- $( '#' + config.targetId ).pointer( options ).pointer( 'open' );
204+ const target = $( '#' + config.targetId );
205+ if ( ! target.length ) {
206+ return;
207+ }
208+
209+ target.pointer( options ).pointer( 'open' );
210+
211+ if ( tracking && window.googlesitekitAdminPointersTracking ) {
212+ trackingHandlers = window.googlesitekitAdminPointersTracking.register(
213+ config.slug,
214+ tracking
215+ );
216+ }
180217 }
181218
182219 $( initPointer );
183220 }
184221 )( window.jQuery, window.wp, { ...document.currentScript.dataset } );
185222 JS
186223 ,
187- array (
188- 'data-slug ' => $ pointer ->get_slug (),
189- 'data-class ' => implode ( ' ' , $ class ),
190- 'data-target-id ' => $ pointer ->get_target_id (),
191- 'data-title ' => wp_kses ( $ pointer ->get_title (), $ kses_title ),
192- 'data-content ' => wp_kses ( $ content , $ kses_content ),
193- 'data-position ' => wp_json_encode ( $ pointer ->get_position () ),
194- )
224+ $ data
195225 );
196226 }
197227}
0 commit comments