title: How to reload DFP ad on ajax success?
body: <p>BACKGROUND STORY: 
  Everytime a new search results is done, server will give whole page HTML back. This includes the ad (that has new targetting, ID, etc). </p>
  
  <p>I need to re-load one of the ads from DFP, that is located inside search results wrapper (content is updated via AJAX). This is my current code:
  <div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>'use strict';
  var $ = require('jquery');
  require('./dfp-events');
  var advertisements = {};
  module.exports = advertisements;
  
  // component configuration
  var COMPONENT_ATTR = 'data-advertisement';
  var COMPONENT_SELECTOR = '[' + COMPONENT_ATTR + ']';
  var LAZY_INIT_SELECTOR = '[data-advertisement-init="lazy"]';
  
  var SIZE_MAPPING_ATTR = 'data-advertisement-size-mapping';
  var SIZE_DEFAULT_ATTR = 'data-advertisement-size-default';
  var ADUNIT_PATH_ATTR = 'data-advertisement-adunit-path';
  var CATEGORIES_URL_ATTR = 'data-advertisement-categories-url';
  var GOOGLE_TAG_SERVICE_URL = 'https://www.googletagservices.com/tag/js/gpt.js';
  var USER_ACCEPTED_COOKIES_ATTR = 'data-advertisement-has-user-accepted-cookies';
  var REFRESH_AD_ATTR = 'data-advertisement-refresh';
  
  advertisements.create = function(elements) {
      var component = this;
      advertisements.loadGoogleTagServices();
      window.googletag.cmd.push(function() {
          window.googletag.pubads().setCookieOptions(advertisements.getCookieOptions(elements));
          window.googletag.pubads().collapseEmptyDivs(true);
          window.googletag.enableServices();
  
          [].forEach.call(elements, function(element) {
              var config = advertisements.getAdConfig(element);
              advertisements.instances.push(element);
              element.id = config.id;
              advertisements.addSlot(config);
              advertisements.refreshAdAfterScroll(element);
              advertisements.refreshAdAfterResultsUpdated();
          });
  
  
          window.googletag.enableServices();
          window.googletag.cmd.push(function() {
              window.googletag.on('gpt-slot_rendered', function(e,level,message,service,slot) {
                  var slotId = slot.getSlotId();
                  var $slot = $('#' + slotId.getDomId());
  
                  // DFP adds two iframes, one for calling scripts and one for displaying the ad. we want the one that is not hidden
                  if ($slot.find('iframe:not([id*=hidden])')
                      .map(function() { return this.contentWindow.document; })
                      .find('body')
                      .children().length &gt; 0
                  ) {
                      $slot.find('iframe').contents().find('body').addClass('redesign');
                  }
              });
          });
      });
  
      component.bindEvents();
  };
  
  advertisements.bindEvents = function() {
      // THIS IS TRIGGERED AFTER THE SEARCH RESULTS ARE DONE
      $(document).on('resultsUpdated', function(elements) {
          console.log('gottgis');
          // HERE IS WHERE I'D LIKE TO GET NEW DATA_ATTRIBUTES AND RELOAD THE DFP.
      });
  };
  
  advertisements.refreshAdAfterScroll = function(element) {
      var refreshAttr = (element.getAttribute(REFRESH_AD_ATTR).toLowerCase());
      var maxAdHeight = 650;
      var atEnd = false;
      var point2 = (($('[' + REFRESH_AD_ATTR + ']').offset().top) + maxAdHeight);
      var lastScrollTop = 0;
      var scrolledToPosition = false;
  
      if (refreshAttr !== 'true') {
          return;
      }
  
      $(window).scroll(function() {
          var currentTopPosition = $(this).scrollTop();
  
          if ($(window).scrollTop() + $(window).height() &gt; $(document).height() - maxAdHeight) {
              atEnd = true;
          }
  
          if (currentTopPosition &lt; lastScrollTop &amp;&amp; currentTopPosition &lt; point2 &amp;&amp; atEnd) {
              if (!scrolledToPosition) {
                  window.googletag.pubads().refresh();
                  scrolledToPosition = true;
              }
          }
          lastScrollTop = currentTopPosition;
      });
  };
  
  advertisements.instances = [];
  advertisements.cachedCategoriesByUrl = {};
  
  advertisements.getCategories = function(categoriesUrl) {
      var cachedCategories = advertisements.cachedCategoriesByUrl[categoriesUrl];
      if (cachedCategories) {
          return cachedCategories;
      } else {
          var getCategories = $.ajax(categoriesUrl);
          advertisements.cachedCategoriesByUrl[categoriesUrl] = getCategories;
          return getCategories;
      }
  };
  
  advertisements.getCookieOptions = function(elements) {
      var cookiesAccepted = elements[0].getAttribute(USER_ACCEPTED_COOKIES_ATTR);
      //Get the first element,this attribute has to be the same for all the ads
      if (cookiesAccepted &amp;&amp; cookiesAccepted.toLowerCase() === 'true') {
          return 0;
      }
      return 1;
  };
  
  advertisements.getAdConfig = function(element) {
      return {
          id: element.id || 'ad-' + advertisements.instances.length,
          adunitpath: element.getAttribute(ADUNIT_PATH_ATTR),
          sizemapping: JSON.parse(element.getAttribute(SIZE_MAPPING_ATTR)),
          sizedefault: JSON.parse(element.getAttribute(SIZE_DEFAULT_ATTR)),
          categoriesUrl: element.getAttribute(CATEGORIES_URL_ATTR)
      };
  };
  
  advertisements.addSlot = function(ad) {
      advertisements.getCategories(ad.categoriesUrl)
          .catch(function() {
              return {};
          })
          .then(function(categories) {
              advertisements.advertisementSlot(ad, categories);
          });
  };
  
  advertisements.advertisementSlot = function(ad, categories) {
      var tag = window.googletag
          .defineSlot(ad.adunitpath, ad.sizedefault, ad.id)
          .defineSizeMapping(ad.sizemapping)
          .addService(window.googletag.pubads());
  
      for (var key in categories) {
          if (categories.hasOwnProperty(key)) {
              tag.setTargeting(key, categories[key]);
          }
      }
  
      window.googletag.display(ad.id);
  };
  
  advertisements.loadGoogleTagServices = function() {
      window.googletag = window.googletag || {};
      window.googletag.cmd = window.googletag.cmd || [];
  
      var gads = document.createElement('script');
      gads.async = true;
      gads.src = GOOGLE_TAG_SERVICE_URL;
      var node = document.getElementsByTagName('script')[0];
      node.parentNode.insertBefore(gads, node);
  };
  
  // Turn all elements with the default selector into components.
  // Only one instance of advertisements per page.
  advertisements.create(
      document.querySelectorAll(COMPONENT_SELECTOR + ':not(' + LAZY_INIT_SELECTOR + ')')
  );</code></pre>
  </div>
  </div>
  </p>