Iubenda logo
Start generating

Documentation

Table of Contents

Prior Blocking of Cookies: Google AdSense and Ad Manager

Google AdSense

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

<script>
    google_ad_client = "ca-pub-00000000";
    google_ad_slot = "XXXXXXXXXX";
    google_ad_width = 728;
    google_ad_height = 90;
</script>
<script 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:

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 Privacy Controls and Cookie Solution within your html.

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>

Google Ad Manager

In order to ensure that Google Ad Manager (previously DFP – DoubleClick for Publishers) can install cookies only after obtaining consent from the user, you have to use the method described below so that the iubenda Privacy Controls and Cookie Solution can interact with Google Ad Manager by blocking tracking when the iubenda preference cookie is not present in the user’s browser.

Code without modifications

Inclusion of the Google Ad Manager script:

<script async src="//securepubads.g.doubleclick.net/tag/js/gpt.js"></script>  
<script>var googletag = window.googletag || {cmd: []};</script>
<script>
    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>

The single Google Ad Manager banner elements:

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

Necessary modifications for the correct functioning of the iubenda Privacy Controls and Cookie Solution

Add the _iub_cs_activate class and the data-iub-purposes="5" attribute (necessary for the per-category consent) to the Google Ad Manager script:

<script async type="text/plain" class="_iub_cs_activate" data-iub-purposes="5" src="//securepubads.g.doubleclick.net/tag/js/gpt.js"></script>  
<script>var googletag = window.googletag || {cmd: []};</script>
<script type="text/plain" class="_iub_cs_activate" data-iub-purposes="5">
    googletag.cmd.push(function () {
        googletag.pubads().set("adsense_background_color", "FFFFFF");
    });
</script>
<script type="text/plain" class="_iub_cs_activate" data-iub-purposes="5">
    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>

Each Google Ad Manager banner gets a _iub_cs_activate-inline and a data-iub-purposes="5":

<div id="div-gpt-ad-1330418177496-1" style="width: 728px; height: 90px">
    <script type="text/plain" class="_iub_cs_activate-inline" data-iub-purposes="5">
        googletag.cmd.push(function () {
            googletag.display("div-gpt-ad-1330418177496-1");
        });
    </script>
</div>

See also