Skip to content

Commit

Permalink
Merge pull request #7218 from /issues/12821
Browse files Browse the repository at this point in the history
Refactored new tab page ads and ad notifications
  • Loading branch information
tmancey authored Nov 21, 2020
2 parents 51d95d1 + be0e073 commit 6f13d50
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
#include "bat/ads/confirmation_type.h"
#include "bat/ads/internal/ad_events/ad_events.h"
#include "bat/ads/internal/ads_history/ads_history.h"
#include "bat/ads/internal/database/tables/ad_events_database_table.h"
#include "bat/ads/internal/frequency_capping/new_tab_page_ads/new_tab_page_ads_frequency_capping.h"
#include "bat/ads/internal/logging.h"
#include "bat/ads/new_tab_page_ad_info.h"

Expand All @@ -24,44 +22,6 @@ AdEventViewed::~AdEventViewed() = default;

void AdEventViewed::FireEvent(
const NewTabPageAdInfo& ad) {
database::table::AdEvents database_table;
database_table.GetAll([=](
const Result result,
const AdEventList& ad_events) {
if (result != Result::SUCCESS) {
BLOG(1, "New tab page ad: Failed to get ad events");
return;
}

if (!ShouldConfirmAd(ad, ad_events)) {
BLOG(1, "New tab page ad: Not allowed");
return;
}

ConfirmAd(ad);
});
}

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

bool AdEventViewed::ShouldConfirmAd(
const NewTabPageAdInfo& ad,
const AdEventList& ad_events) {
FrequencyCapping frequency_capping(ad_events);

if (!frequency_capping.IsAdAllowed()) {
return false;
}

if (frequency_capping.ShouldExcludeAd(ad)) {
return false;
}

return true;
}

