• Resolved mtrehearn

    (@mtrehearn)


    Hi there,

    I have Adsense running both in articles and a sidebar on my site and I have to add the script code wherever I want the ad to go – I.e. multiple times per page. I would like to have 2 toggles to give the user the choice of 1. Having ads that are personalised or not and 2. Having ads on or off altogether.

    Could you please let me know how I can do this and have it work on all ads on all pages simultaneously?

    Thank you for your help.

Viewing 1 replies (of 1 total)
  • Plugin Author Afroditi

    (@ralliaf)

    Hello,
    the generic approach about using AdSense with Cookie Control is to have a cookie category “marketing” in your configuration, and put in it the cookies that are placed by AdSense. Then you would need to run AdSense inside the onAccept callback of that category. To do that, you would need to wrap every block on your page that contains ads with a div of a particular class name, e.g “ad-sense”. The onAccept code would be as follows:

    // Find all AdSense div containers:
                        var adSenseContainers = document.querySelectorAll(".ad-sense");
    
                        // AdSense is inserted using document.write, which will not work after the page is renderes. So we reset it.
                        var w = document.write;
                        document.write = function (content) { container.innerHTML = content; }; 
    
                        // Iterate through the ad containers and inject adSense dynamically:
                        for (var i = 0; i < adSenseContainers.length; i++) {
                            var container = adSenseContainers[i];
    
                            var script = document.createElement('script');
                            script.type = 'text/javascript';
                            script.src = 'https://pagead2.googlesyndication.com/pagead/show_ads.js';;
                            document.body.appendChild(script);

    Cookie Control will automatically clear the cookies when the user revokes consent. For completeness, you might also want to use the third party cookies functionality, to allow your users to permanently opt out from AdSense (optOutLink: https://adssettings.google.com/authenticated).

    Alternatively, if you just want to prevent cookie placement by AdSense (which will block personalised ads) you can achieve that by running Google AdSense as you would normally do, outside Cookie Control. Then add in your “marketing” cookies the cookie “__gad”.

    If the user turns the marketing cookies on, or if he clicks “accept” and that category is on by default, then this cookie will be added to your list of protected cookies and it should not be deleted on subsequent page loads. So on subsequent page loads after AdSense runs Cookie Control will not delete the cookie.

    In order to combine the two cases you should writte some custom javascript code. Please also check Cookie Control documentation here https://www.civicuk.com/cookie-control/v8/documentation

Viewing 1 replies (of 1 total)
  • The topic ‘Google Adsense’ is closed to new replies.