Skip to content

Commit

Permalink
[MerchantTrust] Add UKMs
Browse files Browse the repository at this point in the history
Approved UKM collection review:
https://docs.google.com/document/d/1EguftdXhXPJfAtMH41orSQQavXaJRcSY0bhZ68eOHWw/edit?usp=sharing

(cherry picked from commit f9f0bc3)

Bug: 1300971
Change-Id: Iccdd5b066e33cc8926a3da535e9e896926abc0d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3488679
Reviewed-by: Robert Kaplow <rkaplow@chromium.org>
Reviewed-by: David Trainor <dtrainor@chromium.org>
Commit-Queue: Zhiyuan Cai <zhiyuancai@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#978463}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3514974
Auto-Submit: Zhiyuan Cai <zhiyuancai@chromium.org>
Reviewed-by: Krishna Govind <govind@chromium.org>
Commit-Queue: Krishna Govind <govind@chromium.org>
Owners-Override: Krishna Govind <govind@chromium.org>
Cr-Commit-Position: refs/branch-heads/4896@{#521}
Cr-Branched-From: 1f63ff4-refs/heads/main@{#972766}
  • Loading branch information
Zhiyuan Cai authored and Chromium LUCI CQ committed Mar 14, 2022
1 parent faa538e commit 3a77f23
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
Expand Up @@ -261,8 +261,8 @@ public Collection<PageInfoSubpageController> createAdditionalRowViews(
storeInfoRow.setId(PageInfoStoreInfoController.STORE_INFO_ROW_ID);
rowWrapper.addView(storeInfoRow);
controllers.add(new PageInfoStoreInfoController(mainController, storeInfoRow,
mStoreInfoActionHandlerSupplier,
mPageInfoHighlight.shouldHighlightStoreInfo()));
mStoreInfoActionHandlerSupplier, mPageInfoHighlight.shouldHighlightStoreInfo(),
mWebContents));
}
return controllers;
}
Expand Down
1 change: 1 addition & 0 deletions chrome/browser/commerce/merchant_viewer/android/BUILD.gn
Expand Up @@ -62,6 +62,7 @@ android_library("java") {
"//components/thin_webview:factory_java",
"//components/thin_webview:java",
"//components/thin_webview:thin_webview",
"//components/ukm/android:java",
"//components/url_formatter/android:url_formatter_java",
"//components/user_prefs/android:java",
"//components/version_info/android:version_constants_java",
Expand Down
Expand Up @@ -7,13 +7,16 @@
import android.text.format.DateUtils;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;

import org.chromium.base.TimeUtils;
import org.chromium.base.metrics.RecordHistogram;
import org.chromium.base.metrics.RecordUserAction;
import org.chromium.components.browser_ui.bottomsheet.BottomSheetController.StateChangeReason;
import org.chromium.components.messages.DismissReason;
import org.chromium.components.ukm.UkmRecorder;
import org.chromium.content_public.browser.WebContents;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
Expand Down Expand Up @@ -350,4 +353,37 @@ private String getStarRatingSuffixForMessageImpact() {
}
return ".Rating" + ratingSuffix;
}

/** Record ukm when merchant trust data is available. */
public void recordUkmOnDataAvailable(@Nullable WebContents webContents) {
recordBooleanUkm(webContents, "Shopping.MerchantTrust.DataAvailable", "DataAvailable");
}

/** Record ukm when merchant trust message is displayed. */
public void recordUkmOnMessageSeen(@Nullable WebContents webContents) {
recordBooleanUkm(webContents, "Shopping.MerchantTrust.MessageSeen", "HasOccurred");
}

/** Record ukm when merchant trust message is clicked. */
public void recordUkmOnMessageClicked(@Nullable WebContents webContents) {
recordBooleanUkm(webContents, "Shopping.MerchantTrust.MessageClicked", "HasOccurred");
}

/** Record ukm when store info row in trusted surface is displayed. */
public void recordUkmOnRowSeen(@Nullable WebContents webContents) {
recordBooleanUkm(webContents, "Shopping.MerchantTrust.RowSeen", "HasOccurred");
}

