PHP

Implementing EU User Consent Policy for Google AdSense

If you are using Google AdSense in your website to earn some money from your website, you are probably aware that you are suppose to have EU User Consent Policy in your website by 30th September 2015.

European laws require that digital publishers to notify visitors from European countries about the use of cookies for quite sometimes now. What’s new is that, Google will be applying this to its own services such as including AdSense and DoubleClick for Publishers.

Luckily, Google came to assist AdSense publishers by setting up CookieChoices website. As stated in CookieChoices, there are a few third party vendors that offer free scripts to the publishers, and I like SilkTide scripts & implement it in my website. You can choose from six different themes.

Below are the code example which generated from SilkTide.

<!-- Begin Cookie Consent plugin by Silktide - http://silktide.com/cookieconsent -->
<script type="text/javascript">
 window.cookieconsent_options = {"message":"This website uses cookies to ensure you get the best experience on our website","dismiss":"Got it!","learnMore":"More info","link":null,"theme":"dark-bottom"};
</script>

<script type="text/javascript" src="//s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js"></script>
<!-- End Cookie Consent plugin -->

However, this code will be displayed to all visitors to your website, which may be quite annoying for visitors from non-EU countries. Why not just limit it to only visitors from the EU? Yes you could do it via GeoTargetting.

If we combine SilkTide & Geotargetting code, these are the results. Bear in mind that this code will only work if you are using Cloudflare service for your domain & enable IP Geolocation in the specific domain.

<?php
$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

$europe = array('EU', 'AT', 'BE', 'BG', 'CY', 'HR', 'CZ', 'DK', 'EE', 'FI', 'FR', 'DE', 'GR', 'HU', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PL', 'PT', 'RO', 'SK', 'SI', 'ES', 'SE', 'GB', 'AL', 'MK', 'ME', 'RS', 'TR', 'BA', 'XK');

if(in_array($country_code, $europe)) {

?>
<!-- Begin Cookie Consent plugin by Silktide - http://silktide.com/cookieconsent -->
<script type="text/javascript">
 window.cookieconsent_options = {"message":"This website uses cookies to ensure you get the best experience on our website","dismiss":"Got it!","learnMore":"More info","link":null,"theme":"dark-bottom"};
</script>

<script type="text/javascript" src="//s3.amazonaws.com/cc.silktide.com/cookieconsent.latest.min.js"></script>
<!-- End Cookie Consent plugin -->

<?php } ?>

How to Target Country Specific Visitors with Cloudflare

If you would like to target specific contents or ads for visitors in certain countries easily, you may use the code below. Make sue you change the array in the target countries.

Bear in mind that this code will only work if you are using Cloudflare service for your domain & enable IP Geolocation in the specific domain. IP Geolocation option can be enabled in Network section of Cloudflare panel. By enabling IP Geolocation, you may retrieve IP Geolocation information from the CF-IPCountry HTTP header.

$country_code = $_SERVER["HTTP_CF_IPCOUNTRY"];

$target_country = array('GB', 'US');

if(in_array($country_code, $target_country) {
// action if true
} else {
// action if false
}