From e30708c5bfa8c794b2b594841953095f7a7d089e Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Fri, 1 Jul 2022 11:31:51 +0200 Subject: [PATCH 1/6] Save post segment tracking. --- inc/telemetry/namespace.php | 45 +++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index 804a280..68ab450 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -511,17 +511,58 @@ function track_new_or_updated_content( $post_id, $post, $update ) { return; } + // Set the event value. if ( $update ) { $action = 'update'; } else { $action = 'create'; } + // Set the visibility value. + if ( 'private' === $post->post_status ) { + $visibility = 'Private'; + } elseif ( ! empty( $post->post_password ) ) { + $visibility = 'Protected'; + } else { + $visibility = 'Public'; + } + + // Set the tags value. + $posttags = get_the_tags( $post->ID ); + $post_tags = []; + if ( $posttags ) { + foreach ( $posttags as $tag ) { + $post_tags[] = $tag->name; + } + $post_tags = implode( ', ', $post_tags ); + } + + // Set the blocks value. + $block_counts = ''; + $blocks = parse_blocks( $post->post_content ); + $count = []; + foreach ( $blocks as $block ) { + if ( isset( $block['blockName'] ) ){ + $count[] = $block['blockName']; + } + } + $block_counts = json_encode( array_count_values( $count ), JSON_UNESCAPED_SLASHES ); + + + // Set the image count. + $image_count = substr_count( $post->post_content, ' 'Content', + 'event' => $action, 'properties' => [ + 'status' => $post->post_status, 'content_type' => $post->post_type, - 'content_action' => $action, + 'visibility' => $visibility, + 'scheduled' => ( $post->post_status === 'future' ? 'true' : 'false' ), + 'tags' => ( !empty( $post_tags ) ? $post_tags : "" ), + 'blocks' => $block_counts, + 'images' => $image_count, ], ] ); } From ab7e0d4ed5dd1d1db6811cd2ee63f37df152f852 Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Fri, 1 Jul 2022 12:18:50 +0200 Subject: [PATCH 2/6] add event tracking for previewing a post. --- inc/telemetry/namespace.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index 68ab450..d5054af 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -41,6 +41,9 @@ function bootstrap() { // Default event tracking. add_action( 'save_post', __NAMESPACE__ . '\\track_new_or_updated_content', 10, 3 ); + // Preview event Tracking. + add_action( 'wp_head', __NAMESPACE__ . '\\track_preview' ); + // Allow action hook for tracking, this makes it easy to track events in other code // without having a direct dependency on this module. add_action( 'altis.telemetry.track', __NAMESPACE__ . '\\track' ); @@ -566,3 +569,12 @@ function track_new_or_updated_content( $post_id, $post, $update ) { ], ] ); } + +function track_preview () { + if ( ! is_preview() ){ + return; + } + track( [ + 'event' => 'Preview', + ] ); +} \ No newline at end of file From 77c978033a092e1cd2409e658ee68ad801bab6e1 Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Mon, 4 Jul 2022 08:50:30 +0200 Subject: [PATCH 3/6] Event tracking for post initiated. --- inc/telemetry/namespace.php | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index d5054af..8f6b534 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -15,7 +15,7 @@ const ACTION_OPT_IN = 'altis_tracking_opt_in'; const META_OPT_IN = 'altis_tracking_opt_in'; -const SEGMENT_ID = 'GHqd7Vfs060yZBWOEGV4ajz3S3QHYKhk'; +const SEGMENT_ID = 'We14ttAnGx0IFjNhA0knvNPiga2IRZhE'; /** * Register hooks. @@ -29,6 +29,7 @@ function bootstrap() { } add_action( 'admin_init', __NAMESPACE__ . '\\handle_opt_in_form' ); + add_action( 'admin_init', __NAMESPACE__ . '\\track_posts_initiated' ); add_action( 'admin_head', __NAMESPACE__ . '\\load_segment_js' ); add_action( 'admin_footer', __NAMESPACE__ . '\\render_identity_tag' ); add_action( 'in_admin_header', __NAMESPACE__ . '\\render_opt_in_form' ); @@ -510,7 +511,7 @@ function handle_telemetry_endpoint( WP_REST_Request $request ) { */ function track_new_or_updated_content( $post_id, $post, $update ) { // Bail on auto-save. - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || 'auto-draft' === $post->post_status ) { return; } @@ -570,11 +571,33 @@ function track_new_or_updated_content( $post_id, $post, $update ) { ] ); } +/** + * Push post preview event to Segment.io. + * + * @return void + */ function track_preview () { if ( ! is_preview() ){ return; } track( [ - 'event' => 'Preview', + 'event' => 'preview', ] ); -} \ No newline at end of file +} + +/** + * Push post initiated to Segment.io. + * + * @return void + */ +function track_posts_initiated () { + global $pagenow; + if ( $pagenow === 'post-new.php' ) { + track( [ + 'event' => 'initiate_create', + 'properties' => [ + 'content_type' => ( isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post' ), + ], + ] ); + } +} From 54da117dd835f6973d930db5ac73d208e5acd266 Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Mon, 4 Jul 2022 08:51:49 +0200 Subject: [PATCH 4/6] Restore original SEGMENT_ID. --- inc/telemetry/namespace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index 8f6b534..79c7da3 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -15,7 +15,7 @@ const ACTION_OPT_IN = 'altis_tracking_opt_in'; const META_OPT_IN = 'altis_tracking_opt_in'; -const SEGMENT_ID = 'We14ttAnGx0IFjNhA0knvNPiga2IRZhE'; +const SEGMENT_ID = 'GHqd7Vfs060yZBWOEGV4ajz3S3QHYKhk'; /** * Register hooks. From 60dba47bc617a104c3df8f3b7323b118253f434f Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Mon, 4 Jul 2022 09:01:54 +0200 Subject: [PATCH 5/6] change to function name and ordering. --- inc/telemetry/namespace.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index 79c7da3..167a0f2 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -29,7 +29,6 @@ function bootstrap() { } add_action( 'admin_init', __NAMESPACE__ . '\\handle_opt_in_form' ); - add_action( 'admin_init', __NAMESPACE__ . '\\track_posts_initiated' ); add_action( 'admin_head', __NAMESPACE__ . '\\load_segment_js' ); add_action( 'admin_footer', __NAMESPACE__ . '\\render_identity_tag' ); add_action( 'in_admin_header', __NAMESPACE__ . '\\render_opt_in_form' ); @@ -39,12 +38,11 @@ function bootstrap() { add_action( 'rest_api_init', __NAMESPACE__ . '\\register_api_routes' ); - // Default event tracking. + // Segment.io event tracking. + add_action( 'admin_init', __NAMESPACE__ . '\\track_posts_initiated' ); + add_action( 'wp_head', __NAMESPACE__ . '\\track_posts_preview' ); add_action( 'save_post', __NAMESPACE__ . '\\track_new_or_updated_content', 10, 3 ); - // Preview event Tracking. - add_action( 'wp_head', __NAMESPACE__ . '\\track_preview' ); - // Allow action hook for tracking, this makes it easy to track events in other code // without having a direct dependency on this module. add_action( 'altis.telemetry.track', __NAMESPACE__ . '\\track' ); @@ -576,7 +574,7 @@ function track_new_or_updated_content( $post_id, $post, $update ) { * * @return void */ -function track_preview () { +function track_posts_preview () { if ( ! is_preview() ){ return; } From 6ad472753c71d0df3b7e7ab1bac1cc138d48d15c Mon Sep 17 00:00:00 2001 From: Robin Devitt Date: Mon, 18 Jul 2022 10:01:29 +0200 Subject: [PATCH 6/6] Change the if statement when to bail based on auto-draft status. --- inc/telemetry/namespace.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/inc/telemetry/namespace.php b/inc/telemetry/namespace.php index 167a0f2..93deca7 100644 --- a/inc/telemetry/namespace.php +++ b/inc/telemetry/namespace.php @@ -509,7 +509,12 @@ function handle_telemetry_endpoint( WP_REST_Request $request ) { */ function track_new_or_updated_content( $post_id, $post, $update ) { // Bail on auto-save. - if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || 'auto-draft' === $post->post_status ) { + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { + return; + } + + // Bail if the status is auto-draft. + if ( 'auto-draft' === $post->post_status ) { return; }