Help improve the Google Publisher Tag developer experience. Share your thoughts in our survey.

Configure cookie options

This example illustrates how to enable/disable cookies for ad requests using the Google Publisher Tag library. Learn more about disabling cookies for ad requests in the Google Ad Manager help center.

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Configure Cookie Options</title>
    <script async src="https://securepubads.g.doubleclick.net/tag/js/gpt.js"></script>
    <script>
      window.googletag = window.googletag || {cmd: []};
      var cookiesEnabled = 1;

      googletag.cmd.push(function() {
        // Cookies are enabled by default. Set cookie options to 1 to disable.
        //
        // To ensure this option takes effect for all ad requests, place this
        // call before any calls to enableServices or display.
        googletag.pubads().setCookieOptions(cookiesEnabled);

        googletag.defineSlot('/6355419/Travel/Europe/France',[728, 90], 'banner-ad')
            .addService(googletag.pubads());
        googletag.enableServices();
      });

      function toggleCookieOptions(button) {
        // Cookie options can be changed at any time. Changes apply to all
        // subsequent ad requests.
        //
        // Set to 0 to enable cookies, 1 to disable.
        cookiesEnabled = cookiesEnabled === 0 ? 1 : 0;

        googletag.cmd.push(function() {
          googletag.pubads().setCookieOptions(cookiesEnabled);

          // Refresh all ads on the page for changes to take affect.
          googletag.pubads().refresh();

          button.setAttribute('data-enabled', cookiesEnabled);
        });
      }
    </script>
    <style>
      button::after {
        content: "DISABLED";
        color: red;
        font-weight: bold;
      }
      button[data-enabled="0"]::after {
        content: "ENABLED";
        color: green;
        font-weight: bold;
      }
    </style>
  </head>
  <body>
    <div id="banner-ad" style="width: 728px; height: 90px;">
      <script>
        googletag.cmd.push(function() {
          googletag.display('banner-ad');
        });
      </script>
    </div>
    <div>
      <button onclick="toggleCookieOptions(this);">
        Cookies
      </button>
    </div>
  </body>
</html>