Documentation index

Cookie solution ›

How to configure Google AdSense and DoubleClick for Publishers (DFP) for the cookie law


Guide for Google AdSense

Below is the original procedure, which allows for the addition of two script tags to your pages:

<script type="text/javascript">
        google_ad_client = "ca-pub-00000000";
        google_ad_slot = "XXXXXXXXXX";
        google_ad_width = 728;
        google_ad_height = 90;
</script>
<script type="text/javascript" src="//pagead2.googlesyndication.com/pagead/show_ads.js"></script>

To make sure that the Google AdSense banners don’t save any cookies before user consent, it’s necessary to make these changes:

1. Define the function iubenda_adsense_unblock associating it to the callback onConsentGiven:

<script>
        function iubenda_adsense_unblock(){
            var t = 1;
            $('._iub_cs_activate_google_ads').each(function() {
                var banner = $(this);
                setTimeout(function(){
                    var client = banner.data('client');
                    var slot = banner.data('slot');
                    var width = banner.data('width');
                    var height = banner.data('height');
                    var adsense_script = '<scr'+'ipt type="text/javascript">\n'
                            + 'google_ad_client = "'+client+'";\n'
                            + 'google_ad_slot = "'+slot+'";\n'
                            + 'google_ad_width = '+width+';\n'
                            + 'google_ad_height = '+height+';\n'
                            + '</scr'+'ipt>';
                    var script = document.createElement('script');
                    var ads = document.createElement('ads');
                    var w = document.write;

                    // override and replace with our version
                    document.write = (function(params)
                    {
                        // replace our placeholder with real ads
                        ads.innerHTML = params;
                        // put the old implementation back in place
                        // Aristos, add this check because called more than ones
                        //  and ends, with this symbol.
                        if(params.indexOf("</ins>") != -1)
                            document.write=w;
                    });
                    script.setAttribute('type', 'text/javascript');
                    script.setAttribute('src', 'http://pagead2.googlesyndication.com/pagead/show_ads.js');
                    script.setAttribute('async', 'async');
                    banner.html(adsense_script).append(ads).append(script);
                }, t);
                t += 200;
            });
        }

        if('callback' in _iub.csConfiguration) {
            _iub.csConfiguration.callback.onConsentGiven = iubenda_adsense_unblock;
        } else {
            _iub.csConfiguration.callback = {};
            _iub.csConfiguration.callback.onConsentGiven = iubenda_adsense_unblock;
        }
    </script>
    
    

You should insert the code above just after configuring the Iubenda Cookie Solution within your html.

2. Create div sections for each banner, giving them the attributes of the type data- and adding the class _iub_cs_activate_google_ads:

<div style="width: 728px;height:90px;" class="_iub_cs_activate_google_ads"
         data-client="ca-pub-00000000"
         data-slot="XXXXXXXXXX"
         data-width="728"
         data-height="90">
</div>

Guide for DoubleClick for Publishers (DFP)

In order to ensure that DoubleClick for Publishers (DFP) can install cookies only after obtaining consent from the user, it is necessary to use one of the methods described in this article so that the iubenda Cookie Solution can interact with DFP by blocking tracking when the iubenda preference cookie is not present in the user’s browser.

You can choose one of three methods available for integration, depending on the DFP code version you use. The methods proposed for “asynchronous” and “synchronous” code only work for the creativities provided directly by DFP.

Method 1 – “asynchronous” code

Below you’ll find the “asynchronous” codes before and after the necessary modifications to the script.

BEFORE (asynchronous)

Script for the configuration of DFP:

<script type="text/javascript">
          var googletag = googletag || {};
          googletag.cmd = googletag.cmd || [];
          (function () {
              var gads = document.createElement("script");
              gads.async = true;
              gads.type = "text/javascript";
              var useSSL = "https:" == document.location.protocol;
              gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js";
              var node = document.getElementsByTagName("script")[0];
              node.parentNode.insertBefore(gads, node);
          })();
</script>
      <script type="text/javascript">
          googletag.cmd.push(function () {
              googletag.pubads().set("adsense_background_color", "FFFFFF");
          });
      </script>
      <script>
         googletag.cmd.push(function() {
          googletag.defineSlot("/1234/travel/asia/food", [728, 90], "div-gpt-ad-123456789-0")
                      .addService(googletag.pubads())
                      .setTargeting("interests", ["sports", "music", "movies"]);
              googletag.pubads().setTargeting("topic", "basketball");
              googletag.pubads().enableSingleRequest();
              googletag.enableServices();
         });
      </script>

