Google Analytics

CSP for Google Analytics

analytics

Using Google Analytics with Content Security Policy

When integrating Google Analytics into your Content-Security-Policy - we also add basic conversion domains which are prominent (stats.g.doubleclick.net and www.google.com).

Allow these directives in your CSP, to support Google Analytics in your Content Security Policy:

script-src
  https://www.google-analytics.com
  https://ssl.google-analytics.com
  https://google-analytics.com;
img-src
  data:
  blob:
  www.google-analytics.com
  ssl.google-analytics.com
  www.google.com
  analytics.google.com;
font-src
  data:;
connect-src
  www.google-analytics.com
  stats.g.doubleclick.net
  ampcid.google.com
  analytics.google.com
  about:;

The main domains used by Google Analytics are:

google-analytics.com

Sources:

Using Google (Universal) Analytics with Content Security Policy(Offical Docs)Specifying a connect-src transport mechanism ('xhr' or 'beacon')(Offical Docs)Using Google Analytics And Google Tag Manager With Content Security Policy(Blog Post)RapidSec CSP Generator(RapidSec Data Network)

Example Content-Security-Policy violations / reports:

Using the above CSP package, will fix these errors that you may be seeing in your console logs:

script-src/script-src-elem/script-src-attrviolations

Refused to load the script 'https://www.google-analytics.com/analytics.js' because it violates the following Content Security Policy directive: "script-src 'self' 'report-sample'". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.

If you see inline style errors, you need to add SHA-256 hashes / nonces to your CSP with RapidSec:

Refused to execute inline script because it violates the following Content Security Policy directive: "script-src 'self' 'report-sample'". Either the 'unsafe-inline' keyword, a hash ('sha256-RFWPLDbv2BY+rCkDzsE+0fr8ylGr2R2faWMhq4lfEQc='), or a nonce ('nonce-...') is required to enable inline execution.

If you see inline eval() errors, RapidSec will generate your CSP with the specific content of the errors:

Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'report-sample'".

style-src/style-src-elem/style-src-attrviolations

If you see inline style errors, you need to add SHA-256 hashes / nonces to your CSP with RapidSec:

Refused to apply inline style because it violates the following Content Security Policy directive: "style-src 'self' 'report-sample'". Either the 'unsafe-inline' keyword, a hash ('sha256-RFWPLDbv2BY+rCkDzsE+0fr8ylGr2R2faWMhq4lfEQc='), or a nonce ('nonce-...') is required to enable inline execution.

img-srcviolations

Refused to load the image 'google-analytics.com' because it violates the following Content Security Policy directive: "img-src 'self'".

frame-srcviolations

[Report Only] Refused to frame 'google-analytics.com' because it violates the following Content Security Policy directive: "frame-src 'self'".

form-actionviolations

[Report Only] Refused to send form data to 'google-analytics.com' because it violates the following Content Security Policy directive: "form-action 'self'".

connect-srcviolations

[Report Only] Refused to connect to 'google-analytics.com' because it violates the following Content Security Policy directive: "connect-src 'self'"