Skip to content

Commit

Permalink
Added Brave.NTP.NewTabsCreated and Brave.NTP.SponsoredNewTabsCreated
Browse files Browse the repository at this point in the history
  • Loading branch information
bsclifton committed Nov 2, 2020
1 parent b3d3910 commit b69e1b2
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
2 changes: 2 additions & 0 deletions browser/brave_local_state_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "brave/components/brave_shields/browser/brave_shields_p3a.h"
#include "brave/components/ipfs/buildflags/buildflags.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/ntp_background_images/browser/view_counter_service.h"
#include "brave/components/p3a/brave_p3a_service.h"
#include "brave/components/p3a/buildflags.h"
#include "brave/components/tor/buildflags/buildflags.h"
Expand Down Expand Up @@ -53,6 +54,7 @@ void RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
brave_stats::RegisterLocalStatePrefs(registry);
ntp_background_images::NTPBackgroundImagesService::RegisterLocalStatePrefs(
registry);
ntp_background_images::ViewCounterService::RegisterLocalStatePrefs(registry);
#if BUILDFLAG(ENABLE_BRAVE_REFERRALS)
RegisterPrefsForBraveReferralsService(registry);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ KeyedService* ViewCounterServiceFactory::BuildServiceInstanceFor(
return new ViewCounterService(service,
ads_service,
profile->GetPrefs(),
g_brave_browser_process->local_state(),
is_supported_locale);
}

