CodingGoogle Ad Manager

How to auto refresh ad slots?

There are multiple ways to refresh ad slots using GPT library function. But none of the documents or examples provided for auto refreshing ad slots based on Time event.

How to do?

Declare variable name globally before ‘cmd.push’ function and assign to the ad slot declaration. Example see below

var slot1;

Assign variable to the ad slot.

Add that variable before the ad slot in the header tag.

slot1 = googletag.defineSlot('/200894144/Responsive', [[300, 250], [300, 600]], 'div-gpt-ad-1620832289302-0').addService(googletag.pubads());

Auto Refresh Trigger

The final step is to trigger auto refresh using ‘SetInterval’ function on the Body code. This will trigger the refresh code on specified interval.

setInterval(function(){googletag.pubads().refresh([slot1]);}, 3000);

The variable which declared on the first step to be added within the refresh function on the above code. “3000” represent three second time interval . Time cab be adjusted based on your requirement. Best practices is minimum 30 seconds.

Complete Code Configuration

Here is the full code representation for the above discussions. Added key values for the test purposed. Ignore that in your code and replace as per your requirements.

<script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
<script>
  window.googletag = window.googletag || {cmd: []};
   var slot1;
  googletag.cmd.push(function() {
   slot1 = googletag.defineSlot('/200894144/Responsive', [[300, 250], [300, 600]], 'div-gpt-ad-1620832289302-0').setTargeting("Test_Mode", "ON").addService(googletag.pubads());
    
        googletag.enableServices();
  });
</script>

<div id='div-gpt-ad-1620832289302-0'>
<script>
googletag.cmd.push(function() { googletag.display('div-gpt-ad-1620832289302-0');
setInterval(function(){googletag.pubads().refresh([slot1]);}, 3000);
});
</script>
</div>

Live Working Example.

Hope this article helps!!!. Please share your comments.

Thank You!!!!

*The information contained in this post is for general information purposes only. The information is provided by How to auto refresh ad slots? and while we endeavor to keep the information up to date and correct, we make no representations or warranties of any kind, express or implied, about the completeness, accuracy, reliability, suitability or availability with respect to the website or the information, products, services, or related graphics contained on the post for any purpose.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enable Notifications OK No thanks