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 <contacto@fernandomarichal.com>
This commit is contained in:
Fernando 2021-04-15 11:01:26 -03:00 committed by GitHub
parent 664694e165
commit 36e7a35ef7
4 changed files with 50 additions and 26 deletions

View File

@ -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() ) );
}
/**

View File

@ -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.

View File

@ -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() ) );
}
}

View File

@ -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.
*