PAL 可讓您在廣告請求中和廣告播放期間傳送 Google 廣告信號。
本指南說明如何在應用程式中加入 Android PAL SDK。如要查看使用 PAL 產生隨機值的範例應用程式,請從 GitHub 下載 Android 範例。
將 Android PAL SDK 新增為程式庫
自 18.0.0 版起,PAL SDK 由 Google 的 Maven 存放區代管,您可以按照下列步驟將其新增至應用程式:
implementation 'com.google.android.gms:play-services-pal:22.1.0'
或者,您也可以從 Google 的 Maven 存放區下載 PAL SDK,然後手動新增至應用程式。
產生 Nonce
Nonce 是 PAL 使用 NonceLoader
類別產生的單一加密字串。PAL 要求每個串流要求都必須附上專屬隨機值。不過,您可以在同一個串流中,將隨機碼重複用於多個廣告請求。如要使用 PAL SDK 產生隨機碼,請進行下列變更來匯入及設定 PAL,並建立產生隨機碼的函式:
請按照下列步驟匯入及設定 PAL:
匯入 PAL 課程:
import com.google.ads.interactivemedia.pal.ConsentSettings; import com.google.ads.interactivemedia.pal.NonceLoader; import com.google.ads.interactivemedia.pal.NonceManager; import com.google.ads.interactivemedia.pal.NonceRequest; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import java.util.HashSet; import java.util.Set;
建立私有變數,儲存
NonceLoader
和NonceManager
例項:private NonceLoader nonceLoader; private NonceManager nonceManager;
在
onCreate
方法中,使用ConsentSettings
執行個體初始化NonceLoader
執行個體:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // The default value for allowStorage() is false, but can be // changed once the appropriate consent has been gathered. The // getConsentToStorage() method is a placeholder for the publisher's own // method of obtaining user consent, either by integrating with a CMP or // based on other methods the publisher chooses to handle storage consent. boolean isStorageAllowed = getConsentToStorage(); ConsentSettings consentSettings = ConsentSettings.builder().allowStorage(isStorageAllowed).build(); // It is important to instantiate the NonceLoader as early as possible to // allow it to initialize and preload data for a faster experience when // loading the NonceManager. A new NonceLoader will need to be instantiated // if the ConsentSettings change for the user. nonceLoader = new NonceLoader(this, consentSettings); adClickButton = findViewById(R.id.send_click_button); logView = findViewById(R.id.log_view); logView.setMovementMethod(new ScrollingMovementMethod()); }
在應用程式中,為每個使用者工作階段建立一個
NonceLoader
類別的執行個體。如果應用程式有多個頁面或同等建構函式,請為每個頁面或同等頁面建立新的NonceLoader
執行個體。使用相同的NonceLoader
例項,即可在網頁或應用程式使用者工作階段的生命週期內,讓網頁關聯器&correlator
維持不變。您仍可控制串流關聯器&scor
,但必須為每個新串流產生新的隨機號碼,藉此重設串流關聯器。如要使用展示頻率上限和競爭排除功能,同一串流的所有廣告請求都必須共用相同的
NonceLoader
例項和串流關聯值。產生 Nonce:
public void generateNonceForAdRequest(View view) { logMessage("Generate Nonce Request"); Set supportedApiFrameWorksSet = new HashSet(); // The values 2, 7, and 9 correspond to player support for VPAID 2.0, // OMID 1.0, and SIMID 1.1. supportedApiFrameWorksSet.add(2); supportedApiFrameWorksSet.add(7); supportedApiFrameWorksSet.add(9); NonceRequest nonceRequest = NonceRequest.builder() .descriptionURL("https://example.com/content1") .iconsSupported(true) .omidPartnerVersion("6.2.1") .omidPartnerName("Example Publisher") .playerType("ExamplePlayerType") .playerVersion("1.0.0") .ppid("testPpid") .sessionId("Sample SID") .supportedApiFrameworks(supportedApiFrameWorksSet) .videoPlayerHeight(480) .videoPlayerWidth(640) .willAdAutoPlay(true) .willAdPlayMuted(false) .build(); nonceLoader .loadNonceManager(nonceRequest) .addOnSuccessListener( new OnSuccessListener<NonceManager>() { @Override public void onSuccess(NonceManager manager) { nonceManager = manager; String nonceString = manager.getNonce(); logMessage("Nonce generated"); logMessage(nonceString.substring(0, 20) + "..."); Log.i(LOG_TAG, "Generated nonce: " + nonceString); // From here you would trigger your ad request and move on to initialize content. exampleMakeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString); adClickButton.setEnabled(true); } }) .addOnFailureListener( new OnFailureListener() { @Override public void onFailure(Exception error) { logMessage("Nonce generation failed"); Log.e(LOG_TAG, "Nonce generation failed: " + error.getMessage()); } }); }
在單一串流播放中,所有廣告請求只需要一個隨機碼。 為進行測試,請在測試應用程式中點選按鈕時呼叫此函式。本指南中設定的
NonceRequest
參數為範例參數。根據自家應用程式的特性設定參數。這個函式會以非同步方式產生隨機數。您必須處理 Nonce 要求成功和失敗的情況。Nonce 管理工具可用後,請先使用
nonceManager.getNonce()
方法擷取 Nonce,再發出廣告請求。
將隨機碼附加至廣告請求
如要使用產生的隨機碼,請在發出廣告請求前,將廣告代碼附加 givn
參數和隨機碼值:
// From here you would trigger your ad request and move on to initialize content.
exampleMakeAdRequest(DEFAULT_AD_TAG + "&givn=" + nonceString);
追蹤播放事件
如要追蹤播放事件,請務必設定事件處理常式,將廣告信號傳送至 Google:
// Triggered when a user clicks-through on an ad which was requested using a PAL nonce.
public void sendAdClick(View view) {
logMessage("Ad click sent");
if (nonceManager != null) {
nonceManager.sendAdClick();
}
}
// In a typical PAL app, this is called when a user touch or click is detected,
// on the ad other than an ad click-through.
public void onVideoViewTouch(MotionEvent e) {
if (nonceManager != null) {
nonceManager.sendAdTouch(e);
}
}
// In a typical PAL app, this is called when a content playback session starts.
public void sendPlaybackStart() {
logMessage("Playback start");
if (nonceManager != null) {
nonceManager.sendPlaybackStart();
}
}
// In a typical PAL app, this is called when a content playback session ends.
public void sendPlaybackEnd() {
logMessage("Playback end");
if (nonceManager != null) {
nonceManager.sendPlaybackEnd();
}
}
以下說明在實作項目中呼叫各個函式的時機:
sendPlaybackStart()
:影片播放工作階段開始時sendPlaybackEnd()
:影片播放工作階段結束時sendAdClick()
:觀眾每次點按廣告sendTouch()
:每次與播放器互動時
為了進行測試,請將事件處理常式方法附加至按鈕點擊事件。在實際執行環境中,請設定應用程式的播放器事件,以便呼叫事件處理常式方法。
(選用) 透過第三方廣告伺服器傳送 Google Ad Manager 信號
設定第三方廣告伺服器與 Google Ad Manager 搭配運作時,請參閱伺服器的說明文件,擷取並轉送每個廣告請求中的隨機碼值。提供的範例是包含隨機碼參數的廣告請求網址。Nonce 參數會從 PAL SDK 傳播,經過中介伺服器,然後傳到 Ad Manager,進而提升營利成效。
設定第三方廣告伺服器,在伺服器向 Ad Manager 發出的要求中加入隨機碼。以下是在第三方廣告伺服器中設定的廣告代碼範例:
'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'
詳情請參閱 Google Ad Manager 伺服器端導入指南。
Ad Manager 會尋找 givn=
來識別隨機碼值。第三方廣告伺服器必須支援自己的巨集 (例如 %%custom_key_for_google_nonce%%
),並將其替換為您在上一個步驟中提供的隨機值查詢參數。如要進一步瞭解如何完成這項操作,請參閱第三方廣告伺服器的說明文件。