Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post list: Add track events for quick links and stats #40935

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: added
Comment: Adds tracking for post list quick links and post stats for WPCOM


Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public static function load_wpcom_user_features() {
require_once __DIR__ . '/features/help-center/class-help-center.php';
}
require_once __DIR__ . '/features/pages/pages.php';
require_once __DIR__ . '/features/posts/posts.php';
require_once __DIR__ . '/features/replace-site-visibility/replace-site-visibility.php';
require_once __DIR__ . '/features/wpcom-admin-bar/wpcom-admin-bar.php';
require_once __DIR__ . '/features/wpcom-admin-interface/wpcom-admin-interface.php';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Post list tracking.
*
* @package automattic/jetpack-mu-wpcom
*/

/**
* Add tracking for the post list quick links.
*
* @return void
*/
function wpcom_add_tracking_for_posts_lists() {
global $post_type;

?>
<script>
document.querySelectorAll( '.column-primary .row-actions span' ).forEach( ( span ) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this work if the user uses the Quick Edit to save a change and then uses a quick link?

span.addEventListener( 'click', (event) => {
let name = event.currentTarget.className;

// Classic editor sets the class to "0".
if ( '0' === name ) {
name = 'classic-editor';
}
mmtr marked this conversation as resolved.
Show resolved Hide resolved

// Handle Quick inline edit.
if ( 'inline hide-if-no-js' === name ) {
name = 'quick-edit';
}

const props = {
post_type: '<?php echo esc_html( $post_type ); ?>',
section: name,
}

window._tkq.push( [ 'recordEvent', 'wpcom_post_list_quick_links_clicked', props ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is window._tkq always guaranteed to exist? I always assumed we need to initialize just in case. See PCYsg-4KT-p2

window._tkq = window._tkq || [];

I think the user is not identified by default when using window._tkq directly, and you need to do it manually:

However, this library does not automatically populate the blog_id property, nor does it automatically identify the current user.

See this example for reference.

And now that I found that reference, maybe you can use the wpcomTrackEvent helper?

} );
} );
</script>

<?php
}

add_action( 'admin_print_footer_scripts-edit.php', 'wpcom_add_tracking_for_posts_lists' );

/**
* Adds event for stats clicks in the post list (for pages and post types).
*
* @return void
*/
function wpcom_post_list_add_stats_tracking() {
global $post_type;

if ( ! in_array( $post_type, array( 'post', 'page' ), true ) ) {
return;
}
?>
<script>
document.querySelectorAll( '#the-list .stats a' ).forEach( ( link ) => {
link.addEventListener( 'click', () => {
const props = {
post_type: '<?php echo esc_html( $post_type ); ?>',
}

window._tkq.push( [ 'recordEvent', 'wpcom_post_list_stats_clicked', props ] );
} );
} );
</script>
<?php
}

add_action( 'admin_print_footer_scripts-edit.php', 'wpcom_post_list_add_stats_tracking' );
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php // phpcs:ignore Squiz.Commenting.FileComment.Missing


require_once __DIR__ . '/post-list-tracking.php';
Loading