[Pix] Add metrics to Pix flow (10)

Add metrics regarding InitiatePurchaseAction call:
1. Remove the InitiatePayment result and transaction result histogram;
2. Update InitiatePayment latency histogram to add a result breakdown.

go/payments-autofill-uma has been updated

OBSOLETE_HISTOGRAM[FacilitatedPayments.Pix.InitiatePurchaseAction.Result]=Replaced by FacilitatedPayments.{FacilitatedPaymentsType}.InitiatePurchaseAction.{Result}.Latency.
OBSOLETE_HISTOGRAM[FacilitatedPayments.Pix.InitiatePurchaseAction.Latency]=Replaced by FacilitatedPayments.{FacilitatedPaymentsType}.InitiatePurchaseAction.{Result}.Latency.

go/pix-in-chrome-metrics-revised

Bug: 367751320, 377126728, 378741115
Change-Id: Ib68f2675968efbef19b1b8b8778f845e83d180c3
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6013094
Reviewed-by: Vishwas Uppoor <vishwasuppoor@google.com>
Reviewed-by: Rouslan Solomakhin <rouslan@chromium.org>
Commit-Queue: Siyu An <siyua@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1384574}
diff --git a/components/facilitated_payments/core/browser/facilitated_payments_manager.cc b/components/facilitated_payments/core/browser/facilitated_payments_manager.cc
index e094f047..1119028 100644
--- a/components/facilitated_payments/core/browser/facilitated_payments_manager.cc
+++ b/components/facilitated_payments/core/browser/facilitated_payments_manager.cc
@@ -291,32 +291,14 @@
 }
 
 void FacilitatedPaymentsManager::OnPurchaseActionResult(
-    FacilitatedPaymentsApiClient::PurchaseActionResult result) {
+    PurchaseActionResult result) {
   // When server responds to the purchase action, Google Play Services takes
   // over, and the progress screen gets dismissed. Calling `DismissPrompt`
   // clears the associated Java objects.
   DismissPrompt();
-  LogInitiatePurchaseActionResult(
-      /*result=*/result ==
-          FacilitatedPaymentsApiClient::PurchaseActionResult::kResultOk,
+  LogInitiatePurchaseActionResultAndLatency(
+      GetInitiatePurchaseActionResultString(result),
       base::TimeTicks::Now() - purchase_action_start_time_);
-  // Map the result received from the purchase action to overall transaction
-  // result.
-  TransactionResult transaction_result = TransactionResult::kFailed;
-  switch (result) {
-    case FacilitatedPaymentsApiClient::PurchaseActionResult::kResultOk:
-      transaction_result = TransactionResult::kSuccess;
-      break;
-    case FacilitatedPaymentsApiClient::PurchaseActionResult::kCouldNotInvoke:
-      transaction_result = TransactionResult::kFailed;
-      break;
-    case FacilitatedPaymentsApiClient::PurchaseActionResult::kResultCanceled:
-      transaction_result = TransactionResult::kAbandoned;
-      break;
-  }
-  LogTransactionResult(transaction_result, trigger_source_,
-                       base::TimeTicks::Now() - fop_selector_shown_time_,
-                       ukm_source_id_);
 }
 
 void FacilitatedPaymentsManager::OnUiEvent(UiEvent ui_event_type) {
@@ -363,4 +345,16 @@
   client_->ShowErrorScreen();
 }
 
+std::string FacilitatedPaymentsManager::GetInitiatePurchaseActionResultString(
+    PurchaseActionResult result) {
+  switch (result) {
+    case PurchaseActionResult::kResultOk:
+      return std::string("Succeeded");
+    case PurchaseActionResult::kCouldNotInvoke:
+      return std::string("Failed");
+    case PurchaseActionResult::kResultCanceled:
+      return std::string("Abandoned");
+  }
+}
+
 }  // namespace payments::facilitated
diff --git a/components/facilitated_payments/core/browser/facilitated_payments_manager.h b/components/facilitated_payments/core/browser/facilitated_payments_manager.h
index 4386839..6028052 100644
--- a/components/facilitated_payments/core/browser/facilitated_payments_manager.h
+++ b/components/facilitated_payments/core/browser/facilitated_payments_manager.h
@@ -35,6 +35,8 @@
 class FacilitatedPaymentsClient;
 class FacilitatedPaymentsDriver;
 
