如果使用较长的瀑布流中介链和多个第三方适配器来初始化应用,可能会增加广告请求的延迟,进而影响到用户会话中关键的首次广告展示。首次展示的延迟时间增加可能会导致广告无法展示,因为广告请求尚未返回响应。
本指南涵盖以下主题,旨在减少应用会话中首次广告展示的延迟时间:
准备工作
在继续操作之前,请设置 Google Mobile Ads SDK。
设置初始化超时
为避免错失展示机会,请设置初始化超时。此方法可防止应用超出您设置的时间限制。您可以在设置的时间限制过后或初始化完成后继续请求广告。
以下示例将初始化超时设置为 5 秒:
Swift
Task {
// Create TaskGroup to manage a race between
// SDK initialization and a 5 second timer.
await withTaskGroup(of: Void.self) { group in
// Create a task to initialize the MobileAds SDK.
group.addTask {
await withCheckedContinuation { continuation in
MobileAds.shared.start { _ in
// SDK initialization finished, resume the continuation.
// This will not stop the SDK's internal initialization process.
continuation.resume()
}
}
}
// Create a cancellable task which is a 5 second timer.
group.addTask {
do {
// Sleep for 5 seconds. This delay *is* cancellable.
try await Task.sleep(nanoseconds: 5 * 1_000_000_000)
} catch {
// cancelAll() will stop this task and throw CancellationError.
// This happens if the SDK initialization completes first.
}
}
// Wait for either task to complete.
await group.next()
// Cancel the timer task if the SDK initialized first.
group.cancelAll()
}
// Load an ad once after task group finishes.
await self.loadAd()
从您的第一个请求中移除瀑布流广告来源
若要顺畅地投放广告,避免瀑布流广告来源造成任何额外的延迟,请实现键值对细分,以从第一个广告请求中移除瀑布流广告来源。为此,请完成以下步骤:
- 创建中介组并添加出价广告来源。
- 使用键
latency和值low向新的仅出价中介组添加键值对细分。 - 在界面中,将新的仅出价中介组的优先级设置为高于主要中介组。若要调整顺序,请更改中介组优先级。
在您的第一个广告请求中,添加自定义定位键值对:
Swift
let customTargeting: [String: String] = ["latency": "low"] let request = GADRequest() request.customTargeting = customTargetingObjective-C
NSDictionary *customTargeting = @{@"latency" : @"low"}; GADRequest *request = [GADRequest request]; request.customTargeting = customTargeting;测试键值对细分设置,以验证您是否正确定位了新的仅出价中介组。
提升对时间不太敏感的展示的效果
为了提升对时间不太敏感的展示(例如无需在应用启动时立即展示的广告)的效果,请使用广告预加载,并确保广告请求不定位到仅出价中介组。