Expand Down
5 changes: 3 additions & 2 deletions components/ntp_background_images/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ static_library("browser") {
"//base",
"//brave/components/brave_ads/browser",
"//brave/components/brave_ads/common",
"//brave/components/brave_component_updater/browser",
"//brave/components/brave_referrals/browser",
"//brave/components/brave_referrals/common",
"//brave/components/brave_rewards/common",
"//brave/components/brave_component_updater/browser",
"//brave/components/l10n/browser",
"//brave/components/l10n/common",
"//brave/components/ntp_background_images/common",
"//brave/components/weekly_storage",
"//brave/vendor/bat-native-ads",
"//components/component_updater",
"//components/keyed_service/core",
"//components/prefs",
"//components/pref_registry:pref_registry",
"//components/prefs",
"//content/public/browser",
"//content/public/common",
"//crypto",
Expand Down
49 changes: 49 additions & 0 deletions components/ntp_background_images/browser/view_counter_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/metrics/histogram_macros.h"
#include "bat/ads/pref_names.h"
#include "bat/ads/public/interfaces/ads.mojom.h"
#include "brave/components/brave_referrals/buildflags/buildflags.h"
Expand All @@ -21,13 +22,25 @@
#include "brave/components/ntp_background_images/browser/ntp_background_images_data.h"
#include "brave/components/ntp_background_images/browser/url_constants.h"
#include "brave/components/ntp_background_images/common/pref_names.h"
#include "brave/components/weekly_storage/weekly_storage.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/web_ui_data_source.h"

namespace {
constexpr char kNewTabsCreated[] = "brave.new_tab_page.p3a_new_tabs_created";
constexpr char kSponsoredNewTabsCreated[] =
"brave.new_tab_page.p3a_sponsored_new_tabs_created";
}

namespace ntp_background_images {

// static
void ViewCounterService::RegisterLocalStatePrefs(PrefRegistrySimple* registry) {
registry->RegisterListPref(kNewTabsCreated);
registry->RegisterListPref(kSponsoredNewTabsCreated);
}

void ViewCounterService::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(
Expand All @@ -45,6 +58,7 @@ void ViewCounterService::RegisterProfilePrefs(
ViewCounterService::ViewCounterService(NTPBackgroundImagesService* service,
brave_ads::AdsService* ads_service,
PrefService* prefs,
PrefService* local_state,
bool is_supported_locale)
: service_(service),
ads_service_(ads_service),
Expand All @@ -53,6 +67,11 @@ ViewCounterService::ViewCounterService(NTPBackgroundImagesService* service,
DCHECK(service_);
service_->AddObserver(this);

new_tab_count_state_ =
std::make_unique<WeeklyStorage>(local_state, kNewTabsCreated);
branded_new_tab_count_state_ =
std::make_unique<WeeklyStorage>(local_state, kSponsoredNewTabsCreated);

if (auto* data = GetCurrentBrandedWallpaperData())
model_.set_total_image_count(data->backgrounds.size());

Expand Down Expand Up @@ -81,6 +100,9 @@ void ViewCounterService::BrandedWallpaperWillBeDisplayed(

ads_service_->OnNewTabPageAdEvent(wallpaper_id, creative_instance_id,
ads::mojom::BraveAdsNewTabPageAdEventType::kViewed);

branded_new_tab_count_state_->AddDelta(1);
UpdateP3AValues();
}

NTPBackgroundImagesData*
Expand Down Expand Up @@ -185,6 +207,9 @@ void ViewCounterService::ResetNotificationState() {
}

void ViewCounterService::RegisterPageView() {
new_tab_count_state_->AddDelta(1);
UpdateP3AValues();

// Don't do any counting if we will never be showing the data
// since we want the count to start at the point of data being available
// or the user opt-in status changing.
Expand Down Expand Up @@ -253,4 +278,28 @@ std::string ViewCounterService::GetSuperReferralCode() const {
return service_->GetSuperReferralCode();
}

void ViewCounterService::UpdateP3AValues() const {
uint64_t new_tab_count = new_tab_count_state_->GetHighestValueInWeek();
constexpr int kNewTabCount[] = {0, 1, 4, 9, 21, 51, 101};
const int* it_count =
std::lower_bound(kNewTabCount, std::end(kNewTabCount), new_tab_count);
int answer = it_count - kNewTabCount;
UMA_HISTOGRAM_EXACT_LINEAR("Brave.NTP.NewTabsCreated", answer,
7);

uint64_t branded_new_tab_count =
branded_new_tab_count_state_->GetHighestValueInWeek();
if (branded_new_tab_count == 0 || new_tab_count == 0) {
answer = 0;
} else {
double ratio = (branded_new_tab_count / (double)new_tab_count) * 100;
constexpr double kSponsoredRatio[] = {1.0, 10.0, 20.0, 30.0, 40.0, 50.0};
const double* it_ratio =
std::lower_bound(kSponsoredRatio, std::end(kSponsoredRatio), ratio);
answer = it_ratio - kSponsoredRatio;
}
UMA_HISTOGRAM_EXACT_LINEAR("Brave.NTP.SponsoredNewTabsCreated", answer,
7);
}

} // namespace ntp_background_images
15 changes: 13 additions & 2 deletions components/ntp_background_images/browser/view_counter_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
#include <vector>

#include "base/values.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"
#include "brave/components/brave_ads/browser/ads_service.h"
#include "brave/components/ntp_background_images/browser/ntp_background_images_service.h"
#include "brave/components/ntp_background_images/browser/view_counter_model.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/prefs/pref_change_registrar.h"

class PrefService;

Expand All @@ -27,6 +27,8 @@ namespace user_prefs {
class PrefRegistrySyncable;
} // namespace user_prefs

class WeeklyStorage;

namespace ntp_background_images {

struct NTPBackgroundImagesData;
Expand All @@ -38,12 +40,14 @@ class ViewCounterService : public KeyedService,
ViewCounterService(NTPBackgroundImagesService* service,
brave_ads::AdsService* ads_service,
PrefService* prefs,
PrefService* local_state,
bool is_supported_locale);
~ViewCounterService() override;

ViewCounterService(const ViewCounterService&) = delete;
ViewCounterService& operator=(const ViewCounterService&) = delete;

static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

// Lets the counter know that a New Tab Page view has occured.
Expand Down Expand Up @@ -116,12 +120,19 @@ class ViewCounterService : public KeyedService,

void ResetModel();

void UpdateP3AValues() const;

NTPBackgroundImagesService* service_ = nullptr; // not owned
brave_ads::AdsService* ads_service_ = nullptr; // not owned
PrefService* prefs_ = nullptr; // not owned
bool is_supported_locale_ = false;
PrefChangeRegistrar pref_change_registrar_;
ViewCounterModel model_;

// If P3A is enabled, these will track number of tabs created
// and the ratio of those which are branded images.
std::unique_ptr<WeeklyStorage> new_tab_count_state_;
std::unique_ptr<WeeklyStorage> branded_new_tab_count_state_;
};

} // namespace ntp_background_images
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ class NTPBackgroundImagesViewCounterTest : public testing::Test {
auto* local_registry = local_pref_.registry();
brave::RegisterPrefsForBraveReferralsService(local_registry);
NTPBackgroundImagesService::RegisterLocalStatePrefs(local_registry);
ViewCounterService::RegisterLocalStatePrefs(local_registry);

service_ = std::make_unique<NTPBackgroundImagesService>(nullptr,
&local_pref_);
view_counter_ = std::make_unique<ViewCounterService>(
service_.get(), nullptr, prefs(), true);
service_.get(), nullptr, prefs(), &local_pref_, true);

// Set referral service is properly initialized sr component is set.
local_pref_.SetBoolean(kReferralCheckedForPromoCodeFile, true);
Expand Down

0 comments on commit b69e1b2

Please sign in to comment.