Skip to content

Commit

Permalink
Add UI for subdivision ad targeting
Browse files Browse the repository at this point in the history
  • Loading branch information
Moritz Haller committed May 31, 2020
1 parent d85f96a commit a3ec561
Show file tree
Hide file tree
Showing 40 changed files with 896 additions and 162 deletions.
6 changes: 6 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,12 @@ ExtensionFunction::ResponseAction BraveRewardsSaveAdsSettingFunction::Run() {
const auto is_enabled =
params->value == "true" && ads_service_->IsSupportedLocale();
ads_service_->SetEnabled(is_enabled);
} else if (params->key == "shouldAllowSubdivisionAdTargeting") {
ads_service_->SetAllowSubdivisionAdTargeting(params->value == "true");
} else if (params->key == "didOverrideAdsSubdivision") {
ads_service_->SetOverrideAdsSubdivision(params->value == "true");
} else if (params->key == "isSubdivisionAdTargetingRegion") {
ads_service_->SetSubdivisionAdTargetingRegion(params->value == "true");
}
}
return RespondNow(NoArguments());
Expand Down
29 changes: 29 additions & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ class RewardsDOMHandler : public WebUIMessageHandler,
namespace {

const int kDaysOfAdsHistory = 7;
const char kShouldAllowSubdivisionAdTargeting[] =
"shouldAllowSubdivisionAdTargeting";
const char kAdsSubdivision[] = "adsSubdivision";
const char kDidOverrideAdsSubdivision[] = "didOverrideAdsSubdivision";
const char kIsSubdivisionAdTargetingRegion[] = "isSubdivisionAdTargetingRegion";

} // namespace

Expand Down Expand Up @@ -1103,6 +1108,22 @@ void RewardsDOMHandler::GetAdsData(const base::ListValue *args) {
auto ads_per_hour = ads_service_->GetAdsPerHour();
ads_data.SetInteger("adsPerHour", ads_per_hour);

auto ads_subdivision = ads_service_->GetCountrySubdivision();
ads_data.SetString(kAdsSubdivision, ads_subdivision);

const auto should_allow_subdivision_ad_targeting =
ads_service_->ShouldAllowSubdivisionAdTargeting();
ads_data.SetBoolean(kShouldAllowSubdivisionAdTargeting,
should_allow_subdivision_ad_targeting);

auto did_override_ads_subdivision = ads_service_->DidOverrideAdsSubdivision();
ads_data.SetInteger(kDidOverrideAdsSubdivision, did_override_ads_subdivision);

auto is_subdivision_ad_targeting_region =
ads_service_->IsSubdivisionAdTargetingRegion();
ads_data.SetInteger(kIsSubdivisionAdTargetingRegion,
is_subdivision_ad_targeting_region);

#if BUILDFLAG(BRAVE_ADS_ENABLED)
auto ads_ui_enabled = true;
#else
Expand Down Expand Up @@ -1322,6 +1343,14 @@ void RewardsDOMHandler::SaveAdsSetting(const base::ListValue* args) {
ads_service_->SetEnabled(is_enabled);
} else if (key == "adsPerHour") {
ads_service_->SetAdsPerHour(std::stoull(value));
} else if (key == kAdsSubdivision) {
ads_service_->SetCountrySubdivision(value);
} else if (key == kShouldAllowSubdivisionAdTargeting) {
ads_service_->SetAllowSubdivisionAdTargeting(value == "true");
} else if (key == kDidOverrideAdsSubdivision) {
ads_service_->SetOverrideAdsSubdivision(value == "true");
} else if (key == kIsSubdivisionAdTargetingRegion) {
ads_service_->SetSubdivisionAdTargetingRegion(value == "true");
}

base::ListValue* emptyArgs = nullptr;
Expand Down
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "adsPerHour3", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_3 },
{ "adsPerHour4", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_4 },
{ "adsPerHour5", IDS_BRAVE_REWARDS_LOCAL_ADS_PER_HOUR_5 },
{ "adsSubdivisionTitle", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDIVISION_TITLE }, // NOLINT
{ "adsAllowSubdivisionTargetingTitle", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDEVISION_AD_TARGETING_TITLE }, // NOLINT
{ "adsAllowSubdivisionTargetingDescription", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDEVISION_AD_TARGETING_DESCRIPTION }, // NOLINT
{ "adsAllowSubdivisionTargetingLearn", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDEVISION_AD_TARGETING_LEARN }, // NOLINT
{ "adsAllowSubdivisionTargetingLabel", IDS_BRAVE_REWARDS_LOCAL_ADS_SUBDEVISION_AD_TARGETING_LABEL }, // NOLINT
{ "adsTitle", IDS_BRAVE_REWARDS_LOCAL_ADS_TITLE },