+using PurchaseActionResult = FacilitatedPaymentsApiClient::PurchaseActionResult;
+
 // A cross-platform interface that manages the flow of payments for non-form
 // based form-of-payments between the browser and the Payments platform. It is
 // owned by `FacilitatedPaymentsDriver`.
@@ -192,22 +194,9 @@
       FacilitatedPaymentsManagerTest,
       OnInitiatePaymentResponseReceived_InvokePurchaseActionTriggered);
   FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
-                           OnPurchaseActionPositiveResult_UiPromptDismissed);
+                           OnPurchaseActionResult_UiPromptDismissed);
   FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
-                           OnPurchaseActionNegativeResult_UiPromptDismissed);
-  FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
-                           InvokePurchaseActionCompleted_HistogramLogged);
-  FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
-                           TransactionSuccess_HistogramLogged);
-  FRIEND_TEST_ALL_PREFIXES(
-      FacilitatedPaymentsManagerTest,
-      TransactionAbandonedAfterInvokePurchaseAction_HistogramLogged);
-  FRIEND_TEST_ALL_PREFIXES(
-      FacilitatedPaymentsManagerTest,
-      TransactionFailedAfterInvokePurchaseAction_HistogramLogged);
-  FRIEND_TEST_ALL_PREFIXES(
-      FacilitatedPaymentsManagerTest,
-      FOPSelectorNotShown_TransactionResultHistogramNotLogged);
+                           LogInitiatePurchaseActionResultAndLatency);
   FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
                            ApiClientInitializedLazily);
   FRIEND_TEST_ALL_PREFIXES(FacilitatedPaymentsManagerTest,
@@ -288,8 +277,7 @@
 
   // Called after receiving the `result` of invoking the purchase manager for
   // payment.
-  void OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult result);
+  void OnPurchaseActionResult(PurchaseActionResult result);
 
   // Called by the view to communicate UI events.
   void OnUiEvent(UiEvent ui_event_type);
@@ -308,6 +296,10 @@
   // Sets the internal state and triggers showing the error screen.
   void ShowErrorScreen();
 
+  // Converts the PurchaseActionResult to the string version.
+  std::string GetInitiatePurchaseActionResultString(
+      PurchaseActionResult result);
+
   // Owner.
   const raw_ref<FacilitatedPaymentsDriver> driver_;
 
diff --git a/components/facilitated_payments/core/browser/facilitated_payments_manager_unittest.cc b/components/facilitated_payments/core/browser/facilitated_payments_manager_unittest.cc
index 8c12bb4..1529f0af 100644
--- a/components/facilitated_payments/core/browser/facilitated_payments_manager_unittest.cc
+++ b/components/facilitated_payments/core/browser/facilitated_payments_manager_unittest.cc
@@ -774,30 +774,6 @@
       /*expected_bucket_count=*/1);
 }
 
-// Test that when a positive puchase action result is received, the UI prompt is
-// dismissed.
-TEST_F(FacilitatedPaymentsManagerTest,
-       OnPurchaseActionPositiveResult_UiPromptDismissed) {
-  // `DismissPrompt` is called once when the purchase action result is received,
-  // and again when the test fixture destroys the `manager_`.
-  EXPECT_CALL(*client_, DismissPrompt()).Times(2);
-
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kResultOk);
-}
-
-// Test that when a negative puchase action result is received, the UI prompt is
-// dismissed.
-TEST_F(FacilitatedPaymentsManagerTest,
-       OnPurchaseActionNegativeResult_UiPromptDismissed) {
-  // `DismissPrompt` is called once when the purchase action result is received,
-  // and again when the test fixture destroys the `manager_`.
-  EXPECT_CALL(*client_, DismissPrompt()).Times(2);
-
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kResultCanceled);
-}
-
 // The `IsAvailable` async call is made after a valid Pix code has been
 // detected. This test verifies that the result and latency are logged after the
 // async call is completed.
@@ -835,119 +811,49 @@
       /*expected_bucket_count=*/1);
 }
 
