Forum Moderators: martinibuster

Message Too Old, No Replies

GAM, how to use pubads().clear() when defineSlot is named with an arra

         

csdude55

3:26 pm on Jul 29, 2023 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I have 10 potential ad slots on a page (not all are displayed at once), so it's easier and faster to load them in a loop. The loop looks like this (simplified for the post, of course):

var slotArr = [];

for (var unit in ['ad_0, 'ad_1', ... 'ad_9'])
slotArr[unit] = googletag
.defineSlot(
'/123456/' + slot[unit],
size[unit],
'ad_' + unit)
.addService(googletag.pubads());


Later in the code, I have a condition where I might want to clear one of those slots (if the ad doesn't load then I clear it and display again with something else). That code looks like this:

for (var unit of [5, 2, 3])
googletag.cmd.push(function() {
googletag.pubads().clear([slotArr[unit]]);
...
});


But this throws an error in the console:

[GPT] Invalid arguments: PubAdsService.clear([]).

The error looks like slotArr[unit] is showing up blank!

I've tried every alternative I can think of, but can't get anything to work.

Any suggestions?