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

Content changes to track in Altis #506

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
85 changes: 82 additions & 3 deletions inc/telemetry/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ 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 );

// Allow action hook for tracking, this makes it easy to track events in other code
Expand Down Expand Up @@ -511,17 +513,94 @@ function track_new_or_updated_content( $post_id, $post, $update ) {
return;
}

// Bail if the status is auto-draft.
if ( 'auto-draft' === $post->post_status ) {
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, '<img' );

// Segment tacking.
track( [
'event' => '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,
],
] );
}

/**
* Push post preview event to Segment.io.
*
* @return void
*/
function track_posts_preview () {
if ( ! is_preview() ){
return;
}
track( [
'event' => 'preview',
] );
}

/**
* 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' ),
],
] );
}
}