-// Test that once the purchase action response is received, the result and
+// Test that when a purchase action result is received, the UI prompt is
+// dismissed.
+TEST_F(FacilitatedPaymentsManagerTest,
+       OnPurchaseActionResult_UiPromptDismissed) {
+  // `DismissPrompt` is called once whenever a purchase action result is
+  // received, and again when the test fixture destroys the `manager_`.
+  EXPECT_CALL(*client_, DismissPrompt()).Times(3);
+
+  for (PurchaseActionResult result : {PurchaseActionResult::kResultOk,
+                                      PurchaseActionResult::kResultCanceled}) {
+    manager_->OnPurchaseActionResult(result);
+  }
+}
+
+// Test that when an InitiatePurchaseAction response is received, the result and
 // latency of the invoke purchase action is logged.
 TEST_F(FacilitatedPaymentsManagerTest,
-       InvokePurchaseActionCompleted_HistogramLogged) {
-  base::HistogramTester histogram_tester;
-  ON_CALL(*client_, GetCoreAccountInfo)
-      .WillByDefault(testing::Return(CreateLoggedInAccountInfo()));
-  EXPECT_CALL(GetApiClient(), InvokePurchaseAction);
-  auto response_details =
-      std::make_unique<FacilitatedPaymentsInitiatePaymentResponseDetails>();
-  response_details->action_token_ =
-      std::vector<uint8_t>{'t', 'o', 'k', 'e', 'n'};
-  manager_->OnInitiatePaymentResponseReceived(
-      autofill::payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess,
-      std::move(response_details));
+       LogInitiatePurchaseActionResultAndLatency) {
+  for (PurchaseActionResult result :
+       {PurchaseActionResult::kResultOk, PurchaseActionResult::kCouldNotInvoke,
+        PurchaseActionResult::kResultCanceled}) {
+    base::HistogramTester histogram_tester;
+    ON_CALL(*client_, GetCoreAccountInfo)
+        .WillByDefault(testing::Return(CreateLoggedInAccountInfo()));
+    EXPECT_CALL(GetApiClient(), InvokePurchaseAction);
+    auto response_details =
+        std::make_unique<FacilitatedPaymentsInitiatePaymentResponseDetails>();
+    response_details->action_token_ =
+        std::vector<uint8_t>{'t', 'o', 'k', 'e', 'n'};
+    manager_->OnInitiatePaymentResponseReceived(
+        autofill::payments::PaymentsAutofillClient::PaymentsRpcResult::kSuccess,
+        std::move(response_details));
 
-  FastForwardBy(base::Seconds(2));
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kResultOk);
+    FastForwardBy(base::Seconds(2));
+    manager_->OnPurchaseActionResult(result);
 
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.InitiatePurchaseAction.Result",
-      /*sample=*/true,
-      /*expected_bucket_count=*/1);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.InitiatePurchaseAction.Latency",
-      /*sample=*/2000,
-      /*expected_bucket_count=*/1);
-}
-
-// Test that once the purchase action response is received, the transaction
-// result and latency is logged.
-TEST_F(FacilitatedPaymentsManagerTest, TransactionSuccess_HistogramLogged) {
-  base::HistogramTester histogram_tester;
-  autofill::BankAccount pix_account = CreatePixBankAccount(/*instrument_id=*/1);
-  payments_data_manager_->AddMaskedBankAccountForTest(pix_account);
-  manager_->OnApiAvailabilityReceived(true);
-
-  FastForwardBy(base::Seconds(2));
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kResultOk);
-
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Result",
-      /*sample=*/TransactionResult::kSuccess,
-      /*expected_bucket_count=*/1);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Success.Latency",
-      /*sample=*/2000,
-      /*expected_bucket_count=*/1);
-}
-
-// Test that once the purchase action response is received as result canceled,
-// the transaction result is logged as abandoned and the latency is logged.
-TEST_F(FacilitatedPaymentsManagerTest,
-       TransactionAbandonedAfterInvokePurchaseAction_HistogramLogged) {
-  base::HistogramTester histogram_tester;
-  autofill::BankAccount pix_account = CreatePixBankAccount(/*instrument_id=*/1);
-  payments_data_manager_->AddMaskedBankAccountForTest(pix_account);
-  manager_->OnApiAvailabilityReceived(true);
-
-  FastForwardBy(base::Seconds(2));
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kResultCanceled);
-
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Result",
-      /*sample=*/TransactionResult::kAbandoned,
-      /*expected_bucket_count=*/1);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Abandoned.Latency",
-      /*sample=*/2000,
-      /*expected_bucket_count=*/1);
-}
-
-// Test that if the purchase action was unable to be invoked, the transaction
-// result is logged as failed and the latency is logged.
-TEST_F(FacilitatedPaymentsManagerTest,
-       TransactionFailedAfterInvokePurchaseAction_HistogramLogged) {
-  base::HistogramTester histogram_tester;
-  autofill::BankAccount pix_account = CreatePixBankAccount(/*instrument_id=*/1);
-  payments_data_manager_->AddMaskedBankAccountForTest(pix_account);
-  manager_->OnApiAvailabilityReceived(true);
-
-  FastForwardBy(base::Seconds(2));
-  manager_->OnPurchaseActionResult(
-      FacilitatedPaymentsApiClient::PurchaseActionResult::kCouldNotInvoke);
-
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Result",
-      /*sample=*/TransactionResult::kFailed,
-      /*expected_bucket_count=*/1);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Failed.Latency",
-      /*sample=*/2000,
-      /*expected_bucket_count=*/1);
-}
-
-TEST_F(FacilitatedPaymentsManagerTest,
-       FOPSelectorNotShown_TransactionResultHistogramNotLogged) {
-  base::HistogramTester histogram_tester;
-  autofill::BankAccount pix_account = CreatePixBankAccount(/*instrument_id=*/1);
-  payments_data_manager_->AddMaskedBankAccountForTest(pix_account);
-  manager_->OnApiAvailabilityReceived(true);
-
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Result",
-      /*sample=*/TransactionResult::kFailed,
-      /*expected_bucket_count=*/0);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.Transaction.Failed.Latency",
-      /*sample=*/2000,
-      /*expected_bucket_count=*/0);
+    histogram_tester.ExpectBucketCount(
+        base::StrCat({"FacilitatedPayments.Pix.InitiatePurchaseAction.",
+                      manager_->GetInitiatePurchaseActionResultString(result),
+                      ".Latency"}),
+        /*sample=*/2000,
+        /*expected_count=*/1);
+  }
 }
 
 // Verify that the API client is initialized lazily, so it does not take up
