Skip to content

Instantly share code, notes, and snippets.

@radosinsky
Last active May 9, 2016 20:51
Show Gist options
  • Save radosinsky/2025d306c8fa4fed55268c8ac1147bdc to your computer and use it in GitHub Desktop.
Save radosinsky/2025d306c8fa4fed55268c8ac1147bdc to your computer and use it in GitHub Desktop.
<?php
if ( class_exists( 'Segment_Cookie' ) ) {
add_action( 'wp_insert_comment', 'gcy_segment_insert_comment', 10, 2 );
function gcy_segment_insert_comment( $id, $comment ) {
$commenter = wp_get_current_commenter();
if ( ! array_filter( $commenter ) ) {
$commenter = [
'comment_author' => $comment->comment_author,
'comment_author_email' => $comment->comment_author_email,
'comment_author_url' => $comment->comment_author_url,
];
}
Segment_Cookie::set_cookie( 'left_comment', json_encode( $commenter ) );
}
add_action( 'transition_post_status', 'gcy_segment_insert_post', 10, 3 );
function gcy_segment_insert_post( $new, $old, $post ) {
if ( $old == 'auto-draft' ) {
$post_type = 'updates';
if ( $post->post_status == 'auto-draft' ) {
return;
}
if ( wp_is_post_revision( $post->ID ) || wp_is_post_autosave( $post->ID ) ) {
return;
}
if ( $post->post_type != $post_type ) {
return;
}
Segment_Cookie::set_cookie( 'insert_post', json_encode( $post->ID ) );
}
}
add_filter( 'segment_get_current_page_track', 'gcy_segment_track_events', 10, 3 );
function gcy_segment_track_events( $track, $settings, $this ) {
if ( $settings['track_comments'] ) {
if ( Segment_Cookie::get_cookie( 'left_comment' ) ) {
$commenter = json_decode( stripslashes( Segment_Cookie::get_cookie( 'left_comment' ) ), true );
$track = array(
'event' => __( 'Commented', 'segment' ),
'properties' => array(
'author' => $commenter['comment_author'],
'email' => $commenter['comment_author_email'],
'url' => $commenter['comment_author_url'],
),
'http_event' => 'left_comment'
);
}
}
if ( Segment_Cookie::get_cookie( 'insert_post' ) ) {
$post_id = json_decode( stripslashes( Segment_Cookie::get_cookie( 'insert_post' ) ), true );
$post = get_post( $post_id );
if ( $post ) {
$categories = implode( ', ', wp_list_pluck( get_the_category( $post->ID ), 'name' ) );
$track = [
'event' => 'Post Submitted',
'properties' => [
'title' => $post->post_title,
'category' => $categories,
'status' => $post->post_status,
],
'http_event' => 'insert_post'
];
}
}
if ( $track ) {
$track['properties']['noninteraction'] = true;
$track['properties'] = array_filter( $track['properties'] );
}
return $track;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment