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)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘Suggestion / snippet for people using Google Ads’ is closed to new replies.