Commit 758d8fd8 authored by Sébastien Séguin-Gagnon's avatar Sébastien Séguin-Gagnon Committed by Commit Bot

[SH] Add common logging functions.

Bug: 1131107
Change-Id: Ib7b047409a42fe5a4ad5b33ecf10825214c9089d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2456126
Commit-Queue: sebsg <sebsg@chromium.org>
Reviewed-by: default avatarMark Pearson <mpearson@chromium.org>
Reviewed-by: default avatarCharlie Harrison <csharrison@chromium.org>
Reviewed-by: default avatarCait Phillips <caitkp@chromium.org>
Reviewed-by: default avatarSebastien Lalancette <seblalancette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#817004}
parent 2e0573cb
...@@ -139,6 +139,7 @@ test("components_unittests") { ...@@ -139,6 +139,7 @@ test("components_unittests") {
"//components/services/heap_profiling/public/cpp:unit_tests", "//components/services/heap_profiling/public/cpp:unit_tests",
"//components/services/unzip:unit_tests", "//components/services/unzip:unit_tests",
"//components/sessions:unit_tests", "//components/sessions:unit_tests",
"//components/shared_highlighting/core/common:unit_tests",
"//components/signin/core/browser:unit_tests", "//components/signin/core/browser:unit_tests",
"//components/signin/internal/identity_manager:unit_tests", "//components/signin/internal/identity_manager:unit_tests",
"//components/signin/public/base:unit_tests", "//components/signin/public/base:unit_tests",
...@@ -620,6 +621,7 @@ if (!is_ios && !is_fuchsia) { ...@@ -620,6 +621,7 @@ if (!is_ios && !is_fuchsia) {
"//components/performance_manager:browser_tests", "//components/performance_manager:browser_tests",
"//components/security_state/content", "//components/security_state/content",
"//components/security_state/core", "//components/security_state/core",
"//components/shared_highlighting/core/common",
"//components/strings", "//components/strings",
"//components/ukm:test_support", "//components/ukm:test_support",
"//components/ukm/content", "//components/ukm/content",
......
...@@ -3,5 +3,25 @@ ...@@ -3,5 +3,25 @@
# found in the LICENSE file. # found in the LICENSE file.
static_library("common") { static_library("common") {
sources = [ "shared_highlighting_metrics.h" ] sources = [
"shared_highlighting_metrics.cc",
"shared_highlighting_metrics.h",
]
deps = [
"//base",
"//components/search_engines:search_engine_utils",
"//services/metrics/public/cpp:metrics_cpp",
]
}
source_set("unit_tests") {
testonly = true
sources = [ "shared_highlighting_metrics_unittest.cc" ]
deps = [
"//base/test:test_support",
"//components/shared_highlighting/core/common",
"//testing/gtest",
"//url",
]
} }
include_rules = [
"+base/metrics/histogram_functions.h",
"+components/search_engines/search_engine_utils.h",
"+services/metrics/public",
"+url",
]
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/shared_highlighting/core/common/shared_highlighting_metrics.h"
#include "base/metrics/histogram_functions.h"
#include "base/notreached.h"
#include "components/search_engines/search_engine_utils.h"
namespace shared_highlighting {
namespace {
TextFragmentLinkOpenSource GetLinkSource(const GURL& referrer) {
bool from_search_engine = SearchEngineUtils::GetEngineType(referrer) > 0;
return from_search_engine ? TextFragmentLinkOpenSource::kSearchEngine
: TextFragmentLinkOpenSource::kUnknown;
}
} // namespace
void LogTextFragmentAmbiguousMatch(bool ambiguous_match) {
base::UmaHistogramBoolean("TextFragmentAnchor.AmbiguousMatch",
ambiguous_match);
}
void LogTextFragmentLinkOpenSource(const GURL& referrer) {
base::UmaHistogramEnumeration("TextFragmentAnchor.LinkOpenSource",
GetLinkSource(referrer));
}
void LogTextFragmentMatchRate(int matches, int text_fragments) {
if (text_fragments == 0) {
NOTREACHED();
return;
}
const int match_rate_percent =
static_cast<int>(100 * ((matches + 0.0) / text_fragments));
base::UmaHistogramPercentage("TextFragmentAnchor.MatchRate",
match_rate_percent);
}
void LogTextFragmentSelectorCount(int count) {
base::UmaHistogramCounts100("TextFragmentAnchor.SelectorCount", count);
}
} // namespace shared_highlighting
...@@ -5,10 +5,12 @@ ...@@ -5,10 +5,12 @@
#ifndef COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_ #ifndef COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_
#define COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_ #define COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_
#include "url/gurl.h"
namespace shared_highlighting { namespace shared_highlighting {
// Update corresponding |LinkGenerationError| in enums.xml. // Update corresponding |LinkGenerationError| in enums.xml.
enum LinkGenerationError { enum class LinkGenerationError {
kIncorrectSelector, kIncorrectSelector,
kNoRange, kNoRange,
kNoContext, kNoContext,
...@@ -16,8 +18,33 @@ enum LinkGenerationError { ...@@ -16,8 +18,33 @@ enum LinkGenerationError {
kContextLimitReached, kContextLimitReached,
kEmptySelection, kEmptySelection,
kMaxValue = kContextLimitReached kMaxValue = kEmptySelection,
}; };
// Update corresponding |TextFragmentLinkOpenSource| in enums.xml.
enum class TextFragmentLinkOpenSource {
kUnknown,
kSearchEngine,
kMaxValue = kSearchEngine,
};
// Records whether an individual text fragment could not be scrolled to because
// there was an |ambiguous_match| (generally because more than one matching
// passage was found).
void LogTextFragmentAmbiguousMatch(bool ambiguous_match);
// Records the source of the text fragment based on its |referrer|. E.g. a
// search engine.
void LogTextFragmentLinkOpenSource(const GURL& referrer);
// Records the success rate, which is the number of |matches| over number of
// |text_fragments| in the url param.
void LogTextFragmentMatchRate(int matches, int text_fragments);
// Records the total |count| of text fragment selectors in the URL param.
void LogTextFragmentSelectorCount(int count);
} // namespace shared_highlighting } // namespace shared_highlighting
#endif // COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_ #endif // COMPONENTS_SHARED_HIGHLIGHTING_CORE_COMMON_SHARED_HIGHLIGHTING_METRICS_H_
\ No newline at end of file
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/shared_highlighting/core/common/shared_highlighting_metrics.h"
#include "base/test/metrics/histogram_tester.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace shared_highlighting {
namespace {
TEST(SharedHighlightingMetricsTest, LogTextFragmentAmbiguousMatch) {
base::HistogramTester histogram_tester;
LogTextFragmentAmbiguousMatch(true);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.AmbiguousMatch", 1, 1);
LogTextFragmentAmbiguousMatch(false);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.AmbiguousMatch", 0, 1);
histogram_tester.ExpectTotalCount("TextFragmentAnchor.AmbiguousMatch", 2);
}
TEST(SharedHighlightingMetricsTest, LogTextFragmentLinkOpenSource) {
base::HistogramTester histogram_tester;
GURL search_engine_url = GURL("https://google.com");
GURL non_search_engine_url = GURL("https://example.com");
LogTextFragmentLinkOpenSource(search_engine_url);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.LinkOpenSource", 1, 1);
LogTextFragmentLinkOpenSource(non_search_engine_url);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.LinkOpenSource", 0, 1);
histogram_tester.ExpectTotalCount("TextFragmentAnchor.LinkOpenSource", 2);
}
TEST(SharedHighlightingMetricsTest, LogTextFragmentMatchRate) {
base::HistogramTester histogram_tester;
LogTextFragmentMatchRate(/*matches=*/2, /*nb_selectors=*/2);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.MatchRate", 100, 1);
LogTextFragmentMatchRate(/*matches=*/1, /*nb_selectors=*/2);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.MatchRate", 50, 1);
histogram_tester.ExpectTotalCount("TextFragmentAnchor.MatchRate", 2);
}
TEST(SharedHighlightingMetricsTest, LogTextFragmentSelectorCount) {
base::HistogramTester histogram_tester;
LogTextFragmentSelectorCount(1);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.SelectorCount", 1, 1);
LogTextFragmentSelectorCount(20);
histogram_tester.ExpectBucketCount("TextFragmentAnchor.SelectorCount", 20, 1);
histogram_tester.ExpectTotalCount("TextFragmentAnchor.SelectorCount", 2);
}
} // namespace
} // namespace shared_highlighting
...@@ -14506,9 +14506,18 @@ should be kept until we remove incident reporting. --> ...@@ -14506,9 +14506,18 @@ should be kept until we remove incident reporting. -->
<owner>gayane@chromium.org</owner> <owner>gayane@chromium.org</owner>
<owner>chrome-shared-highlighting@google.com</owner> <owner>chrome-shared-highlighting@google.com</owner>
<summary> <summary>
Recorded for every navigatin to a link with a valid text fragment selector Recorded for every navigation to a link with a valid text fragment selector
(e.g. #:=:text=SELECTOR). Indicates source type that navigation originated (e.g. #:=:text=SELECTOR). Indicates source type that navigation originated
from. from. It only know about search engines that come from the pre-populated
list installed with Chrome. If the user uses a search engine not on the
list, it won't count as a search engine. This is even true if the user have
a custom search engine. These custom search engines can be created by hand
or auto-generated. In either case, it's not going to count as a search
engine. Also, it only checks to see if the *referrer* is from a domain
associated with search engine. If someone posts a link somewhere on a web
site that happens to have a search engine known to Google and that links
includes the fragment tokens, then if a user clicks that link, it'll look
like it came from a search engine even thought they did not.
</summary> </summary>
</histogram> </histogram>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment