質問

I'm defining all my adSlots in the header of my page and in the JavaScript I want to refresh "some" of those adSlots based on their location on the page.

Here's what I have at the moment:

googletag.cmd.push(function() {
  slot1 = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1')
    .addService(googletag.pubads());
  slot2 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2')
    .addService(googletag.pubads());
  slot3 = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3')
    .addService(googletag.pubads());
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();

I'm then putting the name of that defined slot into a data-gpt-slot attribute of each div, as such: <div attr="slot1" id="gpt-divId1">.

In a separate JavaScript, I'm trying to run through my DOM, getting a list of all the data-gpt-slot values within a certain area and adding them to the googletag.pubads().refresh();

If I manually call googletag.pubads().refresh([slot1,slot2]), those ads will refresh, but if I try to build that string through jQuery, it's not playing.

  var gptdivs = [];
  $('.myContainer .gpt-holder').each(function(){
    gptdivs.push($(this).attr('data-gpt-slot'));
  });
  googletag.pubads().refresh([gptdivs]);

I suspect I'm missing something about the properties of $(this).attr('data-gpt-slot') and slot1, slot2, etc?

役に立ちましたか?

解決

The problem is that you are trying to use the refresh method incorrectly. It takes the actual ad unit instance as an argument not just the name of it as a string.

Try this instead:

var adunits = {};

adunits['slot1'] = googletag.defineSlot('/1234/example', [[728, 90], 'gpt-divId1').addService(googletag.pubads());
adunits['slot2'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId2').addService(googletag.pubads());
adunits['slot3'] = googletag.defineSlot('/1234/example', [[300, 250], 'gpt-divId3').addService(googletag.pubads());

And then your code to refresh the adunits becomes:

var gptdivs = [];
$('.myContainer .gpt-holder').each(function(){
    gptdivs.push(adunits[$(this).attr('data-gpt-slot')]);
});
googletag.pubads().refresh(gptdivs);

他のヒント

I think you have a deep array here, causing your issue

refresh([gptdivs])

Just try

refresh(gptdivs)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top