You can use the refresh functionality of an asynchronous Google Publisher Tag to dynamically reload ads without having to refresh the entire contents of your page. Refreshing ads or Refreshing Ad slot in DFP as part of an ajax interface can be a pain, many ad providers do not support this feature out of the box. DFP does however which is yet another awesome feature of the DFP platform.

Refreshing specific ads

By default the call to refresh will target all the ad slots on a page, it is also possible to specify exactly which slots you want to be refreshed.

In order to do this you simply pass an array of ad slots into the refresh function, you will of course need a reference to each ad slot in order to do this, each call to ‘googletag.defineSlot’ returns a reference to the ad that was just instantiated, if you save all of these return values into variables you will be able to construct a list of ad slots that you want to refresh.

Here is a simple example of this:

<script type='text/javascript'>
var slots = {};
googletag.cmd.push(function() {
    slots['sky'] = googletag.defineSlot('{account_id}/{path}', [120, 600], 'ad-sky').addService(googletag.pubads());
    slots['mpu'] = googletag.defineSlot('{account_id}/{path}', [300, 250], 'ad-mpu').addService(googletag.pubads());
    slots['leader'] = googletag.defineSlot('{account_id}/{path}', [728, 90], 'ad-leader').addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
});

refreshAds = function(refresh_slots) {
    if(typeof(refresh_slots) == "undefined") {
        googletag.pubads().refresh();
    } else {
        googletag.pubads().refresh(refresh_slots);
    }
};

</script>
<div>
    <button onclick="javascript:refreshAds(); return false;">Refresh All</button>
    <button onclick="javascript:refreshAds([slots['sky']]); return false;">Refresh Sky</button>
    <button onclick="javascript:refreshAds([slots['mpu']]); return false;">Refresh MPU</button>
    <button onclick="javascript:refreshAds([slots['leader']]); return false;">Refresh Leader</button>
    <button onclick="javascript:refreshAds([slots['sky'], slots['mpu']]); return false;">Refresh Sky + MPU</button>
</div>
<div id='ad-sky' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-sky'); });
    </script>
</div>
<div id='ad-mpu' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-mpu'); });
    </script>
</div>
<div id='ad-leader' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-leader'); });
    </script>
</div>

Note that each ad slot is saved into an object (called slots) for later retrieval.

Please Note that Google allows ad refresh via the Ad Exchange UI. This means publishers can run refreshing ad inventory (display, mobile web, and in-app) to compete on AdX. The only requirement is to declare this via the UI.

©2024 AdTagMacros - Everything About Advertising

We improve our products and advertising by using Microsoft Clarity to see how you use our website. By using our site, you agree that we and Microsoft can collect and use this data. Our privacy statement has more details.

Log in with your credentials

Forgot your details?