void AdEventViewed::ConfirmAd(
const NewTabPageAdInfo& ad) {
BLOG(3, "Viewed new tab page ad with uuid " << ad.uuid
<< " and creative instance id " << ad.creative_instance_id);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#define BAT_ADS_INTERNAL_AD_EVENTS_NEW_TAB_PAGE_ADS_NEW_TAB_PAGE_AD_EVENT_VIEWED_H_ // NOLINT

#include "bat/ads/internal/ad_events/ad_event.h"
#include "bat/ads/internal/ad_events/ad_event_info.h"

namespace ads {

Expand All @@ -23,14 +22,6 @@ class AdEventViewed : public AdEvent<NewTabPageAdInfo> {

void FireEvent(
const NewTabPageAdInfo& ad) override;

private:
bool ShouldConfirmAd(
const NewTabPageAdInfo& ad,
const AdEventList& ad_events);

void ConfirmAd(
const NewTabPageAdInfo& ad);
};

} // namespace new_tab_page_ads
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void AdNotification::FireEvent(

AdNotificationInfo ad;
if (!AdNotifications::Get()->Get(uuid, &ad)) {
BLOG(1, "Failed to trigger ad notification event for uuid " << uuid);
BLOG(1, "Failed to fire ad notification event for uuid " << uuid);

NotifyAdNotificationEventFailed(uuid, event_type);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

#include "bat/ads/internal/ad_events/new_tab_page_ads/new_tab_page_ad_event_factory.h"
#include "bat/ads/internal/bundle/creative_new_tab_page_ad_info.h"
#include "bat/ads/internal/database/tables/ad_events_database_table.h"
#include "bat/ads/internal/database/tables/creative_new_tab_page_ads_database_table.h"
#include "bat/ads/internal/frequency_capping/new_tab_page_ads/new_tab_page_ads_frequency_capping.h"
#include "bat/ads/internal/logging.h"
#include "bat/ads/new_tab_page_ad_info.h"

Expand Down Expand Up @@ -54,10 +56,13 @@ void NewTabPageAd::FireEvent(
const std::string& creative_instance_id,
const NewTabPageAdEventType event_type) {
if (wallpaper_id.empty() || creative_instance_id.empty()) {
BLOG(1, "Failed to trigger new tab page ad event for wallpaper id "
BLOG(1, "Failed to fire new tab page ad event for wallpaper id "
<< wallpaper_id << " and creative instance id "
<< creative_instance_id);

NotifyNewTabPageAdEventFailed(wallpaper_id,
creative_instance_id, event_type);

return;
}

Expand All @@ -67,7 +72,7 @@ void NewTabPageAd::FireEvent(
const std::string& creative_instance_id,
const CreativeNewTabPageAdInfo& creative_new_tab_page_ad) {
if (result != SUCCESS) {
BLOG(1, "Failed to trigger new tab page ad event for wallpaper id");
BLOG(1, "Failed to fire new tab page ad event for wallpaper id");

NotifyNewTabPageAdEventFailed(wallpaper_id,
creative_instance_id, event_type);
Expand All @@ -78,15 +83,63 @@ void NewTabPageAd::FireEvent(
const NewTabPageAdInfo ad =
CreateNewTabPageAd(wallpaper_id, creative_new_tab_page_ad);

FireEvent(ad, wallpaper_id, creative_instance_id, event_type);
});
}

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

bool NewTabPageAd::ShouldFireEvent(
const NewTabPageAdInfo& ad,
const AdEventList& ad_events) {
new_tab_page_ads::FrequencyCapping frequency_capping(ad_events);

if (!frequency_capping.IsAdAllowed()) {
return false;
}

if (frequency_capping.ShouldExcludeAd(ad)) {
return false;
}

return true;
}

void NewTabPageAd::FireEvent(
const NewTabPageAdInfo& ad,
const std::string& wallpaper_id,
const std::string& creative_instance_id,
const NewTabPageAdEventType event_type) {
database::table::AdEvents database_table;
database_table.GetAll([=](
const Result result,
const AdEventList& ad_events) {
if (result != Result::SUCCESS) {
BLOG(1, "New tab page ad: Failed to get ad events");

NotifyNewTabPageAdEventFailed(wallpaper_id,
creative_instance_id, event_type);

return;
}

if (event_type == NewTabPageAdEventType::kViewed &&
!ShouldFireEvent(ad, ad_events)) {
BLOG(1, "New tab page ad: Not allowed");

NotifyNewTabPageAdEventFailed(wallpaper_id,
creative_instance_id, event_type);

return;
}

const auto ad_event = new_tab_page_ads::AdEventFactory::Build(event_type);
ad_event->FireEvent(ad);

NotifyNewTabPageAdEvent(ad, event_type);
});
}

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

void NewTabPageAd::NotifyNewTabPageAdEvent(
const NewTabPageAdInfo& ad,
const NewTabPageAdEventType event_type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include <string>

#include "bat/ads/internal/ad_events/ad_event_info.h"
#include "bat/ads/internal/ads/new_tab_page_ads/new_tab_page_ad_observer.h"
#include "bat/ads/mojom.h"

Expand Down Expand Up @@ -35,6 +36,16 @@ class NewTabPageAd
private:
base::ObserverList<NewTabPageAdObserver> observers_;

bool ShouldFireEvent(
const NewTabPageAdInfo& ad,
const AdEventList& ad_events);

void FireEvent(
const NewTabPageAdInfo& ad,
const std::string& wallpaper_id,
const std::string& creative_instance_id,
const NewTabPageAdEventType event_type);

void NotifyNewTabPageAdEvent(
const NewTabPageAdInfo& ad,
const NewTabPageAdEventType event_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ void Confirmations::OnDidRedeemUnblindedToken(
{unblinded_payment_token});
ConfirmationsState::Get()->Save();

const CatalogIssuersInfo catalog_issuers;
const CatalogIssuersInfo catalog_issuers =
ConfirmationsState::Get()->get_catalog_issuers();

const base::Optional<double> estimated_redemption_value =
catalog_issuers.GetEstimatedRedemptionValue(
Expand Down

0 comments on commit 6f13d50

Please sign in to comment.