Google DFP slotRenderEnded函数在pubads().refresh([currentlot])之

Google DFP slotRenderEnded function working wrongly after pubads().refresh([currentSlot])

本文关键字:currentlot refresh DFP 函数 pubads Google slotRenderEnded      更新时间:2023-09-26

我使用googletag.pubads().refresh([currentAdSlot])在无限循环中滚动鼠标窗口向下,我想在刷新功能后收听'slotRenderEnded'事件。但每次当我调用刷新函数,'slotRenderEvent'触发累积。例如,在第一次调用ResendAd函数时,监听事件只工作一次。在秒调用时,监听事件工作两次并不断增加此计数。如何修复这个bug?我不明白怎么了?这是谷歌dfp api的错误还是我的代码?

function ResendAd(AdSlot,callback) {
  googletag.pubads().clearTargeting();
  googletag.pubads().updateCorrelator();
  googletag.pubads().refresh([AdSlot]);
  googletag.pubads().addEventListener('slotRenderEnded', function (event) {
    console.warn('This function scope triggered multiple times.');
    if (event.slot === AdSlot) {
      if (!event.isEmpty) {
        if (callback && typeof callback === "function") {
          callback(event);
        }
      }
    }
  });
}

这是你代码中的一个bug。每次调用ResendAd时,都要添加一个事件侦听器(slotRenderEnded)。

应该在只调用一次的函数中添加事件侦听器。我不能告诉你具体在哪里,因为我无法访问你的代码