{ "bat", IDS_BRAVE_UI_BAT_REWARDS_TEXT },
Expand Down
16 changes: 16 additions & 0 deletions components/brave_ads/browser/ads_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ class AdsService : public KeyedService {
virtual void SetAllowAdConversionTracking(
const bool should_allow) = 0;

virtual bool ShouldAllowSubdivisionAdTargeting() const = 0;
virtual void SetAllowSubdivisionAdTargeting(
const bool should_allow) = 0;

virtual std::string GetCountrySubdivision() const = 0;
virtual void SetCountrySubdivision(
const std::string& country_subdivision) = 0;

virtual bool DidOverrideAdsSubdivision() const = 0;
virtual void SetOverrideAdsSubdivision(
const bool did_override) = 0;

virtual bool IsSubdivisionAdTargetingRegion() const = 0;
virtual void SetSubdivisionAdTargetingRegion(
const bool is_region) = 0;

virtual uint64_t GetAdsPerHour() const = 0;
virtual void SetAdsPerHour(
const uint64_t ads_per_hour) = 0;
Expand Down
6 changes: 6 additions & 0 deletions components/brave_ads/browser/ads_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ void AdsServiceFactory::RegisterProfilePrefs(

registry->RegisterBooleanPref(prefs::kShouldAllowAdConversionTracking, true);

registry->RegisterBooleanPref(prefs::kShouldAllowSubdivisionAdTargeting,
true);
registry->RegisterStringPref(prefs::kCountrySubdivision, "");
registry->RegisterBooleanPref(prefs::kDidOverrideAdsSubdivision, false);
registry->RegisterBooleanPref(prefs::kIsSubdivisionAdTargetingRegion, false);

registry->RegisterUint64Pref(prefs::kAdsPerHour, 2);
registry->RegisterUint64Pref(prefs::kAdsPerDay, 20);

Expand Down
36 changes: 36 additions & 0 deletions components/brave_ads/browser/ads_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,26 @@ void AdsServiceImpl::SetAllowAdConversionTracking(
SetBooleanPref(prefs::kShouldAllowAdConversionTracking, should_allow);
}

void AdsServiceImpl::SetAllowSubdivisionAdTargeting(
const bool should_allow) {
SetBooleanPref(prefs::kShouldAllowSubdivisionAdTargeting, should_allow);
}

void AdsServiceImpl::SetCountrySubdivision(
const std::string& country_subdivision) {
SetStringPref(prefs::kCountrySubdivision, country_subdivision);
}

void AdsServiceImpl::SetOverrideAdsSubdivision(
const bool did_override) {
SetBooleanPref(prefs::kDidOverrideAdsSubdivision, did_override);
}

void AdsServiceImpl::SetSubdivisionAdTargetingRegion(
const bool is_region) {
SetBooleanPref(prefs::kIsSubdivisionAdTargetingRegion, is_region);
}

void AdsServiceImpl::SetAdsPerHour(
const uint64_t ads_per_hour) {
SetUint64Pref(prefs::kAdsPerHour, ads_per_hour);
Expand Down Expand Up @@ -493,6 +513,22 @@ uint64_t AdsServiceImpl::GetAdsPerDay() const {
return GetUint64Pref(prefs::kAdsPerDay);
}

bool AdsServiceImpl::ShouldAllowSubdivisionAdTargeting() const {
return GetBooleanPref(prefs::kShouldAllowSubdivisionAdTargeting);
}

std::string AdsServiceImpl::GetCountrySubdivision() const {
return GetStringPref(prefs::kCountrySubdivision);
}

bool AdsServiceImpl::DidOverrideAdsSubdivision() const {
return GetBooleanPref(prefs::kDidOverrideAdsSubdivision);
}

bool AdsServiceImpl::IsSubdivisionAdTargetingRegion() const {
return GetBooleanPref(prefs::kIsSubdivisionAdTargetingRegion);
}

///////////////////////////////////////////////////////////////////////////////

void AdsServiceImpl::Shutdown() {
Expand Down
13 changes: 13 additions & 0 deletions components/brave_ads/browser/ads_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,19 @@ class AdsServiceImpl : public AdsService,
uint64_t GetAdsPerHour() const override;
uint64_t GetAdsPerDay() const override;

bool ShouldAllowSubdivisionAdTargeting() const override;
void SetAllowSubdivisionAdTargeting(
const bool should_allow) override;
std::string GetCountrySubdivision() const override;
void SetCountrySubdivision(
const std::string& country_subdivision) override;
bool DidOverrideAdsSubdivision() const override;
void SetOverrideAdsSubdivision(
const bool did_override) override;
bool IsSubdivisionAdTargetingRegion() const override;
void SetSubdivisionAdTargetingRegion(
const bool is_region) override;

// KeyedService implementation
void Shutdown() override;

Expand Down
16 changes: 16 additions & 0 deletions components/brave_ads/common/pref_names.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ const char kHasAdsP3AState[] = "brave.brave_ads.has_p3a_state";
// Stores the maximum amount of ads per hour
const char kAdsPerHour[] = "brave.brave_ads.ads_per_hour";

// Stores whether Brave ads should allow targeting based on country subdevision
const char kShouldAllowSubdivisionAdTargeting[] =
"brave.brave_ads.should_allow_subdivision_ad_targeting";

// Stores the country subdevision for targeting
const char kCountrySubdivision[] =
"brave.brave_ads.country_subdivision";

// Stores whether a user did manually set the country subdivision from settings
const char kDidOverrideAdsSubdivision[] =
"brave.brave_ads.did_override_ads_subdivision";

// Stores whether a locale is a subdivision ad targeting region
const char kIsSubdivisionAdTargetingRegion[] =
"brave.brave_ads.is_subdivision_ad_targeting_region";

// Stores the maximum amount of ads per day
const char kAdsPerDay[] = "brave.brave_ads.ads_per_day";

Expand Down
4 changes: 4 additions & 0 deletions components/brave_ads/common/pref_names.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ extern const char kEnabled[];

extern const char kShouldAllowAdConversionTracking[];

extern const char kShouldAllowSubdivisionAdTargeting[];
extern const char kCountrySubdivision[];
extern const char kDidOverrideAdsSubdivision[];
extern const char kIsSubdivisionAdTargetingRegion[];
extern const char kAdsPerHour[];
extern const char kAdsPerDay[];
extern const char kAdsWereDisabled[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { connect } from 'react-redux'
// Components
import BoxMobile, { Props as BoxMobileProps } from '../../ui/components/mobile/boxMobile'
import { List, NextContribution, Tokens } from '../../ui/components'
import { Grid, Column, Select, ControlWrapper } from 'brave-ui/components'
import { Grid, Column, Select, ControlWrapper, Checkbox } from 'brave-ui/components'
import AdsOnboarding from './adsOnboarding'
import {
StyledListContent,
Expand All @@ -31,10 +31,17 @@ class AdsBox extends React.Component<Props, {}> {

onAdsSettingChange = (key: string, value: boolean) => {
let newValue: any = value
const { adsEnabled } = this.props.rewardsData.adsData
const {
adsEnabled,
shouldAllowSubdivisionAdTargeting
} = this.props.rewardsData.adsData

if (key === 'adsEnabled') {
newValue = !adsEnabled
} else if (key === 'shouldAllowSubdivisionAdTargeting') {
newValue = !shouldAllowSubdivisionAdTargeting
} else if (key == 'adsSubdivision') {
this.props.actions.onAdsSettingSave('didOverrideAdsSubdivision', !didOverrideAdsSubdivision);
}

this.props.actions.onAdsSettingSave(key, newValue)
Expand All @@ -45,10 +52,15 @@ class AdsBox extends React.Component<Props, {}> {
return null
}

const { adsPerHour } = this.props.rewardsData.adsData
const {
adsPerHour,
shouldAllowSubdivisionAdTargeting,
adsSubdivision,
isSubdivisionAdTargetingRegion
} = this.props.rewardsData.adsData

return (
<Grid columns={1} customStyle={{ maxWidth: '270px', margin: '0 auto' }}>
<Grid columns={1}>
<Column size={1} customStyle={{ justifyContent: 'center', flexWrap: 'wrap' }}>
<ControlWrapper text={getLocale('adsPerHour')}>
<Select
Expand All @@ -58,13 +70,44 @@ class AdsBox extends React.Component<Props, {}> {
{['1', '2', '3', '4', '5'].map((num: string) => {
return (
<div key={`num-per-hour-${num}`} data-value={num}>
{getLocale(`adsPerHour${num}`)}
{getLocale(`adsPerHour${num}`)}
</div>
)
})}
</Select>
</ControlWrapper>
</Column>

<div style={{display: isSubdivisionAdTargetingRegion ? 'block' : 'none' }}>
<Column size={1} customStyle={{ justifyContent: 'center', flexWrap: 'wrap' }}>
<ControlWrapper text={getLocale('adsAllowSubdivisionTargetingTitle')}>
<Checkbox
value={{
shouldAllowSubdivisionAdTargeting: shouldAllowSubdivisionAdTargeting
}}
multiple={true}
onChange={this.onAdsSettingChange}
>
<div data-key='shouldAllowSubdivisionAdTargeting'>{getLocale('adsAllowSubdivisionTargetingLabel')}</div>
</Checkbox>

<Select
value={(adsSubdivision || '').toString()}
onChange={this.onAdsSettingChange.bind(this, 'adsSubdivision')}
>
{[['US-AL', 'Alabama'], ['US-AK', 'Alaska'], ['US-AZ', 'Arizona'], ['US-AR', 'Arkansas'], ['US-CA', 'California'], ['US-CO', 'Colorado'], ['US-CT', 'Connecticut'], ['US-DE', 'Delaware'], ['US-FL', 'Florida'], ['US-GA', 'Georgia'], ['US-HI', 'Hawaii'], ['US-ID', 'Idaho'], ['US-IL', 'Illinois'], ['US-IN', 'Indiana'], ['US-IA', 'Iowa'], ['US-KS', 'Kansas'], ['US-KY', 'Kentucky'], ['US-LA', 'Louisiana'], ['US-ME', 'Maine'], ['US-MD', 'Maryland'], ['US-MA', 'Massachusetts'], ['US-MI', 'Michigan'], ['US-MN', 'Minnesota'], ['US-MS', 'Mississippi'], ['US-MO', 'Missouri'], ['US-MT', 'Montana'], ['US-NE', 'Nebraska'], ['US-NV', 'Nevada'], ['US-NH', 'New Hampshire'], ['US-NJ', 'New Jersey'], ['US-NM', 'New Mexico'], ['US-NY', 'New York'], ['US-NC', 'North Carolina'], ['US-ND', 'North Dakota'], ['US-OH', 'Ohio'], ['US-OK', 'Oklahoma'], ['US-OR', 'Oregon'], ['US-PA', 'Pennsylvania'], ['US-RI', 'Rhode Island'], ['US-SC', 'South Carolina'], ['US-SD', 'South Dakota'], ['US-TN', 'Tennessee'], ['US-TX', 'Texas'], ['US-UT', 'Utah'], ['US-VT', 'Vermont'], ['US-VA', 'Virginia'], ['US-WA', 'Washington'], ['US-WV', 'West Virginia'], ['US-WI', 'Wisconsin'], ['US-WY', 'Wyoming']].map((subdivision: Array<String>) => {
return (
<div key={`${subdivision[0]}`} data-value={subdivision[0]}>
{`${subdivision[1]}`}
</div>
)
})}
</Select>
</ControlWrapper>
</Column>

<div>{getLocale('adsAllowSubdivisionTargetingDescription')} <a href={'#'} target={'_blank'}>{getLocale('adsAllowSubdivisionTargetingLearn')}</a></div>
</div>
</Grid>
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State

state.adsData.adsEnabled = action.payload.adsData.adsEnabled
state.adsData.adsPerHour = action.payload.adsData.adsPerHour
state.adsData.adsSubdivision = action.payload.adsData.adsSubdivision
state.adsData.shouldAllowSubdivisionAdTargeting = action.payload.adsData.shouldAllowSubdivisionAdTargeting
state.adsData.didOverrideAdsSubdivision = action.payload.adsData.didOverrideAdsSubdivision
state.adsData.isSubdivisionAdTargetingRegion = action.payload.adsData.isSubdivisionAdTargetingRegion
state.adsData.adsUIEnabled = action.payload.adsData.adsUIEnabled
state.adsData.adsIsSupported = action.payload.adsData.adsIsSupported
break
Expand Down
4 changes: 4 additions & 0 deletions components/brave_rewards/resources/android_page/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export const defaultState: Rewards.State = {
adsData: {
adsEnabled: false,
adsPerHour: 0,
adsSubdivision: '',
shouldAllowSubdivisionAdTargeting: true,
isSubdivisionAdTargetingRegion: true,
didOverrideAdsSubdivision: false,
adsUIEnabled: false,
adsIsSupported: false,
adsEstimatedPendingRewards: 0,
Expand Down
Loading

0 comments on commit a3ec561

Please sign in to comment.