diff --git a/components/facilitated_payments/core/metrics/facilitated_payments_metrics.cc b/components/facilitated_payments/core/metrics/facilitated_payments_metrics.cc
index 434ac2c3..76e01063 100644
--- a/components/facilitated_payments/core/metrics/facilitated_payments_metrics.cc
+++ b/components/facilitated_payments/core/metrics/facilitated_payments_metrics.cc
@@ -102,13 +102,14 @@
       duration);
 }
 
-void LogInitiatePurchaseActionResult(bool result, base::TimeDelta duration) {
+void LogInitiatePurchaseActionResultAndLatency(const std::string& result,
+                                               base::TimeDelta duration) {
   // TODO(crbug.com/337929926): Remove hardcoding for Pix and use
   // FacilitatedPaymentsType enum.
-  UMA_HISTOGRAM_BOOLEAN("FacilitatedPayments.Pix.InitiatePurchaseAction.Result",
-                        result);
   base::UmaHistogramLongTimes(
-      "FacilitatedPayments.Pix.InitiatePurchaseAction.Latency", duration);
+      base::StrCat({"FacilitatedPayments.Pix.InitiatePurchaseAction.", result,
+                    ".Latency"}),
+      duration);
 }
 
 void LogFopSelectorShown(bool shown) {
diff --git a/components/facilitated_payments/core/metrics/facilitated_payments_metrics.h b/components/facilitated_payments/core/metrics/facilitated_payments_metrics.h
index d65d407..f49de093 100644
--- a/components/facilitated_payments/core/metrics/facilitated_payments_metrics.h
+++ b/components/facilitated_payments/core/metrics/facilitated_payments_metrics.h
@@ -123,7 +123,10 @@
 
 // Log the result and latency for the InitiatePurchaseAction call made to the
 // payments platform (client).
-void LogInitiatePurchaseActionResult(bool result, base::TimeDelta duration);
+// TODO(crbug.com/379723883): Move the `PurchaseActionResult` and have this
+// function take in an enum instead of a string.
+void LogInitiatePurchaseActionResultAndLatency(const std::string& result,
+                                               base::TimeDelta duration);
 
 // Log whether the request to show the FOP(form of payment) selector is
 // successful or not.
diff --git a/components/facilitated_payments/core/metrics/facilitated_payments_metrics_unittest.cc b/components/facilitated_payments/core/metrics/facilitated_payments_metrics_unittest.cc
index a2f03422..f87c782 100644
--- a/components/facilitated_payments/core/metrics/facilitated_payments_metrics_unittest.cc
+++ b/components/facilitated_payments/core/metrics/facilitated_payments_metrics_unittest.cc
@@ -144,19 +144,19 @@
   }
 }
 
