Điều kiện tiên quyết
Để tích hợp và kiểm thử PAL SDK cho Cast, bạn cần có những thứ sau:
Một ứng dụng nhận để tạo số chỉ dùng một lần bằng cách sử dụng hoạt động chặn thông báo.
Một ứng dụng người gửi để kích hoạt bộ nhận tải nội dung có chứa yêu cầu quảng cáo.
Vì chỉ cần cập nhật ứng dụng nhận để tích hợp PAL SDK, nên bạn có thể sử dụng Công cụ điều khiển và lệnh truyền (CAC) làm trình gửi web để kiểm thử ứng dụng nhận.
Bạn có thể chạy mẫu ở cuối mỗi bước bằng cách trước tiên chạy ứng dụng nhận web trong Công cụ CAC, sau đó thực hiện mọi yêu cầu tải.
Tạo số chỉ dùng một lần
"Số chỉ dùng một lần" là một chuỗi được mã hoá duy nhất do PAL tạo thông qua NonceManager. NonceManager được tạo bằng phương thức loadNonceManager của NonceLoader, dựa trên các chế độ cài đặt được truyền trong NonceRequest. Để xem một ứng dụng mẫu sử dụng PAL để tạo số chỉ dùng một lần, hãy tải ví dụ Cast xuống từ GitHub.
Mỗi yêu cầu phát trực tiếp mới đều cần một số chỉ dùng một lần mới. Nhiều yêu cầu quảng cáo trong cùng một luồng phát có thể sử dụng cùng một số chỉ dùng một lần. Để tạo số chỉ dùng một lần bằng PAL SDK, trước tiên, hãy tạo một ứng dụng web receiver tuỳ chỉnh và thêm mã sau:
receiver.html
<!DOCTYPE html>
<html>
<head>
<script src="//www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
<script src="//imasdk.googleapis.com/pal/sdkloader/cast_pal.js"></script>
</head>
<body>
<cast-media-player></cast-media-player>
<footer>
<script src="js/receiver.js" type="module"></script>
</footer>
</body>
</html>
Phần tử <cast-media-player> đại diện cho giao diện người dùng trình phát tích hợp do Cast Web Receiver API cung cấp. Tuỳ thuộc vào loại sự kiện phát trực tiếp, trình phát thực tế được dùng có thể khác nhau. Bạn có thể xem chính xác phiên bản của những trình phát này trong ghi chú phát hành của Google Cast SDK.
Tiếp theo, hãy thêm mã sau để chặn các sự kiện LOAD và tạo một số chỉ dùng một lần mỗi khi trình nhận tải một đối tượng MediaInformation mới:
js/receiver.js
const castContext = cast.framework.CastReceiverContext.getInstance();
const playerManager = castContext.getPlayerManager();
const consentSettings = new goog.cast.pal.ConsentSettings();
// For the correct usage of the allowStorage property, See
// developers.google.com/ad-manager/pal/cast/reference/js/ConsentSettings#allowStorage.
consentSettings.allowStorage = true;
// You need a nonce loader to request your stream's nonceManager. The
// nonceManager provides your nonce. You should reuse the same nonce loader for
// the entire lifecycle of the receiver.
const nonceLoader = new goog.cast.pal.NonceLoader(consentSettings);
// You need a reference to the NonceManager to track when an ad is shown or
// clicked.
let nonceManager;
/**
* Sends a debug message to the CAF sender.
*
* @param {String} message - The message to send
*/
const log = (message) => {
// Use CastDebugLogger to log a message to the sender. See
// https://developers.google.com/cast/docs/debugging/cast_debug_logger.
}
/**
* Stores the nonce manager in the outer scoped variable and retrieves a nonce,
* so it can be used to build your ad request URL
*
* @param {NonceManager} loadedNonceManager - The loaded nonce manager
*/
const buildAdRequest = (loadedNonceManager) => {
nonceManager = loadedNonceManager;
const nonce = nonceManager.getNonce();
log('received nonce:' + nonce);
// TODO: Set this nonce as the value for the `givn` parameter of your ad
// request URL. For example:
// const adRequestURL = 'https://myadserver.com/ads?givn=' + nonce;
}
/**
* Configures a new nonce request, then requests a nonce.
*
* @param {LoadRequestData} loadRequestData - the load request object,
* which contains the MediaInformation object from the sender. See
* developers.google.com/cast/docs/reference/web_receiver/cast.framework.messages.LoadRequestData
* @return {(Promise<LoadRequestData>)} - A Promise to build an ad request.
*/
const handleLoadRequest = (loadRequestData) => {
// Clear any old nonceManager before loading new media.
nonceManager = null;
// See developers.google.com/ad-manager/pal/cast/reference/js/NonceRequest
// for details about each property. The NonceRequest parameters set here are
// example parameters. You should set your parameters based on your own app
// characteristics.
const nonceRequest = new goog.cast.pal.NonceRequest();
nonceRequest.adWillAutoPlay = true;
// A URL describing the video stream.
nonceRequest.descriptionUrl = 'https://example.com';
nonceRequest.iconsSupported = true;
nonceRequest.ppid = 'Sample PPID';
nonceRequest.sessionId = 'Sample SID';
nonceRequest.url = loadRequestData.media.contentUrl;
// The height of the player in physical pixels.
// For a fullscreen player on a 1080p screen, the video height would be 1080.
nonceRequest.videoHeight = window.devicePixelRatio * window.screen.height;
// The width of the player in physical pixels.
// For a fullscreen player on a 1080p screen, the video width would be 1920.
nonceRequest.videoWidth = window.devicePixelRatio * window.screen.width;
return nonceLoader.loadNonceManager(nonceRequest)
.then(buildAdRequest)
.catch((e) => {
log("Error: " + e.message);
});
};
// Set up the event handler for the LOAD event type.
playerManager.setMessageInterceptor(cast.framework.messages.MessageType.LOAD, handleLoadRequest);
castContext.start();
Khi thực hiện lệnh gọi VAST trực tiếp (DVC), hãy đặt số chỉ dùng một lần này làm giá trị trên tham số givn. Số chỉ dùng một lần an toàn cho URL; bạn không cần mã hoá số này thành URL.
Nếu đang yêu cầu và hiển thị quảng cáo của Google, bạn phải hiển thị biểu tượng và lớp phủ AdChoices. Để biết thông tin chi tiết về cách phân tích cú pháp phản hồi VAST và hiển thị biểu tượng, hãy xem phần Biểu tượng và lớp phủ Lựa chọn quảng cáo.
Theo dõi lượt tương tác với video
Ngoài việc tạo số chỉ dùng một lần, PAL SDK cần được thông báo về một số hoạt động tương tác với video. Để theo dõi các lượt tương tác với receiver Cast, hãy thêm mã sau vào receiver tuỳ chỉnh:
js/receiver.js
const castContext = cast.framework.CastReceiverContext.getInstance();
const playerManager = castContext.getPlayerManager();
const consentSettings = new goog.cast.pal.ConsentSettings();
// For the correct usage of the allowStorage property, See
// developers.google.com/ad-manager/pal/cast/reference/js/ConsentSettings#allowStorage.
consentSettings.allowStorage = true;
// You need a nonce loader to request your stream's nonceManager. The
// nonceManager provides your nonce. You should reuse the same nonce loader for
// the entire lifecycle of the receiver.
const nonceLoader = new goog.cast.pal.NonceLoader(consentSettings);
// You need a reference to the NonceManager for sending ad events.
let nonceManager;
// Track playback status.
let playbackDidStart = false;
...
// Register the start of playback.
playerManager.addEventListener(cast.framework.events.EventType.PLAYING, () => {
if (playbackDidStart) return;
playbackDidStart = true;
if (nonceManager) {
log('Registered playback start');
nonceManager.sendPlaybackStart();
} else {
log("Error: There is no nonce manager for this media.");
}
});
// Register any interactions with the player.
const interactionEvents = [
cast.framework.events.EventType.REQUEST_SEEK,
cast.framework.events.EventType.REQUEST_STOP,
cast.framework.events.EventType.REQUEST_PAUSE,
cast.framework.events.EventType.REQUEST_PLAY,
cast.framework.events.EventType.REQUEST_SKIP_AD,
cast.framework.events.EventType.REQUEST_PLAY_AGAIN,
cast.framework.events.EventType.REQUEST_PLAYBACK_RATE_CHANGE,
cast.framework.events.EventType.REQUEST_VOLUME_CHANGE,
cast.framework.events.EventType.REQUEST_USER_ACTION,
cast.framework.events.EventType.REQUEST_FOCUS_STATE,
];
playerManager.addEventListener(interactionEvents, (interactionEvent) => {
if (nonceManager) {
log('Registered interaction: ' + interactionEvent);
nonceManager.sendAdTouch(interactionEvent);
} else {
log("Error: There is no nonce manager for this media.");
}
});
// Register the end of playback.
playerManager.addEventListener(cast.framework.events.EventType.MEDIA_FINISHED, () => {
playbackDidStart = false;
if (nonceManager) {
log('Registered playback end');
nonceManager.sendPlaybackEnd();
} else {
log("Error: There is no nonce manager for this media.");
}
});
castContext.start();
(Không bắt buộc) Gửi tín hiệu Google Ad Manager thông qua máy chủ quảng cáo bên thứ ba
Định cấu hình yêu cầu của máy chủ quảng cáo bên thứ ba đối với Ad Manager. Sau khi bạn hoàn tất các bước sau, tham số số chỉ dùng một lần sẽ truyền từ PAL SDK, thông qua các máy chủ trung gian của bạn, rồi đến Google Ad Manager. Điều này giúp bạn kiếm tiền hiệu quả hơn thông qua Google Ad Manager.
Định cấu hình máy chủ quảng cáo của bên thứ ba để đưa số chỉ dùng một lần vào yêu cầu của máy chủ gửi đến Ad Manager. Sau đây là ví dụ về thẻ quảng cáo được định cấu hình bên trong máy chủ quảng cáo của bên thứ ba:
'https://pubads.serverside.net/gampad/ads?givn=%%custom_key_for_google_nonce%%&...'
Để biết thêm thông tin, hãy xem Hướng dẫn triển khai phía máy chủ của Google Ad Manager.
Ad Manager tìm kiếm givn= để xác định giá trị số chỉ dùng một lần. Máy chủ quảng cáo của bên thứ ba cần hỗ trợ một số macro của riêng mình, chẳng hạn như %%custom_key_for_google_nonce%% và thay thế macro đó bằng tham số truy vấn số chỉ dùng một lần mà bạn đã cung cấp ở bước trước. Bạn có thể xem thêm thông tin về cách thực hiện việc này trong tài liệu của máy chủ quảng cáo bên thứ ba.