Skip to main content

How to refresh Google DFP Specific ad slot

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());

refreshAds = function(refresh_slots) {
    if(typeof(refresh_slots) == "undefined") {
    } else {

    <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 id='ad-sky' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-sky'); });
<div id='ad-mpu' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-mpu'); });
<div id='ad-leader' class="ad">
    <script type='text/javascript'>
    googletag.cmd.push(function() { googletag.display('ad-leader'); });

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.

Facebook Comments


My name's Sachin, and I am Blogger and tech geek, currently working in Digital Advertisement Domain having work experience of more than 3 years. Ad-Tag Macros is the professional association dedicated exclusively to online advertising operations and technology. We focus primarily on topics such as ad serving and related technologies, yield management, policies, procedures, and standards. Experience Platform DFP Small Business Google Analytics Native Ads Mgid WP ComScore Blogspot AdSense Skills HTML/CSS Javascript Yield optimization Trafficking Mobile web ads Display, video ads Troubleshooting creative issue SEO and Blog revenue Optimization For Any Queries Please Contact Me