-TEST(FacilitatedPaymentsMetricsTest, LogInitiatePurchaseActionResult) {
-  base::HistogramTester histogram_tester;
+TEST(FacilitatedPaymentsMetricsTest,
+     LogInitiatePurchaseActionResultAndLatency) {
+  for (const std::string& result : {"Succeeded", "Failed", "Abandoned"}) {
+    base::HistogramTester histogram_tester;
 
-  LogInitiatePurchaseActionResult(/*result=*/true, base::Milliseconds(10));
+    LogInitiatePurchaseActionResultAndLatency(result, base::Milliseconds(10));
 
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.InitiatePurchaseAction.Result",
-      /*sample=*/true,
-      /*expected_bucket_count=*/1);
-  histogram_tester.ExpectUniqueSample(
-      "FacilitatedPayments.Pix.InitiatePurchaseAction.Latency",
-      /*sample=*/10,
-      /*expected_bucket_count=*/1);
+    histogram_tester.ExpectBucketCount(
+        base::StrCat({"FacilitatedPayments.Pix.InitiatePurchaseAction.", result,
+                      ".Latency"}),
+        /*sample=*/10,
+        /*expected_count=*/1);
+  }
 }
 
 TEST(FacilitatedPaymentsMetricsTest, LogFopSelectorShown) {
diff --git a/tools/metrics/histograms/metadata/facilitated_payments/histograms.xml b/tools/metrics/histograms/metadata/facilitated_payments/histograms.xml
index abea1bba..ca750a6 100644
--- a/tools/metrics/histograms/metadata/facilitated_payments/histograms.xml
+++ b/tools/metrics/histograms/metadata/facilitated_payments/histograms.xml
@@ -193,7 +193,7 @@
 </histogram>
 
 <histogram
-    name="FacilitatedPayments.{FacilitatedPaymentsType}.InitiatePurchaseAction.Latency"
+    name="FacilitatedPayments.{FacilitatedPaymentsType}.InitiatePurchaseAction.{Result}.Latency"
     units="ms" expires_after="2025-07-01">
   <owner>siashah@google.com</owner>
   <owner>vishwasuppoor@google.com</owner>
@@ -201,29 +201,19 @@
   <owner>payments-autofill-team@google.com</owner>
   <summary>
     Logs the latency for the call made to payments platform (client) which
-    faciliates the payment. [Frequency] Logged at most once per page load.
-    [Trigger] User selects a payment instrument from the list of options shown
-    in the {FacilitatedPaymentsType} UI and the backend returns the instruction
-    to trigger the purchase action flow in GMSCore.
+    faciliates the payment and breaks it down by the result of the call. The
+    result is {Result}. [Frequency] Logged at most once per
+    {FacilitatedPaymentsType} payflow. [Trigger] User selects a payment
+    instrument from the list of options shown in the {FacilitatedPaymentsType}
+    UI and the backend returns the instruction to trigger the purchase action
+    flow in GMSCore.
   </summary>
   <token key="FacilitatedPaymentsType" variants="FacilitatedPaymentsTypes"/>
-</histogram>
-
-<histogram
-    name="FacilitatedPayments.{FacilitatedPaymentsType}.InitiatePurchaseAction.Result"
-    enum="Boolean" expires_after="2025-07-01">
-  <owner>siashah@google.com</owner>
-  <owner>vishwasuppoor@google.com</owner>
-  <owner>rouslan@google.com</owner>
-  <owner>payments-autofill-team@google.com</owner>
-  <summary>
-    Logs the result returned for the call made to payments platform (client)
-    which facilitates the payment. [Frequency] Logged at most once per page
-    load. [Trigger] User selects a payment instrument from the list of options
-    shown in the {FacilitatedPaymentsType} UI and the backend returns the
-    instruction to trigger the purchase action flow in GMSCore.
-  </summary>
-  <token key="FacilitatedPaymentsType" variants="FacilitatedPaymentsTypes"/>
+  <token key="Result">
+    <variant name="Abandoned" summary="Abandoned InitiatePurchaseAction call"/>
+    <variant name="Failed" summary="Failed InitiatePurchaseAction call"/>
+    <variant name="Succeeded" summary="Successful InitiatePurchaseAction call"/>
+  </token>
 </histogram>
 
 <histogram