Google Publisher Tag注册到活动 [英] Google Publisher Tag registering to Events

查看:156
本文介绍了Google Publisher Tag注册到活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了Google发布商代码参考,并决定可以在广告位中添加一些事件.如果我只定义了一个插槽,则效果很好. 如果我添加了更多插槽,则事件在控制台上重复出现的次数会不断重复.

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>

我的console.log将是3x插槽已呈现:".如果我从defineSlot部分中删除了两个 .addService ,则它只会console.logs一次,但不会呈现广告.

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.

谢谢!

推荐答案

Nastasja,

输出重复三遍的原因是您注册的事件处理程序会监听'slotRenderEnded'事件,该事件在插槽渲染每次 时发生.由于您的示例有3个广告位,因此该事件将触发3次.您可以通过检查传入的事件对象来查看哪个插槽触发了回调.

The reason the output repeats three times is the event handler that you registered listens for the 'slotRenderEnded' event, which happens every time a slot renders. Since your example has three slots, the event will fire three times. You can see which slot triggers the callback by inspecting the event object that is passed in.

 googletag.pubads().addEventListener('slotRenderEnded', function(event) {
        console.log('Slot has been rendered:');
        console.log(event); //inspect event
        console.log(event.slot); //inspect slot
 });

即使该事件发生了前三次,您也可以通过比较事件中的插槽来添加基于插槽的逻辑,如下所示:

Even though the the event first three times, you can add slot based logic by comparing the slots in the event like this:

 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');
    }
 });

您可以在 GPT文档

这篇关于Google Publisher Tag注册到活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