Menú de documentación

Lectura en el servidor

Formato de la cookie de consentimiento de CookieHug y lectura en el servidor: ejemplos en PHP, Node.js, Python y C#.

Uso en el servidor

CookieHug guarda una cookie HTTP (CookieHugConsent) junto al localStorage, lo que te permite comprobar el estado del consentimiento en el servidor.

PHP

if (isset($_COOKIE['CookieHugConsent'])) {
    $consent = json_decode(
        urldecode($_COOKIE['CookieHugConsent']), true
    );

    if ($consent && $consent['statistics']) {
        // El usuario aceptó las estadísticas
        echo '<script src="https://www.googletagmanager.com/gtag/js?id=G-XXXXX"></script>';
    }

    if ($consent && $consent['marketing']) {
        // El usuario aceptó el marketing
    }
} else {
    // Aún no hay consentimiento: no cargues scripts de seguimiento
}

Node.js (Express)

const cookieParser = require('cookie-parser');
app.use(cookieParser());

app.get('/', (req, res) => {
  const raw = req.cookies.CookieHugConsent;
  if (raw) {
    const consent = JSON.parse(decodeURIComponent(raw));

    if (consent.statistics) {
      // Renderizar el script de analítica en el servidor
    }
    if (consent.marketing) {
      // Renderizar los píxeles de marketing en el servidor
    }
  }

  res.render('index');
});

Python (Django)

import json
from urllib.parse import unquote

def index(request):
    raw = request.COOKIES.get('CookieHugConsent')
    if raw:
        consent = json.loads(unquote(raw))

        if consent.get('statistics'):
            # Renderizar la analítica
            pass
        if consent.get('marketing'):
            # Renderizar el marketing
            pass

    return render(request, 'index.html')

C# (ASP.NET)

var raw = Request.Cookies["CookieHugConsent"];
if (!string.IsNullOrEmpty(raw))
{
    var consent = JsonSerializer.Deserialize<CookieHugConsent>(
        Uri.UnescapeDataString(raw)
    );

    if (consent.Statistics)
    {
        // Renderizar la analítica en el servidor
    }
}