问题
We have a series of DFP ads on a page that get refreshed automatically using the javascript refresh() call each time a user clicks each thumbnail icon on a gallery viewer:
googletag.pubads().refresh();
The problem is that if a user clicks through the gallery quickly, the ads keep refreshing for every click so that eventually blank ads can occur as the system is unable to keep up with the user clicks.
Hence I'd like to only call refresh only if the ad has completely finished loading.
Is there any way of detecting the loaded state of an ad within a slot?
Our ads are defined using:
slot_header_lge = googletag.defineSlot('/XXX/Header-Home-Large', [945, 230], 'div-gpt-ad-Header-Large').addService(googletag.pubads());
googletag.pubads().enableAsyncRendering();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
回答1:
I made a jquery plugin that wraps DFP and kind of came up with a solution to this problem a while back... there is an internal function on the ad unit called renderEnded() that you can hook into to check when the ad unit has been rendered.
In my experience this just means that the HTML code has been put into the page... and not necessarily that the ad has completely finished loading. You could combine some timeOuts with this to get a fairly robust system going though.
For example with my plugin you can try the following, it shouldn't allow ads to be refreshed until 5 seconds after the last ad on the page has rendered:
<!DOCTYPE html>
<html>
<head>
<title>TEST</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://coop182.github.io/jquery.dfp.js/jquery.dfp.js"></script>
</head>
<body>
Test DFP Ads.
<div class="adunit" id="buyingcars_ATF_728x90" data-dimensions="728x90"></div>
<script>
var adsLoaded = true;
function checkLoading() {
console.log('Checking...');
if (adsLoaded) {
loadAds();
}
}
function loadAds() {
console.log('Loading...');
adsLoaded = false;
$.dfp({
dfpID: '3853655',
afterAllAdsLoaded: function() {
setTimeout(function(){adsLoaded = true;}, 5000);
}
});
}
checkLoading();
</script>
<a href="#" onclick="checkLoading();">Refresh</a>
</body>
</html>
来源:https://stackoverflow.com/questions/18972912/how-to-detect-loading-state-of-a-google-doubleclick-for-publishers-dfp-ad