Tải trước quảng cáo là một tính năng tải quảng cáo do Google quản lý trong Google Mobile Ads SDK, giúp quản lý việc tải và lưu quảng cáo vào bộ nhớ đệm thay cho bạn. Tính năng tải trước quảng cáo yêu cầu bạn thay đổi cách quản lý việc tải quảng cáo. Để tối ưu hoá hiệu suất bằng tính năng tải trước quảng cáo, hãy tắt tính năng lưu vào bộ nhớ đệm tuỳ chỉnh và giao trách nhiệm đó cho Google Mobile Ads SDK.
Tính năng tải trước quảng cáo mang lại những lợi ích sau so với việc tải quảng cáo theo cách thủ công:
- Quản lý tệp đối chiếu: lưu giữ quảng cáo đã tải để bạn không phải duy trì tệp đối chiếu cho đến khi sẵn sàng hiển thị quảng cáo.
- Tự động tải lại: tự động tải một quảng cáo mới khi bạn lấy một quảng cáo ra khỏi bộ nhớ đệm.
- Lần thử lại được quản lý: tự động thử lại các yêu cầu không thành công bằng thuật toán thời gian đợi luỹ thừa.
- Xử lý hết hạn: tự động làm mới quảng cáo trước khi hết hạn (thường là sau một giờ).
- Tối ưu hoá bộ nhớ đệm: nếu bạn sử dụng kích thước bộ nhớ đệm lớn hơn 1, Google Mobile Ads SDK sẽ tối ưu hoá thứ tự bộ nhớ đệm để phân phối quảng cáo phù hợp nhất.
Hướng dẫn này trình bày cách định cấu hình quảng cáo tải trước, kiểm tra xem có quảng cáo tải trước hay không và hiển thị quảng cáo tải trước.
Điều kiện tiên quyết
Trước khi tiếp tục hướng dẫn này, bạn phải thiết lập Google Mobile Ads SDK.
Bắt đầu tải trước quảng cáo
Khi ứng dụng khởi động, hãy gọi phương thức preload một lần. Sau khi
bạn gọi phương thức preload, Google Mobile Ads SDK sẽ tự động
tải trước quảng cáo và thử lại các yêu cầu không thành công đối với cấu hình được tải trước.
Ví dụ sau đây bắt đầu tải trước quảng cáo:
Swift
// Start the preloading initialization process.
let request = Request()
let interstitialConfig = PreloadConfigurationV2(
adUnitID: adUnitID, request: request)
InterstitialAdPreloader.shared.preload(
for: adUnitID, configuration: interstitialConfig, delegate: self)
Objective-C
// Start the preloading initialization process.
GADRequest *request = [GADRequest request];
GADPreloadConfigurationV2 *interstitialConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:adUnitID
request:request];
[GADInterstitialAdPreloader.sharedInstance preloadForPreloadID:adUnitID
configuration:interstitialConfig
delegate:self];
Nhận và hiển thị quảng cáo được tải trước
Khi sử dụng tính năng tải trước quảng cáo, Google Mobile Ads SDK sẽ lưu giữ quảng cáo đã lưu vào bộ nhớ đệm.
Khi bạn muốn hiển thị quảng cáo, hãy gọi phương thức adWithPreloadID.
Google Mobile Ads SDK truy xuất một quảng cáo có sẵn và tự động tải trước quảng cáo tiếp theo ở chế độ nền.
Tránh gọi phương thức adWithPreloadID cho đến khi bạn sẵn sàng hiển thị quảng cáo. Việc giữ quảng cáo trong bộ nhớ đệm cho phép Google Mobile Ads SDK tự động làm mới quảng cáo đã hết hạn và tối ưu hoá bộ nhớ đệm.
Ví dụ sau đây truy xuất và hiển thị một quảng cáo được tải trước:
Swift
private func showInterstitialAd(adUnitID: String) {
// Verify that the preloaded ad is available before polling.
guard isInterstitialAvailable(adUnitID: adUnitID) else {
print("Preloaded interstitial ad is not available.")
return
}
// Polling returns the next available ad and loads another ad in the background.
let ad = InterstitialAdPreloader.shared.ad(with: adUnitID)
// Interact with the ad object as needed.
print("Interstitial ad response info: \(String(describing: ad?.responseInfo))")
ad?.paidEventHandler = { (value: AdValue) in
print("Interstitial ad paid event: \(value.value), \(value.currencyCode)")
}
ad?.fullScreenContentDelegate = self
ad?.present(from: self)
}
Objective-C
- (void)showInterstitialAdWithAdUnitID:(nonnull NSString *)adUnitID {
// Verify that the preloaded ad is available before polling.
if (![self isInterstitialAvailableWithAdUnitID:adUnitID]) {
NSLog(@"Preloaded interstitial ad is not available.");
return;
}
// Getting the preloaded ad loads another ad in the background.
GADInterstitialAd *ad =
[GADInterstitialAdPreloader.sharedInstance adWithPreloadID:adUnitID];
// Interact with the ad object as needed.
NSLog(@"Interstitial ad response info: %@", ad.responseInfo);
ad.paidEventHandler = ^(GADAdValue *_Nonnull value) {
NSLog(@"Interstitial ad paid event: %@ %@ ", value.value, value.currencyCode);
};
ad.fullScreenContentDelegate = self;
[ad presentFromRootViewController:self];
}
Lấy siêu dữ liệu quảng cáo được tải trước
Để lấy siêu dữ liệu quảng cáo được tải trước, bạn có thể tra cứu đối tượng thông tin phản hồi của quảng cáo. Quá trình này cho phép bạn kiểm tra siêu dữ liệu của quảng cáo mà không cần xoá quảng cáo đó khỏi bộ nhớ đệm.
Ví dụ sau đây tra cứu siêu dữ liệu của quảng cáo được tải trước từ đối tượng thông tin phản hồi:
Swift
private func getInterstitialAdResponseInfo(preloadID: String) {
// Get the response info for the preloaded ad.
if let responseInfo = InterstitialAdPreloader.shared.responseInfo(
with: preloadID)
{
print("Ad response ID: \(responseInfo.responseIdentifier ?? "")")
}
}
Objective-C
- (void)getInterstitialAdResponseInfoWithPreloadID:(nonnull NSString *)preloadID {
// Get the response info for the preloaded ad.
GADResponseInfo *responseInfo =
[GADInterstitialAdPreloader.sharedInstance
adResponseInfoWithPreloadID:preloadID];
if (responseInfo) {
NSLog(@"Ad response ID: %@", responseInfo.responseIdentifier);
}
}
Kiểm tra xem có quảng cáo tải trước hay không
Để kiểm tra xem có quảng cáo hay không, hãy chọn một trong các cách sau:
- Nhận thông tin về tình trạng sẵn sàng của quảng cáo tải trước
- Lắng nghe sự kiện có quảng cáo tải trước
Xem tình trạng sẵn có của quảng cáo được tải trước
Ví dụ sau đây kiểm tra xem có quảng cáo hay không:
Swift
private func isInterstitialAvailable(adUnitID: String) -> Bool {
// Verify that an ad is available before polling.
return InterstitialAdPreloader.shared.isAdAvailable(with: adUnitID)
}
Objective-C
- (BOOL)isInterstitialAvailableWithAdUnitID:(nonnull NSString *)adUnitID {
// Verify that an ad is available before polling.
return [GADInterstitialAdPreloader.sharedInstance isAdAvailableWithPreloadID:adUnitID];
}
Theo dõi tình trạng sẵn có của quảng cáo được tải trước
Đăng ký sự kiện tải trước để nhận thông báo khi quảng cáo được tải trước thành công, không tải trước được hoặc bộ nhớ đệm quảng cáo đã hết.
Sự kiện tải trước được dùng cho mục đích phân tích. Trong lệnh gọi lại sự kiện tải trước:
- Không gọi
preload - Tránh gọi
adWithPreloadIDtrừ phi quảng cáo sẽ xuất hiện ngay lập tức.
Ví dụ sau đây đăng ký các sự kiện quảng cáo:
Swift
func adAvailable(forPreloadID preloadID: String, responseInfo: ResponseInfo) {
// This callback indicates that an ad is available for the specified configuration.
// No action is required here, but updating the UI can be useful in some cases.
print("Ad preloaded successfully for ad preload ID: \(preloadID)")
}
func adsExhausted(forPreloadID preloadID: String) {
// This callback indicates that all the ads for the specified configuration have been
// consumed and no ads are available to show. No action is required here, but updating
// the UI can be useful in some cases.
// Don't call InterstitialAdPreloader.shared.preload or
// InterstitialAdPreloader.shared.ad from adsExhausted.
print("Ad exhausted for ad preload ID: \(preloadID)")
}
func adFailedToPreload(forPreloadID preloadID: String, error: Error) {
print(
"Ad failed to load with ad preload ID: \(preloadID), Error: \(error.localizedDescription)"
)
}
Objective-C
- (void)adAvailableForPreloadID:(nonnull NSString *)preloadID
responseInfo:(nonnull GADResponseInfo *)responseInfo {
// This callback indicates that an ad is available for the specified configuration.
// No action is required here, but updating the UI can be useful in some cases.
NSLog(@"Ad preloaded successfully for ad unit ID: %@", preloadID);
}
- (void)adsExhaustedForPreloadID:(nonnull NSString *)preloadID {
// This callback indicates that all the ads for the specified configuration have been
// consumed and no ads are available to show. No action is required here, but updating
// the UI can be useful in some cases.
// Don't call [GAD<Format>AdPreloader preloadForPreloadID:] or
// [GAD<Format>AdPreloader adWithPreloadID:] from adsExhaustedForPreloadID.
NSLog(@"Ad exhausted for ad preload ID: %@", preloadID);
}
- (void)adFailedToPreloadForPreloadID:(nonnull NSString *)preloadID
error:(nonnull NSError *)error {
NSLog(@"Ad failed to load with ad preload ID: %@, Error: %@", preloadID,
error.localizedDescription);
}
Ngừng tải trước quảng cáo
Nếu không cần hiển thị lại quảng cáo cho một mã tải trước trong phiên, bạn có thể ngừng tải trước quảng cáo. Để ngừng tải trước quảng cáo cho một mã tải trước cụ thể, hãy gọi stopPreloadingAndRemoveAdsForPreloadID bằng mã tải trước. Để dừng tải trước cho tất cả trình tải trước, hãy gọi stopPreloadingAndRemoveAllAds.
Đặt dung lượng bộ nhớ đệm
Dung lượng bộ nhớ đệm kiểm soát số lượng quảng cáo được tải trước trong bộ nhớ. Theo mặc định, Google tối ưu hoá dung lượng bộ nhớ đệm để cân bằng mức tiêu thụ bộ nhớ và độ trễ phân phát quảng cáo. Nếu ứng dụng của bạn hiển thị quảng cáo trước khi quảng cáo tiếp theo được tải, thì bạn có thể đặt dung lượng bộ nhớ đệm tuỳ chỉnh để tăng số lượng quảng cáo được lưu trong bộ nhớ.
Swift
let preloadConfig = PreloadConfigurationV2(adUnitID: adUnitID)
// Define a PreloadConfiguration and set the buffer size to 2 preloaded ads.
preloadConfig.bufferSize = 2
Objective-C
GADPreloadConfigurationV2 *preloadConfig =
[[GADPreloadConfigurationV2 alloc] initWithAdUnitID:adUnitID];
// Define a PreloadConfiguration and set the buffer size to 2 preloaded ads.
preloadConfig.bufferSize = 2;
Giới hạn bộ nhớ đệm tải trước
Google Mobile Ads SDK thực thi giới hạn trên toàn ứng dụng đối với tổng số quảng cáo được tải trước trên tất cả đơn vị quảng cáo và mã tải trước:
- Giới hạn mặc định: Google Mobile Ads SDK lưu giữ tối đa 6 quảng cáo được tải trước trong bộ nhớ. Giới hạn này được áp dụng cho tất cả định dạng và mã tải trước.
- Bạn nên duy trì dung lượng bộ nhớ đệm là 2 cho mỗi mã tải trước.