-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/misc_metrics/profile_new_tab_metrics.h" | ||
|
||
#include <optional> | ||
|
||
#include "base/containers/contains.h" | ||
#include "base/metrics/histogram_macros.h" | ||
#include "brave/browser/new_tab/new_tab_shows_options.h" | ||
#include "brave/components/constants/pref_names.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_service.h" | ||
#include "url/gurl.h" | ||
|
||
namespace misc_metrics { | ||
|
||
ProfileNewTabMetrics::ProfileNewTabMetrics(PrefService* profile_prefs) | ||
: profile_prefs_(profile_prefs) { | ||
pref_change_registrar_.Init(profile_prefs_); | ||
|
||
auto callback = base::BindRepeating( | ||
&ProfileNewTabMetrics::ReportNewTabPageDefault, base::Unretained(this)); | ||
pref_change_registrar_.Add(kNewTabPageShowsOptions, callback); | ||
pref_change_registrar_.Add(prefs::kHomePage, callback); | ||
pref_change_registrar_.Add(prefs::kHomePageIsNewTabPage, callback); | ||
|
||
// Report the initial state | ||
ReportNewTabPageDefault(); | ||
} | ||
|
||
ProfileNewTabMetrics::~ProfileNewTabMetrics() = default; | ||
|
||
void ProfileNewTabMetrics::ReportNewTabPageDefault() { | ||
std::optional<NewTabPageDefaultType> type; | ||
brave::NewTabPageShowsOptions option = | ||
static_cast<brave::NewTabPageShowsOptions>( | ||
profile_prefs_->GetInteger(kNewTabPageShowsOptions)); | ||
|
||
if (option == brave::NewTabPageShowsOptions::kDashboard) { | ||
type = NewTabPageDefaultType::kDashboard; | ||
} else if (option == brave::NewTabPageShowsOptions::kHomepage) { | ||
if (profile_prefs_->GetBoolean(prefs::kHomePageIsNewTabPage)) { | ||
type = NewTabPageDefaultType::kDashboard; | ||
} else { | ||
GURL homepage_url(profile_prefs_->GetString(prefs::kHomePage)); | ||
|
||
if (homepage_url.host() == "search.brave.com") { | ||
type = NewTabPageDefaultType::kHomepageBraveSearch; | ||
} else if (base::Contains(homepage_url.host(), "google")) { | ||
type = NewTabPageDefaultType::kHomepageGoogle; | ||
} else if (base::Contains(homepage_url.host(), "duckduckgo")) { | ||
type = NewTabPageDefaultType::kHomepageDuckDuckGo; | ||
} else { | ||
type = NewTabPageDefaultType::kHomepageOther; | ||
} | ||
} | ||
} else if (option == brave::NewTabPageShowsOptions::kBlankpage) { | ||
type = NewTabPageDefaultType::kBlank; | ||
} | ||
|
||
if (type) { | ||
UMA_HISTOGRAM_ENUMERATION(kNewTabPageDefaultHistogramName, *type); | ||
} | ||
} | ||
|
||
} // namespace misc_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_METRICS_H_ | ||
#define BRAVE_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_METRICS_H_ | ||
|
||
#include "components/prefs/pref_change_registrar.h" | ||
|
||
class PrefService; | ||
|
||
namespace misc_metrics { | ||
|
||
inline constexpr char kNewTabPageDefaultHistogramName[] = | ||
"Brave.NTP.DefaultPage"; | ||
|
||
enum class NewTabPageDefaultType { | ||
kDashboard = 0, | ||
kBlank = 1, | ||
kHomepageBraveSearch = 2, | ||
kHomepageGoogle = 3, | ||
kHomepageDuckDuckGo = 4, | ||
kHomepageOther = 5, | ||
kMaxValue = kHomepageOther | ||
}; | ||
|
||
class ProfileNewTabMetrics { | ||
public: | ||
explicit ProfileNewTabMetrics(PrefService* profile_prefs); | ||
~ProfileNewTabMetrics(); | ||
|
||
ProfileNewTabMetrics(const ProfileNewTabMetrics&) = delete; | ||
ProfileNewTabMetrics& operator=(const ProfileNewTabMetrics&) = delete; | ||
|
||
private: | ||
void ReportNewTabPageDefault(); | ||
|
||
raw_ptr<PrefService> profile_prefs_; | ||
PrefChangeRegistrar pref_change_registrar_; | ||
}; | ||
|
||
} // namespace misc_metrics | ||
|
||
#endif // BRAVE_BROWSER_MISC_METRICS_PROFILE_NEW_TAB_METRICS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* Copyright (c) 2025 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/browser/misc_metrics/profile_new_tab_metrics.h" | ||
|
||
#include "base/test/metrics/histogram_tester.h" | ||
#include "brave/browser/new_tab/new_tab_shows_options.h" | ||
#include "brave/components/constants/pref_names.h" | ||
#include "chrome/common/pref_names.h" | ||
#include "components/prefs/pref_registry_simple.h" | ||
#include "components/prefs/testing_pref_service.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace misc_metrics { | ||
|
||
class ProfileNewTabMetricsTest : public testing::Test { | ||
public: | ||
ProfileNewTabMetricsTest() = default; | ||
|
||
void SetUp() override { | ||
pref_service_.registry()->RegisterIntegerPref( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kDashboard)); | ||
pref_service_.registry()->RegisterStringPref(prefs::kHomePage, ""); | ||
pref_service_.registry()->RegisterBooleanPref(prefs::kHomePageIsNewTabPage, | ||
false); | ||
} | ||
|
||
protected: | ||
TestingPrefServiceSimple pref_service_; | ||
base::HistogramTester histogram_tester_; | ||
}; | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestDashboardOption) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kDashboard)); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 0, 1); | ||
} | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestBlankOption) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kBlankpage)); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 1, 1); | ||
} | ||
|
||
TEST_F(ProfileNewTabMetricsTest, TestHomepageOptions) { | ||
pref_service_.SetInteger( | ||
kNewTabPageShowsOptions, | ||
static_cast<int>(brave::NewTabPageShowsOptions::kHomepage)); | ||
|
||
pref_service_.SetBoolean(prefs::kHomePageIsNewTabPage, true); | ||
|
||
ProfileNewTabMetrics metrics(&pref_service_); | ||
|
||
histogram_tester_.ExpectUniqueSample(kNewTabPageDefaultHistogramName, 0, 1); | ||
|
||
histogram_tester_.ExpectTotalCount(kNewTabPageDefaultHistogramName, 1); | ||
|
||
pref_service_.SetBoolean(prefs::kHomePageIsNewTabPage, false); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 5, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://search.brave.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 2, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://www.google.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 3, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://duckduckgo.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 4, 1); | ||
|
||
pref_service_.SetString(prefs::kHomePage, "https://example.com"); | ||
|
||
histogram_tester_.ExpectBucketCount(kNewTabPageDefaultHistogramName, 5, 2); | ||
|
||
histogram_tester_.ExpectTotalCount(kNewTabPageDefaultHistogramName, 6); | ||
} | ||
|
||
} // namespace misc_metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters