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

Configure privacy settings

This example illustrates how to configure privacy settings for ad requests using the Google Publisher Tag library. Learn more about available privacy settings in the Google Ad Manager help center:

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Configure Privacy Settings</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], 'banner-ad')
            .setTargeting('test', 'privacy')
            .addService(googletag.pubads());
        googletag.enableServices();
      });

      var tfcdEnabled = false;
      function toggleChildDirectedTreatment(button) {
        // Set to true to enable, false to disable.
        tfcdEnabled = !tfcdEnabled;

        googletag.cmd.push(function() {
          googletag.pubads().setPrivacySettings({
            childDirectedTreatment: tfcdEnabled
          });

          // Refresh all ads on the page.
          googletag.pubads().refresh();

          button.setAttribute('data-enabled', tfcdEnabled);
        });
      }

      var rdpEnabled = false;
      function toggleRestrictDataProcessing(button) {
        // Set to true to enable, false to disable.
        rdpEnabled = !rdpEnabled;

        googletag.cmd.push(function() {
          googletag.pubads().setPrivacySettings({
            restrictDataProcessing: rdpEnabled
          });

          // Refresh all ads on the page.
          googletag.pubads().refresh();

          button.setAttribute('data-enabled', rdpEnabled);
        });
      }

      var tfuaEnabled = false;
      function toggleUnderAgeOfConsent(button) {
        // Set to true to enable, false to disable.
        tfuaEnabled = !tfuaEnabled;

        googletag.cmd.push(function() {
          googletag.pubads().setPrivacySettings({
            underAgeOfConsent: tfuaEnabled
          });

          // Refresh all ads on the page.
          googletag.pubads().refresh();

          button.setAttribute('data-enabled', tfuaEnabled);
        });
      }
    </script>
    <style>
      button::after {
        content: "OFF";
        color: red;
        font-weight: bold;
      }
      button[data-enabled="true"]::after {
        content: "ON";
        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="toggleChildDirectedTreatment(this);">
        Child Directed Treatment
      </button>
      <button onclick="toggleRestrictDataProcessing(this);">
        Restricted Data Processing
      </button>
      <button onclick="toggleUnderAgeOfConsent(this);">
        Under Age of Consent
      </button>
    </div>
  </body>
</html>