Event-based ad requests

You can request ads based on events using the Google Publisher Tag library. In the example below, a new ad is not requested or rendered until the user clicks a button.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Event-based ad requests example</title>
    <script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
    <script>
      window.googletag = window.googletag || {cmd: []};

     googletag.cmd.push(function() {
        googletag.defineSlot('/6355419/Travel',[728, 90], 'div-for-slot')
            .setTargeting('test', 'event')
            .addService(googletag.pubads());
        googletag.pubads().disableInitialLoad();
        googletag.enableServices();
      });
    </script>
  </head>
  <body>
    <div id="div-for-slot" style="width: 300px; height: 250px;">
      <script>
        googletag.cmd.push(function() {
          // This will only register the slot.
          // Ad will be fetched only when refresh is called.
          googletag.display('div-for-slot');
        });
      </script>
    </div>
    <button onclick="googletag.cmd.push(function() { googletag.pubads().refresh(); });">
      Show/Refresh Ad
    </button>
  </body>
</html>