Repository navigation
Add ad consent option for Google Ads conversion uploads #544
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d02c2ea
Add ad consent option reported as device attributes
yusuftor 7e6e089
Make AdConsent immutable
yusuftor 426667a
Report ad personalization as denied when tracking isn't allowed
yusuftor 9747972
Republish device attributes when ATT changes ad personalization
yusuftor 53a0e23
Rename to AdConsentStatus and serialize ad consent updates
yusuftor a55a7ac
Test the paywall tracking-permission republish hook
yusuftor 8ed70bf
Only count device attributes as sent once they're queued
yusuftor 5757f03
Always re-send device attributes when leaving .none
yusuftor 5c1f9aa
Let tests give AppSessionManager its own NotificationCenter
yusuftor cd4f064
Fix ad consent tests freeing the device helper under IdentityManager
yusuftor 91aad74
Re-send device attributes after any tracking-behavior change
yusuftor 1750d2d
Say ad consent is only passed on to Google Ads for now
yusuftor d34d273
Read ad consent from IAB TCF consent banners
yusuftor de211ff
Reconcile ad consent after every device attributes send
yusuftor 1d7552f
Describe what the ad consent code does, not its history
yusuftor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,74 @@ | ||
| // | ||
| // AdConsent.swift | ||
| // | ||
| // | ||
| // Created by Yusuf Tör on 08/10/2026. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Whether a user has granted or denied consent for an advertising purpose. | ||
| @objc(SWKConsentStatus) | ||
| public enum AdConsentStatus: Int, CustomStringConvertible, Sendable { | ||
| /// The user has granted consent. This is the default. | ||
| case granted = 0 | ||
|
|
||
| /// The user has denied consent. | ||
| case denied = 1 | ||
|
|
||
| public var description: String { | ||
| switch self { | ||
| case .granted: return "granted" | ||
| case .denied: return "denied" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A user's consent for how their data is used for advertising. | ||
| /// | ||
| /// Superwall passes this on with the conversions it uploads to Google Ads and Meta. | ||
| /// Both values default to ``AdConsentStatus/granted``. | ||
| /// | ||
| /// It's immutable: to change consent, assign a new `AdConsent`. | ||
| @objc(SWKAdConsent) | ||
| @objcMembers | ||
| public final class AdConsent: NSObject { | ||
| /// Consent to send user data to ad networks for advertising. | ||
| public let adUserData: AdConsentStatus | ||
|
|
||
| /// Consent for them to use that data for personalized advertising. | ||
| public let adPersonalization: AdConsentStatus | ||
|
|
||
| public init( | ||
| adUserData: AdConsentStatus = .granted, | ||
| adPersonalization: AdConsentStatus = .granted | ||
| ) { | ||
| self.adUserData = adUserData | ||
| self.adPersonalization = adPersonalization | ||
| super.init() | ||
| } | ||
|
|
||
| public override convenience init() { | ||
| self.init(adUserData: .granted, adPersonalization: .granted) | ||
| } | ||
|
|
||
| /// The consent to report. Both purposes are denied while `eventTrackingBehavior` | ||
| /// is ``EventTrackingBehavior/none``, and personalization is denied when the user | ||
| /// hasn't allowed tracking (`attStatus` is restricted or denied). An undetermined | ||
| /// or missing ATT status leaves the values in place. Pass `nil` for consent the app | ||
| /// set itself, which the ATT rule doesn't override. | ||
| func reported( | ||
| for eventTrackingBehavior: EventTrackingBehavior, | ||
| attStatus: Int? | ||
| ) -> AdConsent { | ||
| if eventTrackingBehavior == .none { | ||
| return AdConsent(adUserData: .denied, adPersonalization: .denied) | ||
| } | ||
| switch attStatus.flatMap(FakeTrackingAuthorizationStatus.init(rawValue:)) { | ||
| case .restricted, .denied: | ||
| return AdConsent(adUserData: adUserData, adPersonalization: .denied) | ||
| case .notDetermined, .authorized, nil: | ||
| return self | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
Sources/SuperwallKit/Config/Options/AdConsentUpdateQueue.swift
This file contains hidden or 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,58 @@ | ||
| // | ||
| // AdConsentUpdateQueue.swift | ||
| // | ||
| // | ||
| // Created by Yusuf Tör on 08/10/2026. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| /// Sends ad consent updates one at a time, in the order they were made, so an older | ||
| /// snapshot can never be tracked after a newer one. | ||
| /// | ||
| /// Each ``Superwall/adConsent`` assignment starts a new generation. A job checks | ||
| /// that its generation is still current before tracking, so a superseded update is | ||
| /// dropped in favour of the newer one queued behind it. | ||
| final class AdConsentUpdateQueue: @unchecked Sendable { | ||
| typealias Job = @Sendable () async -> Void | ||
|
|
||
| private let continuation: AsyncStream<Job>.Continuation | ||
| private let lock = NSLock() | ||
| private var generation = 0 | ||
|
|
||
| init() { | ||
| let (stream, continuation) = AsyncStream.makeStream(of: Job.self) | ||
| self.continuation = continuation | ||
| Task { | ||
| for await job in stream { | ||
| await job() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| continuation.finish() | ||
| } | ||
|
|
||
| /// The generation of the latest assignment. | ||
| var currentGeneration: Int { | ||
| lock.withLock { generation } | ||
| } | ||
|
|
||
| /// Starts a new generation, making every earlier one stale. | ||
| func nextGeneration() -> Int { | ||
| lock.withLock { | ||
| generation += 1 | ||
| return generation | ||
| } | ||
| } | ||
|
|
||
| func isCurrent(_ generation: Int) -> Bool { | ||
| lock.withLock { self.generation == generation } | ||
| } | ||
|
|
||
| /// Runs `job` after every job enqueued before it has finished. | ||
| func enqueue(_ job: @escaping Job) { | ||
| continuation.yield(job) | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.