/** Record ukm when store info row in trusted surface is clicked. */
public void recordUkmOnRowClicked(@Nullable WebContents webContents) {
recordBooleanUkm(webContents, "Shopping.MerchantTrust.RowClicked", "HasOccurred");
}

private void recordBooleanUkm(
@Nullable WebContents webContents, String eventName, String metricsName) {
if (webContents != null) {
new UkmRecorder.Bridge().recordEventWithBooleanMetric(
webContents, eventName, metricsName);
}
}
}
Expand Up @@ -71,6 +71,7 @@ void showStoreIcon(WindowAndroid window, String url, Drawable drawable,
private final MerchantTrustSignalsStorageFactory mStorageFactory;
private final ObservableSupplier<Profile> mProfileSupplier;
private final WindowAndroid mWindowAndroid;
private final ObservableSupplier<Tab> mTabSupplier;
private OmniboxIconController mOmniboxIconController;

/** Creates a new instance. */
Expand Down Expand Up @@ -101,6 +102,7 @@ tabSupplier, new MerchantTrustSignalsDataProvider(), profileSupplier, metrics,
mStorageFactory = storageFactory;
mProfileSupplier = profileSupplier;
mWindowAndroid = windowAndroid;
mTabSupplier = tabSupplier;

mMediator = new MerchantTrustSignalsMediator(
tabSupplier, this::onFinishEligibleNavigation, metrics);
Expand Down Expand Up @@ -151,6 +153,7 @@ void onFinishEligibleNavigation(MerchantTrustMessageContext item) {
void maybeDisplayMessage(MerchantTrustSignalsV2 trustSignals, MerchantTrustMessageContext item,
boolean shouldExpediteMessage) {
if (trustSignals == null) return;
mMetrics.recordUkmOnDataAvailable(item.getWebContents());
NavigationHandle navigationHandle = item.getNavigationHandle();
MerchantTrustSignalsEventStorage storage = mStorageFactory.getForLastUsedProfile();
if (navigationHandle == null || navigationHandle.getUrl() == null || storage == null
Expand Down Expand Up @@ -230,6 +233,7 @@ void onMessageEnqueued(MerchantTrustMessageContext messageContext) {
return;
}

mMetrics.recordUkmOnMessageSeen(messageContext.getWebContents());
updateShownMessagesTimestamp();
storage.save(new MerchantTrustSignalsEvent(
messageContext.getHostName(), System.currentTimeMillis()));
Expand All @@ -248,6 +252,12 @@ public void onMessageDismissed(@DismissReason int dismissReason, String messageA
public void onMessagePrimaryAction(
MerchantTrustSignalsV2 trustSignals, String messageAssociatedUrl) {
mMetrics.recordMetricsForMessageTapped();

// TODO(crbug.com/1300971): Pass webContents directly to this method instead of using
// mTabSupplier.
if (mTabSupplier.hasValue()) {
mMetrics.recordUkmOnMessageClicked(mTabSupplier.get().getWebContents());
}
launchDetailsPage(new GURL(trustSignals.getMerchantDetailsPageUrl()),
BottomSheetOpenedSource.FROM_MESSAGE, messageAssociatedUrl);
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import org.chromium.components.page_info.PageInfoMainController;
import org.chromium.components.page_info.PageInfoRowView;
import org.chromium.components.page_info.PageInfoSubpageController;
import org.chromium.content_public.browser.WebContents;

/**
* Class for controlling the {@link ChromePageInfo} "store info" section.
Expand All @@ -39,19 +40,21 @@ public interface StoreInfoActionHandler {
private final PageInfoRowView mRowView;
private final Context mContext;
private final boolean mPageInfoOpenedFromStoreIcon;
private final WebContents mWebContents;
private final PageInfoDiscoverabilityMetrics mDiscoverabilityMetrics =
new PageInfoDiscoverabilityMetrics();
private final MerchantTrustMetrics mMetrics = new MerchantTrustMetrics();

public PageInfoStoreInfoController(PageInfoMainController mainController,
PageInfoRowView rowView,
@Nullable Supplier<StoreInfoActionHandler> actionHandlerSupplier,
boolean pageInfoOpenedFromStoreIcon) {
boolean pageInfoOpenedFromStoreIcon, WebContents webContents) {
mMainController = mainController;
mRowView = rowView;
mContext = mRowView.getContext();
mActionHandlerSupplier = actionHandlerSupplier;
mPageInfoOpenedFromStoreIcon = pageInfoOpenedFromStoreIcon;
mWebContents = webContents;
// Creating the instance of {@link MerchantTrustSignalsDataProvider} will force
// OptimizationGuide to register for the MERCHANT_TRUST_SIGNALS type, so we need to check
// the feature flag first.
Expand Down Expand Up @@ -87,8 +90,10 @@ private void setupStoreInfoRow(@Nullable MerchantTrustSignalsV2 trustSignals) {
}
mMainController.recordAction(PageInfoAction.PAGE_INFO_STORE_INFO_CLICKED);
mMainController.dismiss();
mMetrics.recordUkmOnRowClicked(mWebContents);
mActionHandlerSupplier.get().onStoreInfoClicked(trustSignals);
};
mMetrics.recordUkmOnRowSeen(mWebContents);
}
mMetrics.recordMetricsForStoreInfoRowVisible(rowParams.visible);
mRowView.setParams(rowParams);
Expand Down
Expand Up @@ -84,6 +84,9 @@ public class MerchantTrustSignalsCoordinatorTest {
@Mock
private ObservableSupplier<Tab> mMockTabProvider;

@Mock
private Tab mMockTab;

@Mock
private ObservableSupplier<Profile> mMockProfileSupplier;

Expand Down Expand Up @@ -178,6 +181,9 @@ public void setUp() {
doReturn(mMockProfile).when(mMockProfileSupplier).get();
doReturn(false).when(mMockProfile).isOffTheRecord();
doReturn(FAKE_HOST).when(mMockGurl).getSpec();
doReturn(true).when(mMockTabProvider).hasValue();
doReturn(mMockTab).when(mMockTabProvider).get();
doReturn(mMockWebContents).when(mMockTab).getWebContents();
doAnswer((Answer<String>) invocation -> mSerializedTimestamps)
.when(mMockPrefService)
.getString(eq(Pref.COMMERCE_MERCHANT_VIEWER_MESSAGES_SHOWN_TIME));
Expand Down Expand Up @@ -294,6 +300,7 @@ public void testFetchTrustSiganl_WithScheduledMessage_ForDifferentHost() {
public void testMaybeDisplayMessage_ShouldNotExpediteMessage() {
mCoordinator.maybeDisplayMessage(mDummyMerchantTrustSignals, mMessageContext, false);

verify(mMockMetrics, times(1)).recordUkmOnDataAvailable(eq(mMockWebContents));
verify(mMockMerchantTrustStorage, times(1)).delete(eq(mMockMerchantTrustSignalsEvent));
verifySchedulingMessage(true, false);
}
Expand All @@ -303,6 +310,7 @@ public void testMaybeDisplayMessage_ShouldNotExpediteMessage() {
public void testMaybeDisplayMessage_ShouldExpediteMessage() {
mCoordinator.maybeDisplayMessage(mDummyMerchantTrustSignals, mMessageContext, true);

verify(mMockMetrics, times(1)).recordUkmOnDataAvailable(eq(mMockWebContents));
verify(mMockMerchantTrustStorage, times(1)).delete(eq(mMockMerchantTrustSignalsEvent));
verifySchedulingMessage(true, true);
}
Expand Down Expand Up @@ -338,6 +346,7 @@ public void testMaybeDisplayMessage_NoPreviousEvent() {
public void testMaybeDisplayMessage_NoMerchantTrustData() {
mCoordinator.maybeDisplayMessage(null, mMessageContext, false);

verify(mMockMetrics, times(0)).recordUkmOnDataAvailable(eq(mMockWebContents));
verify(mMockMerchantTrustStorage, times(0)).delete(eq(mMockMerchantTrustSignalsEvent));
verifySchedulingMessage(false, false);
}
Expand Down Expand Up @@ -487,6 +496,7 @@ public void testOnMessageEnqueued() {

mCoordinator.onMessageEnqueued(
new MerchantTrustMessageContext(mMockNavigationHandle, mMockWebContents));
verify(mMockMetrics, times(1)).recordUkmOnMessageSeen(eq(mMockWebContents));
verify(mCoordinator, times(1)).updateShownMessagesTimestamp();
verify(mMockMerchantTrustStorage, times(1)).save(any(MerchantTrustSignalsEvent.class));
}
Expand All @@ -512,6 +522,7 @@ public void testOnMessageDismissed_Gesture() {
public void testOnMessagePrimaryAction() {
mCoordinator.onMessagePrimaryAction(mDummyMerchantTrustSignals, FAKE_URL);
verify(mMockMetrics, times(1)).recordMetricsForMessageTapped();
verify(mMockMetrics, times(1)).recordUkmOnMessageClicked(eq(mMockWebContents));
verify(mMockMetrics, times(1))
.recordMetricsForBottomSheetOpenedSource(eq(BottomSheetOpenedSource.FROM_MESSAGE));
verify(mMockDetailsTabCoordinator, times(1))
Expand Down
68 changes: 68 additions & 0 deletions tools/metrics/ukm/ukm.xml
Expand Up @@ -17186,6 +17186,74 @@ be describing additional metrics about the same event.
</metric>
</event>

<event name="Shopping.MerchantTrust.DataAvailable">
<owner>zhiyuancai@chromium.org</owner>
<owner>ayman@chromium.org</owner>
<summary>
Recorded when user finishes a navigation while the merchant trust data is
available.
</summary>
<metric name="DataAvailable" enum="Boolean">
<summary>
A boolean signaling that data is available. Only records true values.
</summary>
</metric>
</event>

<event name="Shopping.MerchantTrust.MessageClicked">
<owner>zhiyuancai@chromium.org</owner>
<owner>ayman@chromium.org</owner>
<summary>
Recorded when a merchant trust message is clicked by the user.
</summary>
<metric name="HasOccurred" enum="Boolean">
<summary>
A boolean signaling that message is clicked. Only records true values.
</summary>
</metric>
</event>

<event name="Shopping.MerchantTrust.MessageSeen">
<owner>zhiyuancai@chromium.org</owner>
<owner>ayman@chromium.org</owner>
<summary>
Recorded when a merchant trust message is displayed to the user.
</summary>
<metric name="HasOccurred" enum="Boolean">
<summary>
A boolean signaling that message is displayed. Only records true values.
</summary>
</metric>
</event>

<event name="Shopping.MerchantTrust.RowClicked">
<owner>zhiyuancai@chromium.org</owner>
<owner>ayman@chromium.org</owner>
<summary>
Recorded when a store info row in trusted surface is clicked by the user.
</summary>
<metric name="HasOccurred" enum="Boolean">
<summary>
A boolean signaling that store info row is clicked. Only records true
values.
</summary>
</metric>
</event>

<event name="Shopping.MerchantTrust.RowSeen">
<owner>zhiyuancai@chromium.org</owner>
<owner>ayman@chromium.org</owner>
<summary>
Recorded when a store info row in trusted surface is displayed to the user.
</summary>
<metric name="HasOccurred" enum="Boolean">
<summary>
A boolean signaling that store info row is displayed. Only records true
values.
</summary>
</metric>
</event>

<event name="Shopping.WillSendRequest">
<owner>wychen@chromium.org</owner>
<owner>yuezhanggg@chromium.org</owner>
Expand Down

0 comments on commit 3a77f23

Please sign in to comment.