From 74d1afaded4eb0ff4b367d9c8e5eb120a9031583 Mon Sep 17 00:00:00 2001 From: Fernando Date: Mon, 29 Mar 2021 16:00:23 -0300 Subject: [PATCH] Fixed event tracking for merchant email notes (https://github.com/woocommerce/woocommerce-admin/pull/6616) * Modified MerchantEmailNotifications class This commit modifies the class "MerchantEmailNotifications" in order to set the current user before the events tracking # Conflicts: # src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php # src/Notes/MerchantEmailNotifications/NotificationEmail.php # tests/notes/class-wc-tests-email-notes.php * Added changelog # Conflicts: # readme.txt * Added testing instructions * Fixed changelog Co-authored-by: Fernando Marichal --- .../woocommerce-admin/TESTING-INSTRUCTIONS.md | 13 +++++++++++++ plugins/woocommerce-admin/readme.txt | 1 + plugins/woocommerce-admin/src/API/Notes.php | 6 +++++- .../MerchantEmailNotifications.php | 10 +++++++--- .../NotificationEmail.php | 16 +++++++++++----- 5 files changed, 37 insertions(+), 9 deletions(-) diff --git a/plugins/woocommerce-admin/TESTING-INSTRUCTIONS.md b/plugins/woocommerce-admin/TESTING-INSTRUCTIONS.md index da0fb8bcad0..0fe6a109581 100644 --- a/plugins/woocommerce-admin/TESTING-INSTRUCTIONS.md +++ b/plugins/woocommerce-admin/TESTING-INSTRUCTIONS.md @@ -4,6 +4,19 @@ ## 2.2.0 +### Fixed event tracking for merchant email notes #6616 + +- Create a brand new site. +- Install a plugin to log every sent email (you can use [WP mail logging](https://wordpress.org/plugins/wp-mail-logging/)). +- Install and active [this gist](https://gist.github.com/octaedro/864315edaf9c6a2a6de71d297be1ed88) to create an email note. Just download the file and install it as a plugin. +- After activating the plugin, press `Add Email Notes` to create a note. +- Now go to WooCommerce > Settings > Email (`/wp-admin/admin.php?page=wc-settings&tab=email`) and check the checkbox `Enable email insights` and save changes. +- You will need to run the cron so you can install a plugin like [WP Crontol](https://wordpress.org/plugins/wp-crontrol/) +- Go to Tools > Cron events (`/wp-admin/tools.php?page=crontrol_admin_manage_page`). +- Call the hook `wc_admin_daily` by pressing its `Run Now` link. (https://user-images.githubusercontent.com/1314156/111530634-4929ce80-8742-11eb-8b53-de936ceea76e.png) +- Go to Tools > WP Mail Logging Log (`/wp-admin/tools.php?page=wpml_plugin_log`) and verify the testing email note was sent. +- View the message and press `Test action` (a broken image will be visible under the button, but that's expected and only visible in a test environment). + ### Payments task: include Mercado Pago #6572 - Create a brand new store. diff --git a/plugins/woocommerce-admin/readme.txt b/plugins/woocommerce-admin/readme.txt index e5b7f42fb1f..00c79d82491 100644 --- a/plugins/woocommerce-admin/readme.txt +++ b/plugins/woocommerce-admin/readme.txt @@ -76,6 +76,7 @@ Release and roadmap notes are available on the [WooCommerce Developers Blog](htt == Unreleased == - Tweak: Add check to see if value for contains is array, show warning if not. #6645 +- Fix: Event tracking for merchant email notes #6616 == 2.2.0 3/26/2021 == diff --git a/plugins/woocommerce-admin/src/API/Notes.php b/plugins/woocommerce-admin/src/API/Notes.php index 52b278ae3f1..553b228e279 100644 --- a/plugins/woocommerce-admin/src/API/Notes.php +++ b/plugins/woocommerce-admin/src/API/Notes.php @@ -103,7 +103,7 @@ class Notes extends \WC_REST_CRUD_Controller { register_rest_route( $this->namespace, - '/' . $this->rest_base . '/tracker/(?P[\d-]+)', + '/' . $this->rest_base . '/tracker/(?P[\d-]+)/user/(?P[\d-]+)', array( array( 'methods' => \WP_REST_Server::READABLE, @@ -475,7 +475,11 @@ class Notes extends \WC_REST_CRUD_Controller { if ( ! $note ) { 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 ); } /** diff --git a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php index fdb4cd1f4f4..73484f5cd91 100644 --- a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php +++ b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/MerchantEmailNotifications.php @@ -30,6 +30,7 @@ class MerchantEmailNotifications { if ( ! isset( $_GET['external_redirect'] ) || 1 !== intval( $_GET['external_redirect'] ) || + ! isset( $_GET['user'] ) || ! isset( $_GET['note'] ) || ! isset( $_GET['action'] ) ) { @@ -37,6 +38,7 @@ class MerchantEmailNotifications { } $note_id = intval( $_GET['note'] ); $action_id = intval( $_GET['action'] ); + $user_id = intval( $_GET['user'] ); /* phpcs:enable */ $note = Notes::get_note( $note_id ); @@ -51,7 +53,10 @@ 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; @@ -82,7 +87,6 @@ class MerchantEmailNotifications { self::send_merchant_notification( $note ); $note->set_status( 'sent' ); $note->save(); - wc_admin_record_tracks_event( 'wcadmin_email_note_sent', array( 'note_name' => $note->get_name() ) ); } } } @@ -99,7 +103,7 @@ class MerchantEmailNotifications { foreach ( $users as $user ) { if ( is_email( $user->user_email ) ) { $name = self::get_merchant_preferred_name( $user ); - $email->trigger( $user->user_email, $name ); + $email->trigger( $user->user_email, $user->ID, $name ); } } } @@ -126,7 +130,7 @@ class MerchantEmailNotifications { * Get users by role to notify. * * @param object $note The note to send. - * @return array Emails to notify + * @return array Users to notify */ public static function get_notification_recipients( $note ) { $content_data = $note->get_content_data(); diff --git a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php index 8ba6f5e0cd7..40f5a93d907 100644 --- a/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php +++ b/plugins/woocommerce-admin/src/Notes/MerchantEmailNotifications/NotificationEmail.php @@ -176,19 +176,22 @@ class NotificationEmail extends \WC_Email { * Trigger the sending of this email. * * @param string $user_email Email to send the note. + * @param int $user_id User id to to track the note. * @param string $user_name User's name. */ - public function trigger( $user_email, $user_name ) { + public function trigger( $user_email, $user_id, $user_name ) { $this->recipient = $user_email; $this->opened_tracking_url = sprintf( - '%1$s/wp-json/wc-analytics/admin/notes/tracker/%2$d', + '%1$s/wp-json/wc-analytics/admin/notes/tracker/%2$d/user/%3$d', site_url(), - $this->note->get_id() + $this->note->get_id(), + $user_id ); $this->trigger_note_action_url = sprintf( - '%1$s&external_redirect=1¬e=%2$d&action=', + '%1$s&external_redirect=1¬e=%2$d&user=%3$d&action=', wc_admin_url(), - $this->note->get_id() + $this->note->get_id(), + $user_id ); if ( $user_name ) { @@ -203,5 +206,8 @@ 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 ); } }