diff --git a/browser/search_engines/search_engine_tracker.cc b/browser/search_engines/search_engine_tracker.cc index e3bf4771f943..a0e07bf8cbec 100644 --- a/browser/search_engines/search_engine_tracker.cc +++ b/browser/search_engines/search_engine_tracker.cc @@ -44,6 +44,39 @@ void RecordSearchEngineP3A(const GURL& search_engine_url, UMA_HISTOGRAM_ENUMERATION(kDefaultSearchEngineMetric, answer); } +SearchEngineSwitchP3A SearchEngineSwitchP3AMapAnswer(const GURL& to, + const GURL& from) { + SearchEngineSwitchP3A answer; + + DCHECK(from.is_valid()); + DCHECK(to.is_valid()); + + if (from.DomainIs("brave.com")) { + // Switching away from Brave Search. + if (to.DomainIs("google.com")) { + answer = SearchEngineSwitchP3A::kBraveToGoogle; + } else if (to.DomainIs("duckduckgo.com")) { + answer = SearchEngineSwitchP3A::kBraveToDDG; + } else { + answer = SearchEngineSwitchP3A::kBraveToOther; + } + } else if (to.DomainIs("brave.com")) { + // Switching to Brave Search. + if (from.DomainIs("google.com")) { + answer = SearchEngineSwitchP3A::kGoogleToBrave; + } else if (from.DomainIs("duckduckgo.com")) { + answer = SearchEngineSwitchP3A::kDDGToBrave; + } else { + answer = SearchEngineSwitchP3A::kOtherToBrave; + } + } else { + // Any other transition. + answer = SearchEngineSwitchP3A::kOtherToOther; + } + + return answer; +} + } // namespace // static @@ -89,7 +122,9 @@ SearchEngineTracker::SearchEngineTracker( const GURL url = template_url->GenerateSearchURL(search_terms); if (!url.is_empty()) { default_search_url_ = url; + previous_search_url_ = url; RecordSearchEngineP3A(url, template_url->GetEngineType(search_terms)); + RecordSwitchP3A(url); } } } @@ -106,5 +141,17 @@ void SearchEngineTracker::OnTemplateURLServiceChanged() { if (url != default_search_url_) { RecordSearchEngineP3A(url, template_url->GetEngineType(search_terms)); } + RecordSwitchP3A(url); } } + +void SearchEngineTracker::RecordSwitchP3A(const GURL& url) { + auto answer = SearchEngineSwitchP3A::kNoSwitch; + + if (url.is_valid() && url != previous_search_url_) { + answer = SearchEngineSwitchP3AMapAnswer(url, previous_search_url_); + previous_search_url_ = url; + } + + UMA_HISTOGRAM_ENUMERATION(kSwitchSearchEngineMetric, answer); +} diff --git a/browser/search_engines/search_engine_tracker.h b/browser/search_engines/search_engine_tracker.h index 14dbf1f597ae..5f49ad7e9fd6 100644 --- a/browser/search_engines/search_engine_tracker.h +++ b/browser/search_engines/search_engine_tracker.h @@ -17,6 +17,7 @@ // Exposed for tests. constexpr char kDefaultSearchEngineMetric[] = "Brave.Search.DefaultEngine.4"; +constexpr char kSwitchSearchEngineMetric[] = "Brave.Search.SwitchEngine"; // Note: append-only enumeration! Never remove any existing values, as this enum // is used to bucket a UMA histogram, and removing values breaks that. @@ -33,6 +34,18 @@ enum class SearchEngineP3A { kMaxValue = kBrave, }; +enum class SearchEngineSwitchP3A { + kNoSwitch, + kBraveToGoogle, + kBraveToDDG, + kBraveToOther, + kGoogleToBrave, + kDDGToBrave, + kOtherToBrave, + kOtherToOther, + kMaxValue = kOtherToOther, +}; + class SearchEngineTrackerFactory : public BrowserContextKeyedServiceFactory { public: static SearchEngineTrackerFactory* GetInstance(); @@ -69,8 +82,11 @@ class SearchEngineTracker : public KeyedService, base::ScopedObservation observer_{this}; + void RecordSwitchP3A(const GURL& url); + // Keeping this to check for changes in |OnTemplateURLServiceChanged|. GURL default_search_url_; + GURL previous_search_url_; TemplateURLService* template_url_service_; }; diff --git a/components/p3a/brave_p3a_service.cc b/components/p3a/brave_p3a_service.cc index 4ef1a0bd164a..7d5cd8443099 100644 --- a/components/p3a/brave_p3a_service.cc +++ b/components/p3a/brave_p3a_service.cc @@ -82,6 +82,7 @@ constexpr const char* kCollectedHistograms[] = { "Brave.Rewards.WalletState", "Brave.Savings.BandwidthSavingsMB", "Brave.Search.DefaultEngine.4", + "Brave.Search.SwitchEngine", "Brave.Shields.UsageStatus", "Brave.SpeedReader.Enabled", "Brave.SpeedReader.ToggleCount",