How to get ads to refresh using googletag.pubads().refresh()?

hitfactory picture hitfactory · Aug 29, 2012 · Viewed 18.8k times · Source

I have an ad and a link in a page like so:

<!DOCTYPE html>
<head profile="http://www.w3.org/1999/xhtml/vocab">
<title>DPT - Asynchronous + Single Rest Architecture</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
  <script type='text/javascript'>
    var googletag = googletag || {};
    googletag.cmd = googletag.cmd || [];
    (function() {
    var gads = document.createElement('script');
    gads.async = true;
    gads.type = 'text/javascript';
    var useSSL = 'https:' == document.location.protocol;
    gads.src = (useSSL ? 'https:' : 'http:') +
    '//www.googletagservices.com/tag/js/gpt.js';
    var node = document.getElementsByTagName('script')[0];
    node.parentNode.insertBefore(gads, node);
    })();
  </script>
  <script type='text/javascript'>
    googletag.cmd.push(function() {
    googletag.pubads().enableAsyncRendering();
    googletag.defineSlot('/1001256/Home_Top_Leaderboard_728x90', [728, 90], 'div-gpt-ad-1342320102476-72').addService(googletag.pubads());
    googletag.pubads().enableSingleRequest();
    googletag.enableServices();
    });
</script>
</head>
<body>
    <div id="div-gpt-ad-1342320102476-72" style="width:728px; height:90px;">
      <script type="text/javascript">
        googletag.cmd.push(function() { googletag.display('div-gpt-ad-1342320102476-72'); });
      </script>
    </div>
    <a id="refresh" href="#">Refresh ad</a>
    <script type='text/javascript'>
    (function ($) {
        $('#refresh').click(function() {
         googletag.cmd.push(googletag.pubads().refresh());
        return false;
        });
    })(jQuery);
    </script>
</body>
</html> 

The first time I click the link, the ad refreshes. All other times nothing happens. Even just calling the refresh() method in Firebug etc does nothing after the first time.

What's wrong with the above?

Answer

MarkD picture MarkD · May 16, 2014

According to https://support.google.com/dfp_premium/answer/4578089 the refresh function takes a parameter of an array of adSlots:

googletag.pubads().refresh([gptAdSlots[0], gptAdSlots[1]]);

Does that change the behaviour for you?

Failing that, I would recommend enabling the Google Publisher Console by adding the google_console or googfc parameter to your querystring (see https://support.google.com/dfp_sb/answer/181070?hl=en for details), and checking for errors