From 36e7a35ef76f07f7021fe509fcc2ec7ee7b0c342 Mon Sep 17 00:00:00 2001 From: Fernando Date: Thu, 15 Apr 2021 11:01:26 -0300 Subject: [PATCH] Updated event tracking for merchant email notes (https://github.com/woocommerce/woocommerce-admin/pull/6698) * Fixed user id unsetting * Fixed event name * Added cookie unsetting before recording This commit adds the cookie unsetting before recording and adds a small refactor to the merchant email notes event recording. * Added small refactor to the merchant email notes event recording * Renamed event `wcadmin_store_alert_action` This commit renames the event `wcadmin_store_alert_action ` to `store_alert_action ` * Removed check * Fixed method name * Changed "Notes" method to private The method "record_tracks_event_without_cookies" now is `private Co-authored-by: Fernando Marichal --- plugins/woocommerce-admin/src/API/Notes.php | 5 +- .../MerchantEmailNotifications.php | 4 -- .../NotificationEmail.php | 6 +- plugins/woocommerce-admin/src/Notes/Notes.php | 61 ++++++++++++++----- 4 files changed, 50 insertions(+), 26 deletions(-) diff --git a/plugins/woocommerce-admin/src/API/Notes.php b/plugins/woocommerce-admin/src/API/Notes.php index 553b228e279..5ef1f2c8b84 100644 --- a/plugins/woocommerce-admin/src/API/Notes.php +++ b/plugins/woocommerce-admin/src/API/Notes.php @@ -476,10 +476,7 @@ class Notes extends \WC_REST_CRUD_Controller { return; } - // We need to set the current user for tracking reasons. And unset user after tracking. - wp_set_current_user( $request->get_param( 'user_id' ) ); - wc_admin_record_tracks_event( 'wcadmin_email_note_opened', array( 'note_name' => $note->get_name() ) ); - wp_set_current_user( 0 ); + NotesRepository::record_tracks_event_with_user( $request->get_param( 'user_id' ), 'email_note_opened', array( 'note_name' => $note->get_name() ) ); } /** diff --git a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php index 73484f5cd91..2d75e353d46 100644 --- a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php +++ b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php @@ -53,11 +53,7 @@ class MerchantEmailNotifications { return; } - // We need to set the current user for tracking reasons. And unset user after tracking. - wp_set_current_user( $user_id ); Notes::trigger_note_action( $note, $triggered_action ); - wp_set_current_user( 0 ); - $url = $triggered_action->query; // We will use "wp_safe_redirect" when it's an internal redirect. diff --git a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php index 860af7151a5..1831e083389 100644 --- a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php +++ b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php @@ -5,7 +5,7 @@ namespace Automattic\WooCommerce\Admin\Notes\MerchantEmailNotifications; -use Automattic\WooCommerce\Admin\Notes; +use Automattic\WooCommerce\Admin\Notes\Notes; if ( ! defined( 'ABSPATH' ) ) { exit; @@ -216,8 +216,6 @@ class NotificationEmail extends \WC_Email { $this->get_headers(), $this->get_attachments() ); - wp_set_current_user( $user_id ); - wc_admin_record_tracks_event( 'wcadmin_email_note_sent', array( 'note_name' => $this->note->get_name() ) ); - wp_set_current_user( 0 ); + Notes::record_tracks_event_with_user( $user_id, 'email_note_sent', array( 'note_name' => $this->note->get_name() ) ); } } diff --git a/plugins/woocommerce-admin/src/Notes/Notes.php b/plugins/woocommerce-admin/src/Notes/Notes.php index b0b889becbf..20324a4a4a9 100644 --- a/plugins/woocommerce-admin/src/Notes/Notes.php +++ b/plugins/woocommerce-admin/src/Notes/Notes.php @@ -352,27 +352,60 @@ class Notes { $note->save(); + $event_params = array( + 'note_name' => $note->get_name(), + 'note_type' => $note->get_type(), + 'note_title' => $note->get_title(), + 'note_content' => $note->get_content(), + 'action_name' => $triggered_action->name, + 'action_label' => $triggered_action->label, + 'screen' => self::get_screen_name(), + ); + if ( in_array( $note->get_type(), array( 'error', 'update' ), true ) ) { - $tracks_event = 'wcadmin_store_alert_action'; + wc_admin_record_tracks_event( 'store_alert_action', $event_params ); } else { - $tracks_event = 'wcadmin_inbox_action_click'; + self::record_tracks_event_without_cookies( 'inbox_action_click', $event_params ); } - wc_admin_record_tracks_event( - $tracks_event, - array( - 'note_name' => $note->get_name(), - 'note_type' => $note->get_type(), - 'note_title' => $note->get_title(), - 'note_content' => $note->get_content(), - 'action_name' => $triggered_action->name, - 'action_label' => $triggered_action->label, - 'screen' => self::get_screen_name(), - ) - ); return $note; } + /** + * Record tracks event for a specific user. + * + * @param int $user_id The user id we want to record for the event. + * @param string $event_name Name of the event to record. + * @param array $params The params to send to the event recording. + */ + public static function record_tracks_event_with_user( $user_id, $event_name, $params ) { + // We save the current user id to set it back after the event recording. + $current_user_id = get_current_user_id(); + + wp_set_current_user( $user_id ); + self::record_tracks_event_without_cookies( $event_name, $params ); + wp_set_current_user( $current_user_id ); + + } + + /** + * Record tracks event without using cookies. + * + * @param string $event_name Name of the event to record. + * @param array $params The params to send to the event recording. + */ + private static function record_tracks_event_without_cookies( $event_name, $params ) { + // We save the cookie to set it back after the event recording. + // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized + $anon_id = isset( $_COOKIE['tk_ai'] ) ? $_COOKIE['tk_ai'] : null; + + unset( $_COOKIE['tk_ai'] ); + wc_admin_record_tracks_event( $event_name, $params ); + if ( isset( $anon_id ) ) { + setcookie( 'tk_ai', $anon_id ); + } + } + /** * Get screen name. *