Skip to content

Commit

Permalink
Implement Brave Ads Serving Improvements
Browse files Browse the repository at this point in the history
Currently, ad serving is done by randomly picking ads based on matching segments. The approach has several problems like under-delivery of creatives and users not seeing as many ads as they could. This PR aims to solve these short comings by introducing a statistical model for ad selection. We assume that the click probability can be approximated by a linear function of features related to locally observed user behaviour. The function is weighted by a control-parameter called pass-through rate (ptr) to throttle delivery of individual ads.
  • Loading branch information
Moritz Haller committed Sep 21, 2021
1 parent 3cc9930 commit 0f2aac6
Show file tree
Hide file tree
Showing 90 changed files with 2,770 additions and 362 deletions.
18 changes: 18 additions & 0 deletions components/brave_ads/test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/account/ad_rewards/payments/payments_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/account/statement/statement_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_diagnostics/ad_diagnostics_test.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_event_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_event_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_events/ad_event_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_pacing/ad_pacing_test.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_priority/ad_priority_test.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ad_serving/ad_notifications/ad_notification_serving_test.cc",
Expand All @@ -46,6 +49,10 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/ads_history/sorts/ads_history_sort_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/base64_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/browser_manager/browser_manager_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/bundle/creative_ad_notification_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/bundle/creative_ad_notification_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/bundle/creative_ad_unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/bundle/creative_ad_unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/catalog/catalog_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/catalog/catalog_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/container_util_unittest.cc",
Expand Down Expand Up @@ -74,7 +81,12 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/database/tables/segments_database_table_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/ad_notifications/eligible_ad_notifications_issue_17199_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/ad_notifications/eligible_ad_notifications_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/eligible_ads_features_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/eligible_ads_features_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/eligible_ads_predictor_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/eligible_ads_util_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/inline_content_ads/eligible_inline_content_ads_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/eligible_ads/sample_ads_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/features/ad_rewards/ad_rewards_features_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/features/ad_serving/ad_serving_features_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/features/anti_targeting/anti_targeting_features_unittest.cc",
Expand Down Expand Up @@ -189,6 +201,12 @@ source_set("brave_ads_unit_tests") {
"//brave/vendor/bat-native-ads/src/bat/ads/internal/tokens/refill_unblinded_tokens/request_signed_tokens_url_request_builder_unittest.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_base.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_base.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_file_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_file_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_tag_parser_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_tag_parser_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_time_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_time_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_util.cc",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/unittest_util.h",
"//brave/vendor/bat-native-ads/src/bat/ads/internal/url_util_unittest.cc",
Expand Down
11 changes: 11 additions & 0 deletions vendor/bat-native-ads/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ source_set("ads") {
"src/bat/ads/internal/bundle/creative_ad_notification_info.cc",
"src/bat/ads/internal/bundle/creative_ad_notification_info.h",
"src/bat/ads/internal/bundle/creative_ad_notification_info_aliases.h",
"src/bat/ads/internal/bundle/creative_daypart_info.cc",
"src/bat/ads/internal/bundle/creative_daypart_info.h",
"src/bat/ads/internal/bundle/creative_daypart_info_aliases.h",
"src/bat/ads/internal/bundle/creative_inline_content_ad_info.cc",
Expand Down Expand Up @@ -491,12 +492,22 @@ source_set("ads") {
"src/bat/ads/internal/eligible_ads/ad_notifications/eligible_ad_notifications.cc",
"src/bat/ads/internal/eligible_ads/ad_notifications/eligible_ad_notifications.h",
"src/bat/ads/internal/eligible_ads/ad_notifications/eligible_ad_notifications_aliases.h",
"src/bat/ads/internal/eligible_ads/ad_predictor_info.cc",
"src/bat/ads/internal/eligible_ads/ad_predictor_info.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_aliases.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_constants.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_features.cc",
"src/bat/ads/internal/eligible_ads/eligible_ads_features.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_features_util.cc",
"src/bat/ads/internal/eligible_ads/eligible_ads_features_util.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_predictor_util.h",
"src/bat/ads/internal/eligible_ads/eligible_ads_util.h",
"src/bat/ads/internal/eligible_ads/inline_content_ads/eligible_inline_content_ads.cc",
"src/bat/ads/internal/eligible_ads/inline_content_ads/eligible_inline_content_ads.h",
"src/bat/ads/internal/eligible_ads/inline_content_ads/eligible_inline_content_ads_aliases.h",
"src/bat/ads/internal/eligible_ads/round_robin_ads.h",
"src/bat/ads/internal/eligible_ads/round_robin_advertisers.h",
"src/bat/ads/internal/eligible_ads/sample_ads.h",
"src/bat/ads/internal/eligible_ads/seen_ads.h",
"src/bat/ads/internal/eligible_ads/seen_advertisers.h",
"src/bat/ads/internal/features/ad_rewards/ad_rewards_features.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bat/ads/internal/account/ad_rewards/ad_rewards.h"

#include "bat/ads/internal/unittest_base.h"
#include "bat/ads/internal/unittest_time_util.h"
#include "bat/ads/internal/unittest_util.h"
#include "bat/ads/statement_info.h"
#include "net/http/http_status_code.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "bat/ads/internal/account/ad_rewards/ad_rewards.h"

#include "bat/ads/internal/unittest_base.h"
#include "bat/ads/internal/unittest_time_util.h"
#include "bat/ads/internal/unittest_util.h"
#include "bat/ads/statement_info.h"
#include "net/http/http_status_code.h"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <memory>

#include "bat/ads/internal/unittest_base.h"
#include "bat/ads/internal/unittest_time_util.h"
#include "bat/ads/internal/unittest_util.h"

// npm run test -- brave_unit_tests --filter=BatAds*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "bat/ads/internal/account/wallet/wallet.h"
#include "bat/ads/internal/account/wallet/wallet_info.h"
#include "bat/ads/internal/unittest_base.h"
#include "bat/ads/internal/unittest_time_util.h"
#include "bat/ads/internal/unittest_util.h"
#include "bat/ads/statement_info.h"
#include "net/http/http_status_code.h"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#include "bat/ads/internal/ad_events/ad_event_unittest_util.h"

#include <cstdint>

#include "base/guid.h"
#include "bat/ads/confirmation_type.h"
#include "bat/ads/internal/ad_events/ad_event_info.h"
#include "bat/ads/internal/bundle/creative_ad_info.h"

namespace ads {

AdEventInfo GetAdEvent(const CreativeAdInfo& creative_ad,
const ConfirmationType confirmation_type,
const base::Time& time) {
AdEventInfo ad_event;
ad_event.uuid = base::GenerateGUID();
ad_event.confirmation_type = confirmation_type;
ad_event.creative_instance_id = creative_ad.creative_instance_id;
ad_event.advertiser_id = creative_ad.advertiser_id;
ad_event.timestamp = static_cast<int64_t>(time.ToDoubleT());

return ad_event;
}

} // namespace ads
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright (c) 2021 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 http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UNITTEST_UTIL_H_
#define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UNITTEST_UTIL_H_

#include "base/time/time.h"

namespace ads {

struct AdEventInfo;
class ConfirmationType;
struct CreativeAdInfo;

AdEventInfo GetAdEvent(const CreativeAdInfo& creative_ad,
const ConfirmationType confirmation_type,
const base::Time& time);

} // namespace ads

#endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UNITTEST_UTIL_H_
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

#include "bat/ads/internal/ad_events/ad_event_util.h"

#include "base/time/time.h"
#include "bat/ads/ad_info.h"
#include "bat/ads/internal/bundle/creative_ad_info.h"

namespace ads {

Expand All @@ -23,4 +25,39 @@ bool HasFiredAdViewedEvent(const AdInfo& ad, const AdEventList& ad_events) {
return true;
}

absl::optional<base::Time> GetLastSeenAdTime(
const AdEventList& ad_events,
const CreativeAdInfo& creative_ad) {
const auto iter = std::find_if(
ad_events.begin(), ad_events.end(),
[&creative_ad](const AdEventInfo& ad_event) -> bool {
return (ad_event.creative_instance_id ==
creative_ad.creative_instance_id &&
ad_event.confirmation_type == ConfirmationType::kViewed);
});

if (iter == ad_events.end()) {
return absl::nullopt;
}

return base::Time::FromDoubleT(static_cast<double>(iter->timestamp));
}

absl::optional<base::Time> GetLastSeenAdvertiserTime(
const AdEventList& ad_events,
const CreativeAdInfo& creative_ad) {
const auto iter = std::find_if(
ad_events.begin(), ad_events.end(),
[&creative_ad](const AdEventInfo& ad_event) -> bool {
return (ad_event.advertiser_id == creative_ad.advertiser_id &&
ad_event.confirmation_type == ConfirmationType::kViewed);
});

if (iter == ad_events.end()) {
return absl::nullopt;
}

return base::Time::FromDoubleT(static_cast<double>(iter->timestamp));
}

} // namespace ads
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,28 @@
#ifndef BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UTIL_H_
#define BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UTIL_H_

#include "bat/ads/internal/ad_events/ad_event_info.h"
#include "bat/ads/internal/ad_events/ad_event_info_aliases.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace base {
class Time;
}

namespace ads {

struct AdInfo;
struct CreativeAdInfo;

bool HasFiredAdViewedEvent(const AdInfo& ad, const AdEventList& ad_events);

absl::optional<base::Time> GetLastSeenAdTime(const AdEventList& ad_events,
const CreativeAdInfo& creative_ad);

absl::optional<base::Time> GetLastSeenAdvertiserTime(
const AdEventList& ad_events,
const CreativeAdInfo& creative_ad);

} // namespace ads

#endif // BRAVE_VENDOR_BAT_NATIVE_ADS_SRC_BAT_ADS_INTERNAL_AD_EVENTS_AD_EVENT_UTIL_H_
Loading

0 comments on commit 0f2aac6

Please sign in to comment.