The single DFP banners:

    <div id="div-gpt-ad-123456789-0" style="width: width; height: height;">
      <script type="text/javascript">
          googletag.cmd.push(function () {
              googletag.display("div-gpt-ad-123456789-0");
          });
      </script>
    </div>

AFTER (asynchronous)

The necessary modifications for the configuration of DFP for the blocking of cookies before user consent.

1. Define the function isConsentGiven for the management of cookie:

<script type='text/javascript'>
      function isConsentGiven() {
          if('consentIsGiven' in window && (consentIsGiven === true || consentIsGiven === false)) return consentIsGiven;
          if(!('_iub' in window && 'csConfiguration' in _iub)) return false;
          var siteId = _iub.csConfiguration.siteId || '';
          var cookiePolicyId = _iub.csConfiguration.cookiePolicyId || '';
          var cs = document.cookie.split(';');
          var consentIsGiven = false;
          for (var i = 0; i < cs.length; i++) {
              while (cs[i].charAt(0) == ' ') cs[i] = cs[i].substring(1);
              if(cs[i].indexOf('_iub_cs-s'+ siteId) == 0||cs[i].indexOf('_iub_cs-'+ cookiePolicyId) == 0) {
                  consentIsGiven = true;
                  break;
              }
          }
          window.consentIsGiven = consentIsGiven;
          return consentIsGiven;
      }
    </script>

2. To the DFP script we’ll add the control isConsentGiven that figures out if cookies may be installed or not

<script type="text/javascript">
          var googletag = googletag || {};
          googletag.cmd = googletag.cmd || [];
          (function () {
              var gads = document.createElement("script");
              gads.async = true;
              gads.type = "text/javascript";
              var useSSL = "https:" == document.location.protocol;
              gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js";
              var node = document.getElementsByTagName("script")[0];
              node.parentNode.insertBefore(gads, node);
          })();
</script>
<script>
    googletag.cmd.push(function () {
        if(isConsentGiven())
            googletag.pubads().setCookieOptions(0);
        else
            googletag.pubads().setCookieOptions(1);
        googletag.defineSlot('/1234/travel', [728,90], "div-gpt-ad-123456789-0").addService(googletag.pubads());
        googletag.pubads().enableSyncRendering();
        googletag.enableServices();
    });
</script>

3. Add the DFP banner elements without any necessary changes:

<div id="div-gpt-ad-123456789-0" style="width: width; height: height;">
      <script type="text/javascript">
          googletag.cmd.push(function () {
              googletag.display("div-gpt-ad-123456789-0");
          });
      </script>
</div>

The part in the isConsentGiven function in particular gets inserted before the DFP script and preferably after the code that initializes the iubenda Cookie Solution.

Method 2 – “synchronous” code

Here below you will find the codes based on the “synchronous” approach before and after the addition of the iubenda modifications.

BEFORE (synchronous)

DFP configuration script

<script type="text/javascript">
          (function() {
              var useSSL = 'https:' == document.location.protocol;
              var src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
              document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
          })();
</script>
<script type="text/javascript">
              googletag.pubads().set("adsense_background_color", "FFFFFF");
</script>
<script>
    googletag.defineSlot("/1234/travel/asia/food", [728, 90], "div-gpt-ad-123456789-0")
        .addService(googletag.pubads())
        .setTargeting("interests", ["sports", "music", "movies"]);
   googletag.pubads().setTargeting("topic", "basketball");
   googletag.pubads().enableSingleRequest();
   googletag.enableServices();
</script>

DFP single banners:

<div id="div-gpt-ad-123456789-0" style="width: width; height: height;">
      <script type="text/javascript">
              googletag.display("div-gpt-ad-123456789-0");
      </script>
    </div>

AFTER (synchronous)

The necessary modifications for the configuration of DFP for the blocking of cookies before user consent.

1. Define the function isConsentGiven for the management of cookie:

<script type='text/javascript'>
      function isConsentGiven() {
          if('consentIsGiven' in window && (consentIsGiven === true || consentIsGiven === false)) return consentIsGiven;
          if(!('_iub' in window && 'csConfiguration' in _iub)) return false;
          var siteId = _iub.csConfiguration.siteId || '';
          var cookiePolicyId = _iub.csConfiguration.cookiePolicyId || '';
          var cs = document.cookie.split(';');
          var consentIsGiven = false;
          for (var i = 0; i < cs.length; i++) {
              while (cs[i].charAt(0) == ' ') cs[i] = cs[i].substring(1);
              if(cs[i].indexOf('_iub_cs-s'+ siteId) == 0||cs[i].indexOf('_iub_cs-'+ cookiePolicyId) == 0) {
                  consentIsGiven = true;
                  break;
              }
          }
          window.consentIsGiven = consentIsGiven;
          return consentIsGiven;
      }
    </script>

