From c146c2613c3d3b2c5f52ad55ff6629e78218ac2d Mon Sep 17 00:00:00 2001 From: Kyle Hickinson Date: Wed, 17 Feb 2021 15:14:14 -0500 Subject: [PATCH] Fix #3235: Implement NTP-SI confirmations --- .../Frontend/Browser/HomePanel/NTPModels.swift | 6 +++++- .../Backgrounds/NTPDownloader.swift | 3 ++- .../Backgrounds/NewTabPageBackground.swift | 3 +++ .../New Tab Page/NewTabPageViewController.swift | 17 +++++++++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Client/Frontend/Browser/HomePanel/NTPModels.swift b/Client/Frontend/Browser/HomePanel/NTPModels.swift index 70a9a1063e5..18d9a24841f 100644 --- a/Client/Frontend/Browser/HomePanel/NTPModels.swift +++ b/Client/Frontend/Browser/HomePanel/NTPModels.swift @@ -29,15 +29,19 @@ class NTPBackground: Codable { /// Required instead of `CGPoint` due to x/y being optionals let focalPoint: FocalPoint? + // Only available for sponsored images, not normal wallpapers + let creativeInstanceId: String? + /// Only available for normal wallpapers, not for sponsored images let credit: Credit? /// Whether the background is a packaged resource or a remote one, impacts how it should be loaded let packaged: Bool? - init(imageUrl: String, focalPoint: FocalPoint?) { + init(imageUrl: String, focalPoint: FocalPoint?, creativeInstanceId: String?) { self.imageUrl = imageUrl self.focalPoint = focalPoint + self.creativeInstanceId = creativeInstanceId self.credit = nil self.packaged = nil } diff --git a/Client/Frontend/Browser/New Tab Page/Backgrounds/NTPDownloader.swift b/Client/Frontend/Browser/New Tab Page/Backgrounds/NTPDownloader.swift index 590c3e727b0..655cf49c588 100644 --- a/Client/Frontend/Browser/New Tab Page/Backgrounds/NTPDownloader.swift +++ b/Client/Frontend/Browser/New Tab Page/Backgrounds/NTPDownloader.swift @@ -319,7 +319,8 @@ class NTPDownloader { basePath: URL) -> [NTPBackground] { backgrounds.map { NTPBackground(imageUrl: basePath.appendingPathComponent($0.imageUrl).path, - focalPoint: $0.focalPoint) + focalPoint: $0.focalPoint, + creativeInstanceId: $0.creativeInstanceId) } } diff --git a/Client/Frontend/Browser/New Tab Page/Backgrounds/NewTabPageBackground.swift b/Client/Frontend/Browser/New Tab Page/Backgrounds/NewTabPageBackground.swift index 7d7661d2c99..86c9b2ef213 100644 --- a/Client/Frontend/Browser/New Tab Page/Backgrounds/NewTabPageBackground.swift +++ b/Client/Frontend/Browser/New Tab Page/Backgrounds/NewTabPageBackground.swift @@ -19,9 +19,12 @@ class NewTabPageBackground: PreferencesObserver { /// The current background image & possibly sponsor private(set) var currentBackground: (wallpaper: NTPBackground, type: NTPDataSource.BackgroundType)? { didSet { + wallpaperId = UUID() changed?() } } + /// A unique wallpaper identifier + private(set) var wallpaperId: UUID? /// The background/wallpaper image if available var backgroundImage: UIImage? { currentBackground?.wallpaper.image diff --git a/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift b/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift index 788f41f66f4..aab632f94c1 100644 --- a/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift +++ b/Client/Frontend/Browser/New Tab Page/NewTabPageViewController.swift @@ -254,6 +254,7 @@ class NewTabPageViewController: UIViewController, Themeable { override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) + reportSponsoredImageBackgroundEvent(.viewed) presentNotification() } @@ -392,6 +393,20 @@ class NewTabPageViewController: UIViewController, Themeable { backgroundView.imageConstraints?.landscapeCenter.update(offset: inset) } + private func reportSponsoredImageBackgroundEvent(_ event: NewTabPageAdEventType) { + guard let backgroundType = background.currentBackground?.type, + case .withBrandLogo = backgroundType, + let wallpaperId = background.wallpaperId?.uuidString, + let creativeInstanceId = background.currentBackground?.wallpaper.creativeInstanceId else { + return + } + rewards.ads.reportNewTabPageAdEvent( + wallpaperId, + creativeInstanceId: creativeInstanceId, + eventType: event + ) + } + // MARK: - Notifications private var notificationController: UIViewController? @@ -665,6 +680,8 @@ class NewTabPageViewController: UIViewController, Themeable { private func tappedSponsorButton(_ logo: NTPLogo) { UIImpactFeedbackGenerator(style: .medium).bzzt() delegate?.navigateToInput(logo.destinationUrl, inNewTab: false, switchingToPrivateMode: false) + + reportSponsoredImageBackgroundEvent(.clicked) } private func tappedQRCode(_ code: String) {