Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ads] Fix NTP media type P3A metrics when modified from Rewards page (uplift to 1.70.x) #25587

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions browser/profiles/profile_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "brave/components/brave_shields/core/common/brave_shield_utils.h"
#include "brave/components/constants/brave_constants.h"
#include "brave/components/constants/pref_names.h"
#include "brave/components/ntp_background_images/browser/ntp_p3a_util.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "brave/components/search_engines/brave_prepopulated_engines.h"
#include "brave/components/tor/buildflags/buildflags.h"
Expand Down Expand Up @@ -45,15 +46,6 @@ bool IsTorDisabledForProfile(Profile* profile) {
#endif
}

void RecordSponsoredImagesEnabledP3A(Profile* profile) {
bool is_sponsored_image_enabled =
profile->GetPrefs()->GetBoolean(kNewTabPageShowBackgroundImage) &&
profile->GetPrefs()->GetBoolean(
kNewTabPageShowSponsoredImagesBackgroundImage);
UMA_HISTOGRAM_BOOLEAN("Brave.NTP.SponsoredMediaType",
is_sponsored_image_enabled);
}

void RecordInitialP3AValues(Profile* profile) {
// Preference is unregistered for some reason in profile_manager_unittest
// TODO(bsclifton): create a proper testing profile
Expand All @@ -62,7 +54,7 @@ void RecordInitialP3AValues(Profile* profile) {
kNewTabPageShowSponsoredImagesBackgroundImage)) {
return;
}
RecordSponsoredImagesEnabledP3A(profile);
ntp_background_images::RecordSponsoredImagesEnabledP3A(profile->GetPrefs());
if (profile->IsRegularProfile()) {
brave_shields::MaybeRecordInitialShieldsSettings(
profile->GetPrefs(),
Expand Down
4 changes: 0 additions & 4 deletions browser/profiles/profile_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ namespace brave {

bool IsTorDisabledForProfile(Profile* profile);

// Specifically used to record if sponsored images are enabled.
// Called from BraveAppearanceHandler and BraveNewTabMessageHandler
void RecordSponsoredImagesEnabledP3A(Profile* profile);

// Records default values for some histograms.
//
// For profile agnostic values (ex: local_state) see
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,6 @@ void BraveNewTabMessageHandler::HandleSaveNewTabPagePref(
return;
}
prefs->SetBoolean(settingsKey, settingsValueBool);

// P3A can only be recorded after profile is updated
if (settingsKeyInput == "showBackgroundImage" ||
settingsKeyInput == "brandedWallpaperOptIn") {
brave::RecordSponsoredImagesEnabledP3A(profile_);
}
}

void BraveNewTabMessageHandler::HandleRegisterNewTabPageView(
Expand Down
5 changes: 0 additions & 5 deletions browser/ui/webui/settings/brave_appearance_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,6 @@ void BraveAppearanceHandler::OnBraveDarkModeChanged() {
}
}

void BraveAppearanceHandler::OnBackgroundPreferenceChanged(
const std::string& pref_name) {
brave::RecordSponsoredImagesEnabledP3A(profile_);
}

void BraveAppearanceHandler::OnPreferenceChanged(const std::string& pref_name) {
if (IsJavascriptAllowed()) {
if (pref_name == kNewTabPageShowsOptions || pref_name == prefs::kHomePage ||
Expand Down
1 change: 0 additions & 1 deletion browser/ui/webui/settings/brave_appearance_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class BraveAppearanceHandler : public settings::SettingsPageUIHandler {
void OnJavascriptDisallowed() override {}

void OnBraveDarkModeChanged();
void OnBackgroundPreferenceChanged(const std::string& pref_name);
void OnPreferenceChanged(const std::string& pref_name);
void SetBraveThemeType(const base::Value::List& args);
void GetBraveThemeType(const base::Value::List& args);
Expand Down
2 changes: 2 additions & 0 deletions components/ntp_background_images/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ static_library("browser") {
"ntp_background_images_service.cc",
"ntp_background_images_service.h",
"ntp_p3a_helper.h",
"ntp_p3a_util.cc",
"ntp_p3a_util.h",
"ntp_sponsored_images_data.cc",
"ntp_sponsored_images_data.h",
"sponsored_images_component_data.cc",
Expand Down
24 changes: 24 additions & 0 deletions components/ntp_background_images/browser/ntp_p3a_util.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) 2024 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/components/ntp_background_images/browser/ntp_p3a_util.h"

#include "base/metrics/histogram_macros.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "components/prefs/pref_service.h"

namespace ntp_background_images {

void RecordSponsoredImagesEnabledP3A(const PrefService* const prefs) {
CHECK(prefs);

const bool is_sponsored_image_enabled =
prefs->GetBoolean(prefs::kNewTabPageShowBackgroundImage) &&
prefs->GetBoolean(prefs::kNewTabPageShowSponsoredImagesBackgroundImage);
UMA_HISTOGRAM_BOOLEAN("Brave.NTP.SponsoredMediaType",
is_sponsored_image_enabled);
}

} // namespace ntp_background_images
18 changes: 18 additions & 0 deletions components/ntp_background_images/browser/ntp_p3a_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2024 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_COMPONENTS_NTP_BACKGROUND_IMAGES_BROWSER_NTP_P3A_UTIL_H_
#define BRAVE_COMPONENTS_NTP_BACKGROUND_IMAGES_BROWSER_NTP_P3A_UTIL_H_

class PrefService;

namespace ntp_background_images {

// Specifically used to record if sponsored images are enabled.
void RecordSponsoredImagesEnabledP3A(const PrefService* prefs);

} // namespace ntp_background_images

#endif // BRAVE_COMPONENTS_NTP_BACKGROUND_IMAGES_BROWSER_NTP_P3A_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "brave/components/ntp_background_images/browser/brave_ntp_custom_background_service.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_data.h"
#include "brave/components/ntp_background_images/browser/ntp_p3a_helper.h"
#include "brave/components/ntp_background_images/browser/ntp_p3a_util.h"
#include "brave/components/ntp_background_images/browser/ntp_sponsored_images_data.h"
#include "brave/components/ntp_background_images/browser/url_constants.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
Expand Down Expand Up @@ -325,6 +326,11 @@ void ViewCounterService::OnPreferenceChanged(const std::string& pref_name) {
return;
}

if (pref_name == prefs::kNewTabPageShowBackgroundImage ||
pref_name == prefs::kNewTabPageShowSponsoredImagesBackgroundImage) {
RecordSponsoredImagesEnabledP3A(prefs_);
}

// Reset model because SI and SR use different policy.
// Start from initial model state whenever
// prefs::kNewTabPageSuperReferralThemesOption or
Expand Down
Loading