Forum Moderators: martinibuster

Message Too Old, No Replies

Refreshing GAM with Adsense backup

         

csdude55

5:56 am on Sep 10, 2023 (gmt 0)

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



I use a somewhat complicated system to first show ads from Google Ad Manager, and then if there's nothing there then it loads Adsense. If there's still nothing there, I show a direct sold ad.

It looks like this:

googletag.cmd.push(function() {
// do all of the stuff to show GAM ads
});

var interval = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(interval);

// do all the stuff to show Adsense, or direct sale
}
}, 500);


Now I'm thinking about refreshing those GAM ads to see if I can increase revenue any. But the issue there is that I'm not sure if the ad unit is filled with GAM, Adsense, or the direct sale! So what I really need to do is clear the ad unit no matter what's there, then start it over.

Is it acceptable to wrap the whole thing in a setInterval? Nesting a setInterval inside of another setInterval looks hacky as all get out, but it doesn't throw an error.

var x = 0;

setInterval(() => {
if (x)
// this clears the GAM slot, but how would I clear it if it's filled with Adsense?
googletag.cmd.push(function() {
googletag.pubads().clear();
});

x = 1;

googletag.cmd.push(function() {
// do all of the stuff to show GAM ads
});

var interval = setInterval(() => {
if (document.readyState === 'complete') {
clearInterval(interval);

// do all the stuff to show Adsense, or direct sale
}
}, 500);
}, 45000);