Agregué el siguiente CSP: (siguiendo las instrucciones de esta página: https://www.bounteous.com/insights/2017/07/20/using-google-analytics-and-google-tag-manager-content-security- política/ )
script-src 'auto' https://www.google-analytics.com;
img-src https://www.google-analytics.com www.google-analytics.com https://stats.g.doubleclick.net;
conectar-src https://www.google-analytics.com www.google-analytics.com https://stats.g.doubleclick.net
<head> <script src="./googleAnalytics.js"></script> </head> let ga_id = "xxxxxxxxxxxx"; // (id) let ga_script = document.createElement("SCRIPT"); ga_script.type = "text/javascript"; ga_script.src = `https://www.googletagmanager.com/gtag/js?id=${ga_id}`; let script2 = document.createElement("SCRIPT"); script2.type = "text/javascript"; script2.text = ` window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', '${ga_id}');`; document.head.appendChild(ga_script); document.head.appendChild(script2); app.use(function (req, res, next) { res.setHeader( "Content-Security-Policy", "script-src 'self' https://www.google-analytics.com; img-src 'self' https://www.google-analytics.com www.google-analytics.com https://stats.g.doubleclick.net; connect-src 'self' https://www.google-analytics.com www.google-analytics.com https://stats.g.doubleclick.net;" ); next(); }); app.use(helmet( ........ ));Pero todavía me da errores de CSP. No puedo averiguar qué CSP agregar o qué hice mal que no funciona. ¿Alguien ve que he implementado algo mal?
Gracias
Lamentablemente se han seguido las instrucciones incorrectas. Además, no ha mostrado qué se está bloqueando exactamente en su caso; solo necesita agregar las fuentes bloqueadas a las directivas correspondientes.
De todos modos, si no usa nonces, el CSP para Google Analytics es:
connect-src www.google-analytics.com https://ampcid.google.com https://stats.g.doubleclick.net; img-src www.google-analytics.com https://www.google.com/ads/ga-audiences https://stats.g.doubleclick.net; script-src 'self' google-analytics.com https://ssl.google-analytics.com www.google-analytics.com; No necesita 'unsafe-inline' en el script-src ya que inserta el script GA como un archivo externo googleAnalytics.js que se encuentra en 'self' . Puede verificar exactamente su ID de Google Analytics en la prueba vinculada anteriormente.
Solo puede haber un problema: Google Analytics está estrechamente integrado con las etiquetas de conversión/remarketing de Google Ads, que utilizan dominios geográficos como https://www.google.(com|fr|co.uk|...)/ads/ para cargar el rastreador de anuncios.
Por lo tanto, la geovalla del dominio www.google depende de la IP del visitante, pero no es realista enumerar todos los geodominios de Google (lo siento, el enlace está en francés, pero los nombres de dominio son legibles).
Por lo tanto, ocasionalmente se observará el bloqueo de tales dominios geográficos. La buena noticia es que el trabajo de GA no se ve afectado por esto.