Skip to content

Commit

Permalink
Adds the alternative way to set custom URL with nonTrackingURL.
Browse files Browse the repository at this point in the history
  • Loading branch information
mdanylov-sigma committed Mar 3, 2025
1 parent 9947afd commit 0948e3b
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 8 deletions.
2 changes: 1 addition & 1 deletion PrebidMobile.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
5BC37908271F1CFF00444D5E /* PBMFunctions.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376AA271F1CFE00444D5E /* PBMFunctions.h */; settings = {ATTRIBUTES = (Public, ); }; };
5BC37909271F1CFF00444D5E /* PBMDeepLinkPlusHelper+Testing.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376AB271F1CFE00444D5E /* PBMDeepLinkPlusHelper+Testing.h */; };
5BC3790B271F1CFF00444D5E /* PBMWKScriptMessageHandlerLeakAvoider.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376AD271F1CFE00444D5E /* PBMWKScriptMessageHandlerLeakAvoider.h */; };
5BC3790F271F1CFF00444D5E /* PBMDeviceAccessManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376B1271F1CFE00444D5E /* PBMDeviceAccessManager.h */; };
5BC3790F271F1CFF00444D5E /* PBMDeviceAccessManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376B1271F1CFE00444D5E /* PBMDeviceAccessManager.h */; settings = {ATTRIBUTES = (Public, ); }; };
5BC37910271F1CFF00444D5E /* PBMWindowLocker.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BC376B2271F1CFE00444D5E /* PBMWindowLocker.m */; };
5BC37911271F1CFF00444D5E /* PBMDeepLinkPlus.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BC376B3271F1CFE00444D5E /* PBMDeepLinkPlus.m */; };
5BC37912271F1CFF00444D5E /* PBMConstants.h in Headers */ = {isa = PBXBuildFile; fileRef = 5BC376B4271F1CFE00444D5E /* PBMConstants.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down
1 change: 1 addition & 0 deletions PrebidMobile/BuildFiles/PrebidMobile.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ framework module PrebidMobile {
header "PBMAbstractCreative.h"
header "PBMAdViewManager.h"
header "PBMViewExposure.h"
header "PBMDeviceAccessManager.h"

header "PBMBidRequester.h"
header "Log+Extensions.h"
Expand Down
11 changes: 10 additions & 1 deletion PrebidMobile/ConfigurationAndTargeting/Prebid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,16 @@ public class Prebid: NSObject {
/// - Throws: An error if setting the custom host URL fails.
public func setCustomPrebidServer(url: String) throws {
prebidServerHost = .Custom
try Host.shared.setCustomHostURL(url)
try Host.shared.setCustomHostURL(url, nonTrackingURLString: nil)
}

/// Sets a custom Prebid Server URL.
/// - Parameter url: The custom Prebid Server URL, used when a user allowed the app to track.
/// - Parameter nonTrackingURL:The custom Prebid Server URL, used when a user rejected the app to track.
/// - Throws: An error if setting the custom host URL fails.
public func setCustomPrebidServer(url: String, nonTrackingURL: String) throws {
prebidServerHost = .Custom
try Host.shared.setCustomHostURL(url, nonTrackingURLString: nonTrackingURL)
}

// MARK: - Stored Bid Response
Expand Down
42 changes: 37 additions & 5 deletions PrebidMobile/Host.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
*/

import UIKit
import AppTrackingTransparency

/// `PrebidHost` represents various Prebid server hosts used for ad bidding.
@objc public enum PrebidHost: Int {
Expand Down Expand Up @@ -41,19 +42,49 @@ import UIKit
@objcMembers
public class Host: NSObject {

private var customHostURL: URL?
private var customHostURL: URL? {
get {
guard nonTrackingURL != nil else {
return trackingURL
}

guard #available(iOS 14.0, *) else {
return deviceManager.advertisingTrackingEnabled() ? trackingURL : nonTrackingURL
}
let isAutorized = deviceManager.appTrackingTransparencyStatus() == ATTrackingManager.AuthorizationStatus.authorized.rawValue
return isAutorized ? trackingURL : nonTrackingURL
}
}

private var trackingURL: URL?
private var nonTrackingURL: URL?

private var deviceManager = PBMDeviceAccessManager(rootViewController: nil)

/// The class is created as a singleton object & used
public static let shared = Host()

override init() {}

convenience init(deviceManager: PBMDeviceAccessManager) {
self.init()
self.deviceManager = deviceManager
}

/// The CustomHost property holds the URL for the custom prebid adaptor
@objc public func setCustomHostURL(_ urlString: String?) throws {
guard let url = URL.urlWithoutEncoding(from: urlString) else {
@objc public func setCustomHostURL(_ urlString: String?, nonTrackingURLString: String?) throws {
guard let trackingURL = URL.urlWithoutEncoding(from: urlString) else {
throw ErrorCode.prebidServerURLInvalid(urlString ?? "")
}
customHostURL = url

if let nonTrackingURLString {
guard let nonTrackingURL = URL.urlWithoutEncoding(from: nonTrackingURLString) else {
throw ErrorCode.prebidServerURLInvalid(nonTrackingURLString)
}
self.nonTrackingURL = nonTrackingURL
}

self.trackingURL = trackingURL
}

/// This function retrieves the prebid server URL for the selected host
Expand Down Expand Up @@ -87,6 +118,7 @@ public class Host: NSObject {
/// This function used for unit testing to reset `customHostURL`.
/// nternal only.
func reset() {
customHostURL = nil
trackingURL = nil
nonTrackingURL = nil
}
}
44 changes: 43 additions & 1 deletion PrebidMobileTests/RenderingTests/Tests/PrebidTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class PrebidTest: XCTestCase {

Prebid.reset()
PrebidMobilePluginRegister.shared.unregisterAllPlugins()
MockDeviceAccessManager.reset()

super.tearDown()
}
Expand Down Expand Up @@ -155,14 +156,55 @@ class PrebidTest: XCTestCase {
// try! Prebid.shared.setCustomPrebidServer(url: customHost)

Prebid.shared.prebidServerHost = PrebidHost.Custom
try Host.shared.setCustomHostURL(customHost)
try Host.shared.setCustomHostURL(customHost, nonTrackingURLString: nil)

//then
XCTAssertEqual(PrebidHost.Custom, Prebid.shared.prebidServerHost)
let getHostURLResult = try Host.shared.getHostURL(host: .Custom)
XCTAssertEqual(customHost, getHostURLResult)
}

func testServerHostCustomOnAuthorizedTrackingStatus() throws {
//given
let customTrackingHost = "https://prebid-server.tracking.com/openrtb2/auction"
let customNonTrackingHost = "https://prebid-server.nontracking.com/openrtb2/auction"

MockDeviceAccessManager.mockAdvertisingTrackingEnabled = true
if #available(iOS 14, *) {
MockDeviceAccessManager.mockAppTrackingTransparencyStatus = .authorized
}
let host = Host(deviceManager: MockDeviceAccessManager(rootViewController: nil))

//when
try host.setCustomHostURL(customTrackingHost, nonTrackingURLString: customNonTrackingHost)

//then
XCTAssertEqual(PrebidHost.Custom, Prebid.shared.prebidServerHost)
let getHostURLResult = try host.getHostURL(host: .Custom)
XCTAssertEqual(customTrackingHost, getHostURLResult)
}

func testServerHostCustomOnNonAuthorizedTrackingStatus() throws {
//given
let customTrackingHost = "https://prebid-server.tracking.com/openrtb2/auction"
let customNonTrackingHost = "https://prebid-server.nontracking.com/openrtb2/auction"

MockDeviceAccessManager.mockAdvertisingTrackingEnabled = false
if #available(iOS 14, *) {
MockDeviceAccessManager.mockAppTrackingTransparencyStatus = .denied
}
let host = Host(deviceManager: MockDeviceAccessManager(rootViewController: nil))

//when
Prebid.shared.prebidServerHost = PrebidHost.Custom
try host.setCustomHostURL(customTrackingHost, nonTrackingURLString: customNonTrackingHost)

//then
XCTAssertEqual(PrebidHost.Custom, Prebid.shared.prebidServerHost)
let getHostURLResult = try host.getHostURL(host: .Custom)
XCTAssertEqual(customNonTrackingHost, getHostURLResult)
}

func testAccountId() {
//given
let serverAccountId = "123"
Expand Down

0 comments on commit 0948e3b

Please sign in to comment.