Hiển thị paywall được nhắm mục tiêu bởi Apple Ads khi khởi chạy lần đầu trên iOS SDK
Apple Ads (AA) attribution đến bất đồng bộ sau Adapty.activate(). Nếu bạn gọi getFlow quá sớm, attribution thường chưa được ghi nhận và Adapty sẽ phân giải placement theo đối tượng mặc định — bỏ qua các paywall được phân khúc theo AA. AdaptyProfile.appliedExternalAttributionProviders cho phép ứng dụng phát hiện khi nào AA attribution đã được áp dụng vào hồ sơ người dùng, để yêu cầu paywall có thể chờ cho đến khi phân khúc AA được phân giải chính xác.
appliedExternalAttributionProviders chỉ báo cáo Apple Ads. Attribution từ các nhà cung cấp khác vẫn hiển thị trên hồ sơ người dùng và có thể dùng trong bộ lọc phân khúc, nhưng chưa xuất hiện ở đây.
Trước khi bắt đầu
Bạn cần:
- Adapty iOS SDK 4.1 trở lên.
- Apple Ads đã được cấu hình cho ứng dụng trong Adapty. Xem Apple Ads.
Các ví dụ sử dụng tên API từ SDK 4.1. Trên SDK 4.0, thuộc tính profile được đặt tên là appliedAttributionSources. Trên SDK 3.x, thuộc tính đó có cùng tên cũ, và paywall được lấy bằng getPaywall/getPaywallForDefaultAudience, trả về một AdaptyPaywall. Xem Migrate Adapty iOS SDK sang v4.1.
Cách hoạt động
Sau khi Adapty.activate(), SDK sẽ yêu cầu attribution từ Apple Ads trong nền và chuyển kết quả về backend của Adapty. Khi AA trở thành nguồn attribution đang hoạt động cho hồ sơ người dùng, SDK sẽ gửi một AdaptyProfile đã cập nhật với mảng appliedExternalAttributionProviders chứa .appleAds.
Mảng rỗng có thể có nghĩa là:
- Attribution từ Apple Ads chưa được xử lý cho hồ sơ người dùng này.
- Chưa có attribution nào được nhận.
- Attribution đã đến từ nhà cung cấp khác, và mảng này không báo cáo điều đó.
Ngay cả với một mảng rỗng, getFlow vẫn có thể gọi an toàn — Adapty sẽ xử lý yêu cầu dựa trên đối tượng phù hợp với trạng thái hồ sơ người dùng hiện tại, thường là đối tượng mặc định.
Việc chờ đợi chỉ áp dụng cho lần khởi chạy đầu tiên. Sau khi attribution Apple Ads đã được ghi lại, dữ liệu đó sẽ được lưu trữ vĩnh viễn trong hồ sơ người dùng. Ở mọi lần khởi chạy tiếp theo, hồ sơ người dùng đã được cache sẽ mang sẵn .appleAds trong appliedExternalAttributionProviders, didLoadLatestProfile sẽ kích hoạt ngay với giá trị đó, và getFlow trả về paywall theo phân khúc Apple Ads mà không cần chờ đợi.
Triển khai
Ở lần khởi chạy đầu tiên, hãy theo dõi .appleAds trong hồ sơ người dùng và áp dụng một timeout cứng — nếu attribution Apple Ads không bao giờ đến, những người dùng đó vẫn cần được xem paywall.
- Kích hoạt SDK. Xem Cài đặt & cấu hình iOS SDK.
- Đăng ký nhận cập nhật hồ sơ người dùng bằng cách tuân thủ
AdaptyDelegatevà triển khaididLoadLatestProfile. Nếu bạn chưa thiết lập delegate, xem Lắng nghe cập nhật gói đăng ký. - Theo dõi
.appleAdstrongappliedExternalAttributionProviders. Khi nó xuất hiện, hãy yêu cầu paywall — Adapty sẽ trả về biến thể được phân khúc theo AA:
extension <YourAdaptyDelegateImpl>: AdaptyDelegate {
nonisolated func didLoadLatestProfile(_ profile: AdaptyProfile) {
if profile.appliedExternalAttributionProviders.contains(where: { $0 == .appleAds }) {
// load the paywall via Adapty.getFlow(placementId:)
}
}
}
- Bắt đầu một bộ đếm thời gian 3–5 giây song song với việc đăng ký. Nếu bộ đếm thời gian kích hoạt trước khi
.appleAdsxuất hiện, hãy yêu cầu paywall ngay:
Bất kỳ đường dẫn nào kích hoạt trước đều nên tải paywall; đường dẫn còn lại nên bị bỏ qua. Sử dụng một cờ trạng thái duy nhất (ví dụ: hasLoadedPaywall) để loại trùng lặp để tránh tải paywall hai lần. Cấu hình paywall dự phòng cho placement để người dùng không bao giờ bị kẹt nếu yêu cầu mạng thất bại.
Ví dụ hoàn chỉnh
Phần triển khai dưới đây chạy đua giữa attribution và timeout, tải trước paywall dành cho đối tượng mặc định song song, và trả về paywall phù hợp. Phía gọi hàm chỉ cần await một hàm async duy nhất — không cần delegate hay cờ trạng thái nào ở call site.
ProfileObserver là một singleton có thể tái sử dụng, phát ra các cập nhật hồ sơ người dùng từ AdaptyDelegate. FlowLoader.getFlowOrDefault chạy cuộc đua bằng TaskGroup trong structured concurrency:
- Nếu attribution đến trong khoảng
timeout, nó sẽ trả về paywall đã phân khúc thông quagetFlow(placementId:). - Nếu
timeouthết trước, nó sẽ trả về paywall đối tượng mặc định đã được tải trước thông quagetFlowForDefaultAudience(placementId:).
/// Demonstrates how to fetch a paywall that depends on attribution being applied,
/// falling back to the default-audience paywall if attribution doesn't arrive in time.
///
/// Stateless and self-contained: every call kicks off its own default-audience
/// prefetch and races it against attribution + segmented fetch.
enum FlowLoader {
static func getFlowOrDefault(
placementId: String,
timeout: TimeInterval
) async throws -> AdaptyFlow {
struct TimedOut: Error {}
// Kick off the default-audience request immediately so it has the full
// `timeout` window to load. We'll either cancel it on success or await
// its result on timeout — never a duplicate network call.
let defaultFlowTask = Task {
try await Adapty.getFlowForDefaultAudience(placementId: placementId)
}
do {
// Race two child tasks: whichever finishes first wins.
let result = try await withThrowingTaskGroup(of: AdaptyFlow.self) { group in
// 1. Wait for attribution, then ask Adapty for the segmented paywall.
group.addTask {
await waitForAttribution()
return try await Adapty.getFlow(placementId: placementId)
}
// 2. Time-bomb: throws `TimedOut` after `timeout` seconds.
group.addTask {
try await Task.sleep(nanoseconds: UInt64(timeout * 1_000_000_000))
throw TimedOut()
}
guard let value = try await group.next() else { throw CancellationError() }
group.cancelAll() // stop the loser (sleeper or the attribution wait).
return value
}
// Segmented paywall won — we no longer need the default-audience prefetch.
defaultFlowTask.cancel()
return result
} catch is TimedOut {
// Attribution didn't apply in time — return the prefetched default
// (instant if already done, otherwise we await the in-flight request).
return try await defaultFlowTask.value
}
}
/// Suspends until a profile with the desired attribution source is observed.
/// `@Published.values` emits the current profile immediately on subscription,
/// so this returns on the first iteration if attribution is already applied.
@MainActor
private static func waitForAttribution() async {
for await profile in ProfileObserver.shared.$profile.values {
if profile?.appliedExternalAttributionProviders.contains(.appleAds) == true { return }
}
}
}
@MainActor
final class ProfileObserver: AdaptyDelegate {
static let shared = ProfileObserver()
@Published private(set) var profile: AdaptyProfile?
nonisolated func didLoadLatestProfile(_ profile: AdaptyProfile) {
Task { @MainActor [weak self] in
self?.profile = profile
}
}
}
Kết nối ProfileObserver vào AdaptyDelegate một lần, sau khi Adapty.activate() hoàn tất:
Adapty.delegate = ProfileObserver.shared
Gọi từ màn hình splash:
do {
let flow = try await FlowLoader.getFlowOrDefault(
placementId: "YOUR_PLACEMENT_ID",
timeout: 5
)
// present the paywall
} catch {
// handle the error or show a fallback paywall
}
Nếu ứng dụng của bạn đã sử dụng AdaptyDelegate cho các mục đích khác (ví dụ: lắng nghe cập nhật gói đăng ký), hãy chuyển tiếp didLoadLatestProfile đến ProfileObserver.shared từ delegate hiện có của bạn thay vì đặt Adapty.delegate = ProfileObserver.shared.