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 4 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
82 changes: 79 additions & 3 deletions inc/telemetry/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand All @@ -41,6 +42,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' );
Expand Down Expand Up @@ -507,21 +511,93 @@ 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;
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have had to include the auto-draft status here in the return as it seems to apply when a new post ( the exact post_type ) is created but not other post types. Not returning then fires off the tracking event. Essentially firing two tracking events when creating a new post.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would do this in a separate if statement. Read through the logic here, I don't think it will do what you in every scenario. What if DOING_AUTOSAVE is defined but false. You should never mix && operators directly with ||.


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