usuarios deciden

Analytics y Cookies


Esta es una propuesta de cara a cumplir con la ley de protección de datos que nos indica que hemos de solicitar al visitante a nuestra web su consentimiento de cara a escribir cookies en su sitio.

Plugin

function hook_Analytics()
{
    ?>
    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXX"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }
        gtag('consent', 'default', {
            'ad_storage': 'denied',
            'analytics_storage': 'denied'
        });

        gtag('js', new Date());
        gtag('config', 'G-XXXXXXXX');
    </script>
    <?php
}
add_action('wp_head', 'hook_Analytics');
// endif;

require MI_PLUGIN_PATH .  'parts/aviso_cookies.php';

aviso_cookies.php

<?php
function poner_aviso(){
if(!is_user_logged_in())
echo '<div class="grt-cookie grt-cookie-active"><div class="grt-cookies-msg"><p>Usamos cookies para asegurar que te damos la mejor experiencia en nuestra web. <a href="/politica-privacidad/"> política de privacidad</a> y <a href="/politica-de-cookies/">cookies</a>.</p>
	<span class="grt-cookie-button" onClick="aceptar()">Aceptar</span>
	<span class="grt-cookie-button cancelar" onClick="cancelar()">Cancelar</span>
	<span class="grt-cookie-button recordar" onClick="recordar()">Recordar en otro momento</span>
	</div>
</div>';
}
add_action('wp_footer','poner_aviso',100);

function cookies_js()
{
	wp_enqueue_script('cookies_js', plugin_dir_url(__FILE__) . '../assets/cookies.js', '', '');
}
add_action('wp_enqueue_scripts', 'cookies_js');

assets/cookies.js

function readCookie(name){
	var cookieValue = "";
	var search = name + "=";
	if(document.cookie.length > 0){ 
		offset = document.cookie.indexOf(search);
		if (offset != -1){ 
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) 
				end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset, end))
		}
	}
	return cookieValue;
}
function writeCookie(name, value, hours){
	var expire="";
	if(hours != null){
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + expire +";secure";
}
function aceptar(){
	var d1 = new Date();
	var days1 = 365; // 1 año
	d1.setTime(d1.getTime() + days1 * 24 * 60 * 60 * 1000);
	var expiredate = "expires=" + d1.toUTCString();
	document.cookie = "acceptlgpd=si;" + expiredate + ";path=/;secure";
	document.querySelector(".grt-cookie").classList.remove("grt-cookie-active");	

	gtag("consent", "update", {
		ad_storage: "granted",
		analytics_storage: "granted",
	});


}
function recordar(){
	document.querySelector(".grt-cookie").classList.remove("grt-cookie-active");
}
function cancelar(){
	window["ga-disable-G-XXXXXXXX"] = true;
	document.querySelector(".grt-cookie").classList.remove("grt-cookie-active");
	var d1 = new Date();
	var days1 = 60; // 2 meses
	d1.setTime(d1.getTime() + days1 * 24 * 60 * 60 * 1000);
	var expiredate = "expires=" + d1.toUTCString();
	document.cookie = "acceptlgpd=no;" + expiredate + ";path=/;secure";
}
if (!(document.cookie.indexOf("acceptlgpd") > -1)) {
	if ((readCookie("acceptlgpd").length == 0) || (readCookie("acceptlgpd") == null)) {
		//console.log("no existe, se muestra mensaje");
		document.querySelector(".grt-cookie").classList.add("grt-cookie-active");
	}
} 
else {
	if (readCookie("acceptlgpd") == "no") {
		//console.log("Recordar cookies canceladas");	
		document.querySelector(".grt-cookie").classList.remove("grt-cookie-active");	
	}
	if (readCookie("acceptlgpd") == "si")  {
		//console.log("Cookies aceptadas");	
		document.querySelector(".grt-cookie").classList.remove("grt-cookie-active");

		gtag("consent", "update", {
			ad_storage: "granted",
			analytics_storage: "granted",
		});

	}
}

De este modo, será el/la usuario/a quien decida si quiere que se carguen las cookies de Google Analytics.


subir