Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google dfp pubads().refresh() not working as expected

Tags:

google-dfp

I'm using some of the new features in Google DFP and can't get a single add to refresh on it's own. I can refresh all ads on the page but I can't get just one to refresh by itself.

I've been trying the following code to get one add to refresh without luck:

   var slot1 = googletag.display('/6900315/myad2', [300, 250], 'div-2');
        googletag.pubads().refresh([slot1]);

Any help would be appreciated!

Full source:

<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("/69003153/myad1", [728, 90], "div-1").addService         (googletag.pubads());
    googletag.defineSlot("/69003153/myad2", [300, 250], "div-2").addService    (googletag.pubads());
    googletag.enableServices();
});
</script>
<script type='text/javascript'>
function changeAd() {
    googletag.cmd.push(function () {
        var slot1 = googletag.display('/6900315/myad2', [300, 250], 'div-2');
        googletag.pubads().refresh([slot1]);
        //googletag.pubads().refresh();


    });
}
</script>
</head>
<body>
   <form id="form1" runat="server">


    <div id='div-1' style='width:728px; height:90px;'>
<script type='text/javascript'>
     googletag.cmd.push(function () { googletag.display('div-1'); });
</script>
</div>


 <!-- BoxAdd_Details -->
<div id='div-2' style='width:300px; height:250px;'>
   <script type='text/javascript'>
        googletag.cmd.push(function () { googletag.display('div-2); });
</script>
</div>

<input type="button" name="test" title="test" onclick="javascript:window.changeAd();"     value="Reload Ad" />
like image 690
sooty Avatar asked Nov 28 '11 15:11

sooty


1 Answers

I'm also having the same problem. I haven't got my ads to refresh yet, but I should suggest that you don't need to redeclare your ad slot in changeAd().

Instead of:

googletag.cmd.push(function () {
    googletag.pubads().enableAsyncRendering();
    googletag.defineSlot("/69003153/myad1", [728, 90], "div-1").addService         (googletag.pubads());
    googletag.defineSlot("/69003153/myad2", [300, 250], "div-2").addService    (googletag.pubads());
    googletag.enableServices();
});

Try:

googletag.cmd.push(function () {
    googletag.pubads().enableAsyncRendering();
    slot1 = googletag.defineSlot("/69003153/myad1", [728, 90], "div-1").addService         (googletag.pubads());
    slot2 = googletag.defineSlot("/69003153/myad2", [300, 250], "div-2").addService    (googletag.pubads());
    googletag.enableServices();
});

Declaring them without 'var' preceding them makes the slots global in scope. In your changeAd() function, simply call

googletag.pubads().refresh([slot1]);

I'm not even sure if you need to place that inside googletag.cmd.push()

When I execute it this way, my ads still dont refresh, but running console.log(slot1) shows that the ad slot is alive and kicking...

Can anyone else help further?

like image 172
dinie Avatar answered Nov 14 '22 06:11

dinie