2. To the DFP script we’ll add the control isConsentGiven that figures out if cookies may be installed or not

<script type="text/javascript">
          (function() {
              var useSSL = 'https:' == document.location.protocol;
              var src = (useSSL ? 'https:' : 'http:') + '//www.googletagservices.com/tag/js/gpt.js';
              document.write('<scr' + 'ipt src="' + src + '"></scr' + 'ipt>');
          })();
</script>
<script type="text/javascript">
              googletag.pubads().set("adsense_background_color", "FFFFFF");
</script>
<script>
    if(isConsentGiven())
         googletag.pubads().setCookieOptions(0);
    else
         googletag.pubads().setCookieOptions(1);
    googletag.defineSlot("/1234/travel/asia/food", [728, 90], "div-gpt-ad-123456789-0")
        .addService(googletag.pubads())
        .setTargeting("interests", ["sports", "music", "movies"]);
   googletag.pubads().setTargeting("topic", "basketball");
   googletag.pubads().enableSingleRequest();
   googletag.enableServices();
</script>

The single DFP banner elements without any necessary changes:

<div id="div-gpt-ad-123456789-0" style="width: width; height: height;">
      <script type="text/javascript">
              googletag.display("div-gpt-ad-123456789-0");
      </script>
</div>

Also in this case the code with the function isConsentGiven should be inserted before DFP and preferably after the iubenda script.

Method 3

Last but no least, if the creativities originate from services outside, the following codes need to be used instead.

This is the code without modifications:

1. Inclusion of the DFP script:

<script type="text/javascript">
          var googletag = googletag || {};
          googletag.cmd = googletag.cmd || [];
          (function() {
              var gads = document.createElement('script');
              gads.async = true;
              gads.type = 'text/javascript';
              var useSSL = 'https:' == document.location.protocol;
              gads.src = (useSSL ? 'https:' : 'http:') +
                      '//www.googletagservices.com/tag/js/gpt.js';
              var node = document.getElementsByTagName('script')[0];
              node.parentNode.insertBefore(gads, node);
          })();
      </script>
      <script type="text/javascript">
          googletag.cmd.push(function () {
              googletag.pubads().set("adsense_background_color", "FFFFFF");
          });
      </script>
      <script>
          googletag.cmd.push(function() {
              googletag.defineSlot('/1010125/Prontoimprese/Ros/300x250Top-ProntoImprese-Ros', [[300, 250], [300, 600]], 'div-gpt-ad-1330418177496-1').addService(googletag.pubads());
              googletag.pubads().enableSingleRequest();
              googletag.enableServices();
          });
      </script>

2. The single DFP banner elements:

<div id="div-gpt-ad-1330418177496-1">
      <script  type="text/javascript">
          googletag.cmd.push(function () {
              googletag.display("div-gpt-ad-1330418177496-1");
          });
      </script>
</div>

And here below you’ll find the codes with the necessary modifications for the correct functioning of the iubenda Cookie Solution:

1. Add the class _iub_cs_activate to the DFP script:

<script type="text/plain" class="_iub_cs_activate">
          var googletag = googletag || {};
          googletag.cmd = googletag.cmd || [];
          (function() {
              var gads = document.createElement('script');
              gads.async = true;
              gads.type = 'text/javascript';
              var useSSL = 'https:' == document.location.protocol;
              gads.src = (useSSL ? 'https:' : 'http:') +
                      '//www.googletagservices.com/tag/js/gpt.js';
              var node = document.getElementsByTagName('script')[0];
              node.parentNode.insertBefore(gads, node);
          })();
      </script>
      <script type="text/plain" class="_iub_cs_activate">
          googletag.cmd.push(function () {
              googletag.pubads().set("adsense_background_color", "FFFFFF");
          });
      </script>
      <script type="text/plain" class="_iub_cs_activate">
        googletag.cmd.push(function() {
              googletag.defineSlot('/1010125/Prontoimprese/Ros/300x250Top-ProntoImprese-Ros', [[300, 250], [300, 600]], 'div-gpt-ad-1330418177496-1').addService(googletag.pubads());
              googletag.pubads().enableSingleRequest();
              googletag.enableServices();
            });
      </script>

2. Each DFP banner gets a _iub_cs_activate-inline

<div id="div-gpt-ad-1330418177496-1" style="width: 728px; height: 90px">

      <script  type="text/plain" class="_iub_cs_activate-inline">
          googletag.cmd.push(function () {
              googletag.display("div-gpt-ad-1330418177496-1");
          });
      </script>

    </div>

RELATED ARTICLES


Still have questions?

Visit our support forum Email us