From 3639cd094ce71de3cb9fc7dba81a7ab8a196cb0c Mon Sep 17 00:00:00 2001
From: Colin Kincaid <ckincaid@chromium.org>
Date: Fri, 8 Jul 2022 02:28:44 +0000
Subject: [PATCH] multipaste: Record user action for which menu item was
 selected on paste

This CL adds user actions for each of clipboard history's five possible
menu items, emitting when that item is pasted. This action will be used
to analyze usage patterns related to the order users paste clipboard
history items.

Bug: 1342294
Change-Id: I65a8d69714cac46de996d6c8ee4548b8f2a9837a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3751870
Commit-Queue: Colin Kincaid <ckincaid@chromium.org>
Reviewed-by: Mark Pearson <mpearson@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1021970}
---
 .../clipboard_history_controller_impl.cc      | 38 ++++++++++++++++++
 tools/metrics/actions/actions.xml             | 40 +++++++++++++++++++
 2 files changed, 78 insertions(+)

diff --git a/ash/clipboard/clipboard_history_controller_impl.cc b/ash/clipboard/clipboard_history_controller_impl.cc
index 621bfbf7d4551..4b784518f5105 100644
--- a/ash/clipboard/clipboard_history_controller_impl.cc
+++ b/ash/clipboard/clipboard_history_controller_impl.cc
@@ -32,6 +32,8 @@
 #include "base/location.h"
 #include "base/metrics/histogram_functions.h"
 #include "base/metrics/histogram_macros.h"
+#include "base/metrics/user_metrics.h"
+#include "base/notreached.h"
 #include "base/one_shot_event.h"
 #include "base/ranges/algorithm.h"
 #include "base/strings/utf_string_conversions.h"
@@ -103,6 +105,36 @@ void EncodeBitmapToPNG(
   std::move(barrier_callback).Run();
 }
 
+// Emits a user action indicating that the clipboard history item at menu index
+// `command_id` was pasted.
+void RecordMenuIndexPastedUserAction(int command_id) {
+  // Per guidance in user_metrics.h, use string literals for action names.
+  switch (command_id) {
+    case 1:
+      base::RecordAction(
+          base::UserMetricsAction("Ash_ClipboardHistory_PastedItem1"));
+      break;
+    case 2:
+      base::RecordAction(
+          base::UserMetricsAction("Ash_ClipboardHistory_PastedItem2"));
+      break;
+    case 3:
+      base::RecordAction(
+          base::UserMetricsAction("Ash_ClipboardHistory_PastedItem3"));
+      break;
+    case 4:
+      base::RecordAction(
+          base::UserMetricsAction("Ash_ClipboardHistory_PastedItem4"));
+      break;
+    case 5:
+      base::RecordAction(
+          base::UserMetricsAction("Ash_ClipboardHistory_PastedItem5"));
+      break;
+    default:
+      NOTREACHED();
+  }
+}
+
 using ClipboardHistoryPasteType =
     ash::ClipboardHistoryControllerImpl::ClipboardHistoryPasteType;
 bool IsPlainTextPaste(ClipboardHistoryPasteType paste_type) {
@@ -715,9 +747,15 @@ void ClipboardHistoryControllerImpl::ExecuteCommand(int command_id,
 void ClipboardHistoryControllerImpl::PasteMenuItemData(
     int command_id,
     ClipboardHistoryPasteType paste_type) {
+  // Record the paste item's history list index in a histogram to get a
+  // distribution of where in the list users paste from.
   UMA_HISTOGRAM_ENUMERATION(
       "Ash.ClipboardHistory.ContextMenu.MenuOptionSelected", command_id,
       ClipboardHistoryUtil::kMaxCommandId);
+  // Record the paste item's history list index as a user action to analyze
+  // usage patterns, e.g., how frequently the same index is pasted multiple
+  // times in a row.
+  RecordMenuIndexPastedUserAction(command_id);
 
   // Deactivate ClipboardImageModelFactory prior to pasting to ensure that any
   // modifications to the clipboard for HTML rendering purposes are reversed.
diff --git a/tools/metrics/actions/actions.xml b/tools/metrics/actions/actions.xml
index b653959772188..bfc5e8fd6e5c1 100644
--- a/tools/metrics/actions/actions.xml
+++ b/tools/metrics/actions/actions.xml
@@ -3715,6 +3715,46 @@ should be able to be added at any place in this file.
   </description>
 </action>
 
+<action name="Ash_ClipboardHistory_PastedItem1">
+  <owner>ckincaid@chromium.org</owner>
+  <owner>multipaste@google.com</owner>
+  <description>
+    Emitted when a user pastes the first item from their clipboard history.
+  </description>
+</action>
+
+<action name="Ash_ClipboardHistory_PastedItem2">
+  <owner>ckincaid@chromium.org</owner>
+  <owner>multipaste@google.com</owner>
+  <description>
+    Emitted when a user pastes the second item from their clipboard history.
+  </description>
+</action>
+
+<action name="Ash_ClipboardHistory_PastedItem3">
+  <owner>ckincaid@chromium.org</owner>
+  <owner>multipaste@google.com</owner>
+  <description>
+    Emitted when a user pastes the third item from their clipboard history.
+  </description>
+</action>
+
+<action name="Ash_ClipboardHistory_PastedItem4">
+  <owner>ckincaid@chromium.org</owner>
+  <owner>multipaste@google.com</owner>
+  <description>
+    Emitted when a user pastes the fourth item from their clipboard history.
+  </description>
+</action>
+
+<action name="Ash_ClipboardHistory_PastedItem5">
+  <owner>ckincaid@chromium.org</owner>
+  <owner>multipaste@google.com</owner>
+  <description>
+    Emitted when a user pastes the fifth item from their clipboard history.
+  </description>
+</action>
+
 <action name="Ash_Tablet_BackGesture">
   <owner>minch@chromium.org</owner>
   <owner>tclaiborne@chromium.org</owner>