-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for Native Impression Tracking (#1060)
* feat: impression tracking for banner * feat: observe interstitial apperance * feat: some improvements * feat: add interstitial impression tracking * refactor: clean up * fix: fix calling timer on background thread * refactor: simplify AdViewUtils class * feat: implement cache id search * feat: make AdViewUtils class more generic * feat: add additional check of hb_cache_id tp ensure that we found Prebid creative * feat: update tests * feat: minor changes * feat: add ability to pass ad view in PrebidAdUnit * feat: update tests * feat: move fetchDemand with adView to BannerAdUnit * feat: add tests for BannerViewReloadTracker * feat: update tests * refactor: minor corrections * feat: update scripts * feat: disable parallel test execution & run trackers only for banner ads * feat: update AdViewUtilsTests * feat: update timeout * feat: minor changes * feat: change pollingInterval * feat: add activatePrebidImpressionTracker flag for interstitials * feat: introduce activatePrebidImpressionTracker method * feat: move impression tracking logic to PrebidImpressionTracker * refactor: transform activatePrebidImpressionTracker to method for consistency * fix: small update for InternalTestApp * test: increase intervals for some tests * feat: update Podfile.lock * feat: resolve duplicats
- Loading branch information
1 parent
96c8a5a
commit 9947afd
Showing
30 changed files
with
1,206 additions
and
328 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
PrebidMobile/AdUnits/ImpressionTracking/BannerViewImpressionTracker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* Copyright 2018-2024 Prebid.org, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import UIKit | ||
|
||
/// Schedules the reload tracker to monitor the GAM banner view. | ||
/// Each time the banner view reloads, the viewability tracker is triggered. | ||
class BannerViewImpressionTracker: PrebidImpressionTrackerProtocol { | ||
|
||
private weak var monitoredView: UIView? | ||
|
||
private var reloadTracker: BannerViewReloadTracker? | ||
private var viewabilityTracker: PBMCreativeViewabilityTracker? | ||
|
||
private var payload: PrebidImpressionTrackerPayload? | ||
|
||
private var isImpressionTracked = false | ||
|
||
private var pollingInterval: TimeInterval { | ||
0.2 | ||
} | ||
|
||
init() { | ||
reloadTracker = BannerViewReloadTracker( | ||
reloadCheckInterval: pollingInterval | ||
) { [weak self] in | ||
self?.isImpressionTracked = false | ||
self?.attachViewabilityTracker() | ||
} | ||
} | ||
|
||
func start(in view: UIView) { | ||
self.monitoredView = view | ||
reloadTracker?.start(in: view) | ||
} | ||
|
||
func stop() { | ||
reloadTracker?.stop() | ||
viewabilityTracker?.stop() | ||
} | ||
|
||
private func attachViewabilityTracker() { | ||
guard let monitoredView else { return } | ||
|
||
viewabilityTracker = PBMCreativeViewabilityTracker( | ||
view: monitoredView, | ||
pollingTimeInterval: pollingInterval, | ||
onExposureChange: { [weak self, weak monitoredView] _, viewExposure in | ||
guard let self = self, let monitoredView else { return } | ||
|
||
if viewExposure.exposureFactor > 0 && !self.isImpressionTracked { | ||
self.viewabilityTracker?.stop() | ||
self.isImpressionTracked = true | ||
|
||
// Ensure that we found Prebid creative | ||
AdViewUtils.findPrebidCacheID(monitoredView) { [weak self] result in | ||
guard let self = self else { return } | ||
|
||
switch result { | ||
case .success(let foundCacheID): | ||
if let trackingURLs = self.payload?.trackingURLs, | ||
let creativeCacheID = self.payload?.cacheID, | ||
foundCacheID == creativeCacheID { | ||
for trackingURL in trackingURLs { | ||
PrebidServerConnection.shared.fireAndForget(trackingURL) | ||
} | ||
} | ||
case .failure(let error): | ||
Log.warn(error.localizedDescription) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
|
||
viewabilityTracker?.start() | ||
} | ||
|
||
func register(payload: PrebidImpressionTrackerPayload) { | ||
self.payload = payload | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
PrebidMobile/AdUnits/ImpressionTracking/InterstitialImpressionTracker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* Copyright 2018-2024 Prebid.org, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import WebKit | ||
|
||
/// Schedules an observer for interstitial views and, upon detection, activates the viewability tracker. | ||
class InterstitialImpressionTracker: PrebidImpressionTrackerProtocol { | ||
|
||
private var interstitialObserver: InterstitialObserver? | ||
private var viewabilityTracker: PBMCreativeViewabilityTracker? | ||
|
||
private var payload: PrebidImpressionTrackerPayload? | ||
|
||
private var pollingInterval: TimeInterval { | ||
0.2 | ||
} | ||
|
||
func start(in view: UIView) { | ||
interstitialObserver = InterstitialObserver(window: view as? UIWindow) { [weak self] view in | ||
self?.attachViewabilityTracker(to: view) | ||
} | ||
|
||
interstitialObserver?.start() | ||
} | ||
|
||
func stop() { | ||
interstitialObserver?.stop() | ||
viewabilityTracker?.stop() | ||
} | ||
|
||
private func attachViewabilityTracker(to view: UIView) { | ||
viewabilityTracker = PBMCreativeViewabilityTracker( | ||
view: view, | ||
pollingTimeInterval: pollingInterval, | ||
onExposureChange: { [weak self, weak view] _, viewExposure in | ||
guard let self, let view else { return } | ||
|
||
if viewExposure.exposureFactor > 0 { | ||
self.stop() | ||
|
||
// Ensure that we found Prebid creative | ||
AdViewUtils.findPrebidCacheID(view) { result in | ||
switch result { | ||
case .success(let foundCacheID): | ||
if let trackingURLs = self.payload?.trackingURLs, | ||
let creativeCacheID = self.payload?.cacheID, | ||
foundCacheID == creativeCacheID { | ||
for trackingURL in trackingURLs { | ||
PrebidServerConnection.shared.fireAndForget(trackingURL) | ||
} | ||
} | ||
case .failure(let error): | ||
Log.warn(error.localizedDescription) | ||
} | ||
} | ||
} | ||
} | ||
) | ||
|
||
viewabilityTracker?.start() | ||
} | ||
|
||
func register(payload: PrebidImpressionTrackerPayload) { | ||
self.payload = payload | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
PrebidMobile/AdUnits/ImpressionTracking/PrebidImpressionTracker.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* Copyright 2018-2024 Prebid.org, Inc. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import UIKit | ||
|
||
/// Payload for impression trackers. | ||
struct PrebidImpressionTrackerPayload { | ||
|
||
/// seatbid.bid.ext.prebid.targeting.hb_cache_id. | ||
/// Used to identify if SDK found Prebid creative. | ||
let cacheID: String? | ||
|
||
/// URL strings to track when impression conditions are met. | ||
let trackingURLs: [String] | ||
} | ||
|
||
class PrebidImpressionTracker { | ||
|
||
private let tracker: PrebidImpressionTrackerProtocol | ||
|
||
init(isInterstitial: Bool) { | ||
if isInterstitial { | ||
tracker = InterstitialImpressionTracker() | ||
} else { | ||
tracker = BannerViewImpressionTracker() | ||
} | ||
} | ||
|
||
func register(payload: PrebidImpressionTrackerPayload) { | ||
tracker.register(payload: payload) | ||
} | ||
|
||
func start(in adView: UIView) { | ||
DispatchQueue.main.async { | ||
self.tracker.start(in: adView) | ||
} | ||
} | ||
|
||
func stop() { | ||
tracker.stop() | ||
} | ||
} |
Oops, something went wrong.