DFP广告管理系统在视图中的X秒后刷新广告

时间:2018-06-04 09:24:14

标签: google-dfp

我也希望这样做但是使用GPT APi(我对这些编码很新,所以不知道做什么,所以寻找有效的帮助)

我在GPT ApI文档中找到了这个:

  1. 展示可见侦听器,插槽特定逻辑。 当印象被视为可见时,将调用监听器。 此事件也在服务级别运行,但如上所述,您可以进行过滤 使用此模式仅响应某个广告位

    googletag.pubads()。addEventListener(' impressionViewable',function(event){ if(event.slot == targetSlot){ // Slot特定逻辑。 } });

  2. 已经有了这个,这是在X秒后刷新广告

    googletag.cmd.push(function() {
      var mappingleaderslot = googletag.sizeMapping().
      addSize([1024, 769], [[970,250],[970,90],[728,90],[468,60],[320,50],[234,60]]).
      addSize([768, 500], [[728,90],[468,60],[320,50],[234,60]]).
      addSize([1, 1], [[320,50],[234,60]]).
      build();
      var mappingbigboxslot = googletag.sizeMapping().
      addSize([1024, 769], [[300,600],[300,250],[160,600],[120,600],[250,250]]).
      addSize([768, 500], [[300,250],[250,250]]).
      addSize([1, 1], [[300,250],[250,250]]).
      build();
      topSlot = googletag.defineSlot('/1001824/prebid_test2', [[970, 250],[970, 90],[728, 90],[468, 60],[320, 50],[234, 60]], 'topSlot').defineSizeMapping(mappingleaderslot).setTargeting("test", "refresh").addService(googletag.pubads());
      middlerightSlot = googletag.defineSlot('/1001824/prebid_test3', [[300, 600],[300, 250],[160, 600],[120, 600],[250, 250]], 'middlerightSlot').defineSizeMapping(mappingbigboxslot).setTargeting("test", "refresh").addService(googletag.pubads());
      bottomrightSlot = googletag.defineSlot('/1001824/prebid_test1', [[300, 600],[300, 250],[160, 600],[120, 600],[250, 250]], 'bottomrightSlot').defineSizeMapping(mappingbigboxslot).setTargeting("test", "refresh").addService(googletag.pubads());
      bottomleftSlot = googletag.defineSlot('/1001824/prebid_test4', [[300, 600],[300, 250],[160, 600],[120, 600],[250,250]], 'bottomleftSlot').defineSizeMapping(mappingbigboxslot).setTargeting("test", "refresh").addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.pubads().collapseEmptyDivs(true, true);
    googletag.pubads().setCentering(true);
    googletag.enableServices();
    googletag.display("topSlot");
    googletag.display("middlerightSlot");
    googletag.display("bottomrightSlot");
    googletag.display("bottomleftSlot");
    setInterval(function(){googletag.pubads().refresh([topSlot]);}, 120000);
    setInterval(function(){googletag.pubads().refresh([middlerightSlot]);}, 120000);
    setInterval(function(){googletag.pubads().refresh([bottomrightSlot]);}, 120000);
    setInterval(function(){googletag.pubads().refresh([bottomleftSlot]);}, 120000);
    });
    
    function refreshSlot(slot) {
        pbjs.que.push(function() {
            pbjs.requestBids({
                timeout: PREBID_TIMEOUT,
                adUnitCodes: [slot.getSlotElementId()],
                bidsBackHandler: function() {
                    pbjs.setTargetingForGPTAsync([slot.getSlotElementId()]);
                    googletag.pubads().refresh([slot]);
                }
            });
        });
    }
    function refreshtopSlot() {
        refreshSlot(topSlot);
    }
    function refreshmiddlerightSlot() {
        refreshSlot(middlerightSlot);
    }
    function refreshbottomrightSlot() {
        refreshSlot(bottomrightSlot);
    }  
    function refreshbottomleftSlot() {
        refreshSlot(bottomleftSlot);
    }
    

    但不知道我如何将两者结合起来,以便广告一旦刷新就会刷新"在视图中#34;对于X秒,任何指针/任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

你可以看一下这个要点,它有一个完整的code example

这是Javascript示例

var size = [
  //[300, 600],
  //[300, 250],
  //[160, 600],
  //[120, 600],
  [250, 250]
];
var PREBID_TIMEOUT = 1000;

googletag.cmd.push(function () {
  (function (googletag, pbjs, config) {
    var sizeMappings = {};
    var slots = {};
    function refreshSlot(slot) {
      pbjs.que.push(function() {
        pbjs.requestBids({
          timeout: PREBID_TIMEOUT,
          adUnitCodes: [slot.getSlotElementId()],
          bidsBackHandler: function() {
            pbjs.setTargetingForGPTAsync([slot.getSlotElementId()]);
            googletag.pubads().refresh([slot]);
          }
        });
      });
    }
    Object.keys(config.sizeMappings).forEach(function (key) {
      var sizeMappingBuilder = googletag.sizeMapping();
      config.sizeMappings[key].forEach(function (mapping) {
        sizeMappingBuilder.addSize(mapping[0], mapping[1]);
      });
      var sizeMapping = sizeMappingBuilder.build();
      sizeMappings[key] = sizeMapping;
      console.log('created sizemapping ', key, ' ', sizeMappings[key]);
    });

    //googletag.pubads().enableSingleRequest();
    googletag.pubads().collapseEmptyDivs(true, true);
    googletag.pubads().setCentering(true);
    googletag.pubads().disableInitialLoad();
    googletag.enableServices();
    googletag.pubads().addEventListener('impressionViewable', function (event) {
      var elementId = event.slot.getSlotElementId();
      var slotConfig = slots[elementId];
      if (slotConfig) {
        var handle = setTimeout(function () {
          googletag.cmd.push(function () {
            refreshSlot(event.slot);
          });
        }, config.definitons[elementId].timeout);
        console.log('handle for time ', handle, ' elementId ', elementId, ' duration ', config.definitons[elementId].timeout);
      }
    });
    Object.keys(config.definitons).forEach(function (key) {
      var def = config.definitons[key];
      var slot = googletag.defineSlot(def.adUnitPath, def.size, key);
      slot.setTargeting('test', 'refresh');
      slot.defineSizeMapping(sizeMappings[def.sizeMapping]);
      slot.addService(googletag.pubads());
      googletag.display(key);
      slots[key] = { slot: slot };
    });
    googletag.pubads().refresh();
  })(window.googletag, window.pbjs, {
    definitons: {
      topSlot: {
        adUnitPath: '/1001824/prebid_test2',
        size: size,
        sizeMapping: 'mappingleaderslot',
        timeout: 5000,
      },
      middlerightSlot: {
        adUnitPath: '/1001824/prebid_test3',
        size: size,
        sizeMapping: 'mappingbigboxslot',
        timeout: 3000,
      },
      bottomrightSlot: {
        adUnitPath: '/1001824/prebid_test1',
        size: size,
        sizeMapping: 'mappingbigboxslot',
        timeout: 3000,
      },
      bottomleftSlot: {
        adUnitPath: '/1001824/prebid_test4',
        size: size,
        sizeMapping: 'mappingbigboxslot',
        timeout: 3000,
      },
    },
    sizeMappings: {
      mappingleaderslot: [
        [
          [1024, 769],
          [
            [970, 250],
            [970, 90],
            [728, 90],
            [468, 60],
            [320, 50],
            [234, 60]
          ]
        ],
        [
          [768, 500],
          [
            [728, 90],
            [468, 60],
            [320, 50],
            [234, 60]
          ]
        ],
        [
          [1, 1],
          [
            [320, 50],
            [234, 60]
          ]
        ],
      ],
      mappingbigboxslot: [
        [
          [1024, 769],
          [
            [300, 600],
            [300, 250],
            [160, 600],
            [120, 600],
            [250, 250]
          ]
        ],
        [
          [768, 500],
          [
            [300, 250],
            [250, 250]
          ]
        ],
        [
          [1, 1],
          [
            [300, 250],
            [250, 250]
          ]
        ],
      ]
    }
  });
});

答案 1 :(得分:0)

我认为我发现了另一个问题,如果预付广告首先出现,那么它会将预付费的热门广告/页面出价推送到"下一个"预付出价,它刷新相同的出价,因此没有替代品" rev"要有。我尝试添加这个,但似乎没有工作

googletag.pubads().addEventListener('impressionViewable', function 
(event) {
          var elementId = event.slot.getSlotElementId();
          var slotConfig = slots[elementId];
          if (slotConfig) {
            var handle = setTimeout(function () {
              googletag.cmd.push(function () {
                  function refreshBid() {
                      pbjs.que.push(function() {
                          pbjs.requestBids({
                              timeout: PREBID_TIMEOUT,
                              bidsBackHandler: function() {
                                  googletag.pubads().refresh([slot1]);