Skip to content

Commit

Permalink
Widgets: Record events for the Simple Payments widget (#9803)
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons authored and Rodrigo Iloro committed Jun 26, 2018
1 parent aefdf50 commit bdfe618
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions modules/widgets/simple-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ public function ajax_save_payment_button() {
wp_send_json_error( $product_post_id );
}

$tracks_properties = array(
'id' => $product_post_id,
'currency' => $params['currency'],
'price' => $params['price']
);
if ( 0 === $product_post['ID'] ) {
$this->record_event( 'created', 'create', $tracks_properties );
} else {
$this->record_event( 'updated', 'update', $tracks_properties );
}

wp_send_json_success( [
'product_post_id' => $product_post_id,
'product_post_title' => $params['post_title'],
Expand Down Expand Up @@ -208,6 +219,8 @@ public function ajax_delete_payment_button() {
$return['status'] = 'deleted';
}

$this->record_event( 'deleted', 'delete', array( 'id' => $product_id ) );

wp_send_json_success( $return );
}

Expand Down Expand Up @@ -329,6 +342,32 @@ private function get_product_from_post( $product_post_id ) {
return $this->defaults();
}

/**
* Record a Track event and bump a MC stat.
*
* @param string $stat_name
* @param string $event_action
* @param array $event_properties
*/
private function record_event( $stat_name, $event_action, $event_properties = array() ) {
$current_user = wp_get_current_user();

// `bumps_stats_extra` only exists on .com
if ( function_exists( 'bump_stats_extras' ) ) {
require_lib( 'tracks/client' );
tracks_record_event( $current_user, 'simple_payments_button_' . $event_action, $event_properties );
/** This action is documented in modules/widgets/social-media-icons.php */
do_action( 'jetpack_bump_stats_extra', 'jetpack-simple_payments', $stat_name );
return;
}

jetpack_tracks_record_event( $current_user, 'jetpack_wpa_simple_payments_button_' . $event_action, $event_properties );
$jetpack = Jetpack::init();
// $jetpack->stat automatically prepends the stat group with 'jetpack-'
$jetpack->stat( 'simple_payments', $stat_name ) ;
$jetpack->do_stats( 'server_side' );
}

/**
* Sanitize widget form values as they are saved.
*
Expand Down

0 comments on commit bdfe618

Please sign in to comment.