Skip to content

Commit

Permalink
Fix brave/brave-ios#7876: Replace Rewards ads usage P3A question (bra…
Browse files Browse the repository at this point in the history
…ve/brave-ios#7935)

This removes the `Brave.Rewards.AdsEnabledDuration` question and answers the new `Brave.Rewards.AdTypesEnabled` question
  • Loading branch information
kylehickinson authored Aug 23, 2023
1 parent 634a4b8 commit 8ab14c6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 43 deletions.
5 changes: 5 additions & 0 deletions Sources/Brave/Frontend/Browser/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ public class BrowserViewController: UIViewController {
Preferences.Playlist.enablePlaylistMenuBadge.observe(from: self)
Preferences.Playlist.enablePlaylistURLBarButton.observe(from: self)
Preferences.Playlist.syncSharedFoldersAutomatically.observe(from: self)
Preferences.NewTabPage.backgroundSponsoredImages.observe(from: self)
ShieldPreferences.blockAdsAndTrackingLevelRaw.observe(from: self)

pageZoomListener = NotificationCenter.default.addObserver(forName: PageZoomView.notificationName, object: nil, queue: .main) { [weak self] _ in
Expand All @@ -464,6 +465,7 @@ public class BrowserViewController: UIViewController {
guard let self = self else { return }
self.updateRewardsButtonState()
self.setupAdsNotificationHandler()
self.recordAdsUsageType()
}
Preferences.Playlist.webMediaSourceCompatibility.observe(from: self)
Preferences.PrivacyReports.captureShieldsData.observe(from: self)
Expand Down Expand Up @@ -551,6 +553,7 @@ public class BrowserViewController: UIViewController {
recordAccessibilityDocumentsDirectorySizeP3A()
recordTimeBasedNumberReaderModeUsedP3A(activated: false)
PlaylistP3A.recordHistogram()
recordAdsUsageType()

// Revised Review Handling
AppReviewManager.shared.handleAppReview(for: .revisedCrossPlatform, using: self)
Expand Down Expand Up @@ -3145,6 +3148,8 @@ extension BrowserViewController: PreferencesObserver {
updateURLBarWalletButton()
case Preferences.Playlist.syncSharedFoldersAutomatically.key:
syncPlaylistFolders()
case Preferences.NewTabPage.backgroundSponsoredImages.key:
recordAdsUsageType()
default:
Logger.module.debug("Received a preference change for an unknown key: \(key, privacy: .public) on \(type(of: self), privacy: .public)")
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,24 @@ extension BrowserViewController {
value: storage.combinedValue
)
}

func recordAdsUsageType() {
enum Answer: Int, CaseIterable {
case none = 0
case ntpOnly = 1
case pushOnly = 2
case ntpAndPush = 3
}
var answer: Answer = .none
if rewards.ads.isEnabled && Preferences.NewTabPage.backgroundSponsoredImages.value {
answer = .ntpAndPush
} else if rewards.ads.isEnabled {
answer = .pushOnly
} else if Preferences.NewTabPage.backgroundSponsoredImages.value {
answer = .ntpOnly
}
UmaHistogramEnumeration("Brave.Rewards.AdTypesEnabled", sample: answer)
}
}

extension P3AFeatureUsage {
Expand Down
43 changes: 0 additions & 43 deletions Sources/Brave/Frontend/Rewards/BraveRewards.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public class BraveRewards: NSObject {
if Preferences.Rewards.adsEnabledTimestamp.value == nil, ads.isEnabled {
Preferences.Rewards.adsEnabledTimestamp.value = Date()
}
reportLastAdsUsageP3A()
}

func startRewardsService(_ completion: (() -> Void)?) {
Expand Down Expand Up @@ -130,7 +129,6 @@ public class BraveRewards: NSObject {
} else if wasEnabled && !newValue {
Preferences.Rewards.adsDisabledTimestamp.value = Date()
}
self.reportLastAdsUsageP3A()
if !newValue {
self.ads.isEnabled = newValue
self.proposeAdsShutdown()
Expand Down Expand Up @@ -255,47 +253,6 @@ public class BraveRewards: NSObject {
private func tabRetrieved(_ tabId: Int, url: URL, html: String?) {
rewardsAPI?.fetchPublisherActivity(from: url, faviconURL: nil, publisherBlob: html, tabId: UInt64(tabId))
}

// MARK: - P3A

func reportLastAdsUsageP3A() {
enum Answer: Int, CaseIterable {
case neverOn = 0
case stillOn = 1
case lessThanThreeHours = 2
case lessThanThreeDays = 3
case lessThanThreeWeeks = 4
case lessThanThreeMonths = 5
case moreThanThreeMonths = 6
}
var answer: Answer = .neverOn
if ads.isEnabled {
answer = .stillOn
} else if let start = Preferences.Rewards.adsEnabledTimestamp.value,
let end = Preferences.Rewards.adsDisabledTimestamp.value, end > start {
let components = Calendar.current.dateComponents(
[.hour, .day, .weekOfYear, .month],
from: start,
to: end
)
let (month, week, day, hour) = (components.month ?? 0,
components.weekOfYear ?? 0,
components.day ?? 0,
components.hour ?? 0)
if month >= 3 {
answer = .moreThanThreeMonths
} else if week >= 3 {
answer = .lessThanThreeMonths
} else if day >= 3 {
answer = .lessThanThreeWeeks
} else if hour >= 3 {
answer = .lessThanThreeDays
} else {
answer = .lessThanThreeHours
}
}
UmaHistogramEnumeration("Brave.Rewards.AdsEnabledDuration", sample: answer)
}
}

extension BraveRewards {
Expand Down

0 comments on commit 8ab14c6

Please sign in to comment.