Skip to content

Commit

Permalink
P3A: Record default search engine changes.
Browse files Browse the repository at this point in the history
Add a P3A probe to record when the default search engine is changed,
and whether it's a transition between Brave Search or one of the
other major services. This helps understand what options are seem
better to users.

Keep track of the previous default search engine url. The event
the tracker observes only indicates something has changed, and
by then the previous state is no longer accessible; we need this
to correctly mark the transition in our matrix, separate from
whatever the original default was at startup.

Move the actual record step into an object method so it can access
the saved previous default. This simplifies calling it at startup
to set the initial kNoSwitch answer.

Note: this means we only report a change if the ping is sent from
the same application instance. We don't reset to kNoSwitch at the
end of the weekly sample cadence, and restarting the browser always
resets.

Closes brave/brave-browser#18224
  • Loading branch information
rillian committed Oct 20, 2021
1 parent 27f19ff commit adc0a81
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
47 changes: 47 additions & 0 deletions browser/search_engines/search_engine_tracker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
16 changes: 16 additions & 0 deletions browser/search_engines/search_engine_tracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down Expand Up @@ -69,8 +82,11 @@ class SearchEngineTracker : public KeyedService,
base::ScopedObservation<TemplateURLService, TemplateURLServiceObserver>
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_;
};
Expand Down
1 change: 1 addition & 0 deletions components/p3a/brave_p3a_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit adc0a81

Please sign in to comment.