I looked at the Google Publisher Tag reference and decided that I could add some events to my ad slots. It works great if I have defined only one slot. If I add more slots, the events keep repeating on my console number of times I have slots.
So, if I do something like this:
<script type='text/javascript'>
googletag.cmd.push(function() {
var slot1 = googletag.defineSlot('/123456/leadeboard', [[728, 90]], 'div-gpt-ad-123456789-0').addService(googletag.pubads());
var slot2 = googletag.defineSlot('/123456/leadeboard', [[728, 90]], 'div-gpt-ad-123456789-0').addService(googletag.pubads());
var slot3 = googletag.defineSlot('/123456/leadeboard', [[728, 90]], 'div-gpt-ad-123456789-0').addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
console.log('Slot has been rendered:');
});
googletag.enableServices();
});
</script>
My console.log would be 3x "Slot has been rendered:". If I remove two of the .addService from the defineSlot part, it only console.logs it once, but then the ad is not rendered.
Is there a way how to remove extra logging without breaking everything else? It can get extremely messy after a while.
Thanks!
答案 0 :(得分:6)
Nastasja,
输出重复三次的原因是您注册的事件处理程序侦听了&#39; slotRenderEnded&#39;事件,每次一个插槽渲染时发生。由于您的示例有三个插槽,因此该事件将触发三次。您可以通过检查传入的事件对象来查看哪个槽触发回调。
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
console.log('Slot has been rendered:');
console.log(event); //inspect event
console.log(event.slot); //inspect slot
});
即使事件前三次,您也可以通过比较事件中的插槽来添加基于插槽的逻辑:
var slot1 = googletag.defineSlot('/123456/leadeboard', [[728, 90]], 'div-gpt-ad-123456789-0').addService(googletag.pubads());
googletag.pubads().addEventListener('slotRenderEnded', function(event) {
if(slot1 === event.slot){
console.log('slot1 has been rendered');
}
});
中阅读更多内容