tải kịch bản trong một phần của trang của bạn:
// set global variable if not already set
var googletag = googletag || {};
googletag.cmd = googletag.cmd || [];
// load asynchronously the GPT JavaScript library used by DFP,
// using SSL/HTTPS if necessary
(function() {
var gads = document.createElement('script');
gads.async = true;
gads.type = 'text/javascript';
var useSSL = 'https:' === document.location.protocol;
gads.src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
var node =document.getElementsByTagName('script')[0];
node.parentNode.insertBefore(gads, node);
})();
khởi google xuất bản tag với kịch bản sau đây, thích hợp hơn cũng như trong của trang của bạn:
// can be moved as well in the body
// if using async mode, wrap all the javascript into googletag.cmd.push!
googletag.cmd.push(function() {
// set page-level attributes for ad slots that serve AdSense
googletag.pubads().set("adsense_background_color", "FFFFFF");
googletag.pubads().setTargeting("topic","basketball");
// enables Single Request Architecture (SRA)
googletag.pubads().enableSingleRequest();
// Disable initial load, we will use refresh() to fetch ads.
// Calling this function means that display() calls just
// register the slot as ready, but do not fetch ads for it.
googletag.pubads().disableInitialLoad();
// Collapses empty div elements on a page when there is no ad content to display.
googletag.pubads().collapseEmptyDivs();
// Enables all GPT services that have been defined for ad slots on the page.
googletag.enableServices();
});
Đăng ký các khe riêng lẻ (có thể được tạo ra bằng vòng lặp foreach) và hiển thị chúng. Trình nghe sự kiện có thể được đăng ký cũng trên mỗi vị trí. Đây là phần quan trọng: đảm bảo rằng bạn làm mới chúng lại với nhau để tránh kết thúc với cùng một quảng cáo trên cả hai vị trí (nếu quảng cáo được gán cho cả hai vị trí) => googletag.pubads(). Refresh ([slot1, slot2]]);
// this code can be moved externally to improve performance
googletag.cmd.push(function() {
// define slot1
slot1 = googletag.defineSlot(
"/1234/travel/asia/food",
[728, 90],
"banner1"
)
.addService(googletag.pubads())
.setTargeting(
"interests",
["sports", "music", "movies"]
);
// prerender the slot but don't display it because of disableInitialLoad()
googletag.display("banner1");
// define slot2
slot2 = googletag.defineSlot(
"/1234/travel/asia/food",
[[468, 60], [728, 90], [300, 250]],
"banner2"
)
.addService(googletag.pubads())
.setTargeting("gender", "male")
.setTargeting("age", "20-30");
// prerender the slot but don't display it because of disableInitialLoad()
googletag.display("banner2");
// add event to sign the slot as redered or not
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if (event.slot === slot1 || event.slot === slot2) {
// do something related to the slot
}
});
// refresh all container ads and show them
// very important to call refresh with an array to avoid
// multiple callback to the registered event
googletag.pubads().refresh([slot1, slot2]);
});
<div id="banner1" style="width:300px; height:250px;"></div>
<div id="banner2" style="width:300px; height:250px;"></div>
Sau khi quảng cáo đã được trả lại, gọi lại được kích hoạt.
Để biết thêm thông tin có một cái nhìn vào tập tin này: https://github.com/davidecantoni/googletag
may mắn với điều này? – alexp
Bạn đã kết thúc hướng nào với điều này? Giả sử vấn đề là bạn đang cố gắng ngăn chặn flash của không gian quảng cáo trống do collapseEmptyDivs gây ra? –
Tôi đã tìm thấy giải pháp cho vấn đề tôi đã nêu trước đây: http://stackoverflow.com/a/17931853/105061 –