jQuery 如何检查 Google 发布商标签广告位是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15864980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check Google publisher tag ad slot is empty or not?
提问by user2254912
I am using Google publisher tags to fetch the ads.
我正在使用 Google 发布商标签来获取广告。
How to check whether I am getting an ad or any empty ad for an specific ad slot. I am using the below code.
如何检查我是否收到特定广告位的广告或任何空广告。我正在使用下面的代码。
googletag.defineSlot("/1234/travel", [[300,250],[300x600]], "div-gpt-ad-123456789-0"))
<div id="div-gpt-ad-123456789-0" style="width: 728px; height: 90px">
<script type="text/javascript">
googletag.cmd.push(function() {
googletag.display("div-gpt-ad-123456789-0");
});
</script>
</div>
How to check this div("div-gpt-ad-123456789-0") contains an ad or not?
如何检查此 div("div-gpt-ad-123456789-0") 是否包含广告?
回答by Federico J. álvarez Valero
You could do something like this:
你可以这样做:
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if (event.slot.getSlotElementId() == "div-gpt-ad-123456789-0") {
var containsAd = !event.isEmpty;
}
});
回答by dougwig
The GPT API provides a "SlotRenderEndedEvent" which fires when the ad is rendered ( or if nothing is returned )
GPT API 提供了一个“SlotRenderEndedEvent”,它会在广告呈现时触发(或者如果没有返回任何内容)
https://developers.google.com/doubleclick-gpt/reference
https://developers.google.com/doubleclick-gpt/reference
You add the handler and check for isEmpty to determine if an ad has been served.
您添加处理程序并检查 isEmpty 以确定是否已提供广告。
Also if you've enabled CollapsEmptyDivs for the ad slot, the ad div will be set to display:none by GPT so that is also a clue that nothing was returned.
此外,如果您为广告位启用了 CollapsEmptyDivs,则广告 div 将被 GPT 设置为 display:none,因此这也是没有返回任何内容的线索。
回答by Prasanna Jathan
You can use an on-screen debugging tool called the Google Publisher Console to troubleshoot delivery problems.Use "?googfc" as querystring. Reference: https://support.google.com/dfp_premium/answer/2462712?hl=en
您可以使用名为 Google Publisher Console 的屏幕调试工具来解决投放问题。使用“?googfc”作为查询字符串。参考:https: //support.google.com/dfp_premium/answer/2462712?hl=en
回答by Piyush Sahu
googletag.cmd.push(function () {
googletag.pubads().addEventListener('slotRenderEnded', function (event) {
if (event.isEmpty) {
var id = event.slot.getSlotElementId();
var x = document.getElementById(id);
if (x.parentElement.classList.contains("ad-slot")) {
x.parentElement.style.display = "none";
}
}
});
});
回答by Matt Cooper
Have you tried inspecting the div with firebug or chrome web inspector? There should be an iframe inside of the div if it is loading correctly and inside of the iframe there will be some html with the ad creative.
您是否尝试过使用 firebug 或 chrome web 检查器检查 div?如果 div 加载正确,则 div 内应该有一个 iframe,并且在 iframe 内会有一些带有广告创意的 html。
You can debug DFP pretty easily with the DFP consoleto test if your page is tagged correctly and that the ads are being delivered.
您可以使用DFP 控制台轻松调试 DFP,以测试您的网页是否正确标记以及广告是否正在投放。