Support » Plugin: GDPR » Suggestion / snippet for people using Google Ads

  • I know the plugin is not meant to actually block cookies, but simply to provide tools for developers to do the actual blocking, but…
    certain ad networks / scripts, like GoogleAdsense, have known methods and scripts.

    So, just an idea, you could ask admins in the settings, if they are using GoogleAdsense and in case run something like this in the wp_head:

    <?php
    
    if(function_exists('has_consent')) {
    	
    	$cookie_bool = has_consent('IDE') ? 'true' : 'false'; // name of cookie that Google Ads uses, would be even better to check a whole cookie cat OR a cookie cat subcat like Advertising -> Google AdSense
      	?>
    
    	<!-- The google ad script needs to run BEFORE the ads are shown, so we can set all the necessary stuff, that's why it all goes in the <head> -->
    	<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
    
    	<script type='text/javascript'>
    		
    		//  Pause Google Ads and anonymize until we know the consent status
    		(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 1;
    		(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = 1;
    		// Check consent
    		var hascookie = <?php echo $cookie_bool; ?>;
            if(typeof hascookie === 'boolean' && hascookie === true) {
    			hasconsent = 1;
    		} else {
    			hasconsent = 0;
    		}
    		// Check if we have consent AND browser is not sending a DoNotTrack request 
    		var google_ads_personalized_consent =  (hascookie >= 0 && (navigator.doNotTrack != 'unspecified' && navigator.doNotTrack != '1'));
    		// If we have both consents set the method = 0 otherwise = 1
    		(adsbygoogle = window.adsbygoogle || []).requestNonPersonalizedAds = !google_ads_personalized_consent;
    		// Resume Google Ads
    		(adsbygoogle = window.adsbygoogle || []).pauseAdRequests = 0;
    	</script>
    
    <?php } ?>

    I haven’t tested this fully, but it seems to work fine for now.
    I’ll update my Gist with more code when I have some time.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Does this code work with this plugin? How do I check exactly what is working? How do I check if I’m loading custom ads or not?

    Thread Starter Shambix

    (@shambix)

    Currently it seems there isn’t a way to know from the page itself.
    Not sure if Google takes the info from the user (from the current page) and sets it like that in their account (assuming they are logged in a Google account).

    https://support.google.com/ads/answer/2662856?hl=en-AU

    All I can tell you is that the script IS sending a request for non personalized ads… however I can’t tell you what Google does with that afterwards or how it handles it (can’t find any useful documentation on that… yet).

    Please do check the Gist rather than the code above, I have updated the Gist but I wont update the script in this post.

    Plugin Author Fernando Claussen

    (@fclaussen)

    Thank you for this.
    Replace has_consent with is_allowed_cookie. Consent is more like policies and stuff.

    Thread Starter Shambix

    (@shambix)

    No problem 🙂
    I’ll update my Gist then (can’t edit my old post anymore).

    Hi,
    Just to be sure I understand how to proceed, could you please tell me which lines of code you put in the BODY at the exact place where the ads should be shown (max. 3 ads per page). I suppose there must be (adsbygoogle = window.adsbygoogle || []).push({});)
    Are there other lines that should be set there?
    Thanks for your help

    Thread Starter Shambix

    (@shambix)

    @dgoethals in the body you can put the ads as you get them from AdWords, just like you would normally (including the js with “push”, the one you quoted).

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Suggestion / snippet for people using Google Ads’ is closed to new replies.