Google analytics.js and Content Security Policy

后端 未结 3 1343
Happy的楠姐
Happy的楠姐 2021-02-03 22:56

I have a web app using the default html5boilerplate Content Security Policy.

However, we have the new Google analytics.js snippet on the page, which is being blocked by

3条回答
  •  误落风尘
    2021-02-04 00:02

    Using the alternate way described on https://developers.google.com/analytics/devguides/collection/analyticsjs/ helped. It is much like OP described in the second edit, you can either use a custom inline script that you apply a nonce on or outsource the inline script content into a seperate script, like OP suggested. Don't forget the async attribute on the script tag that references analytics.js.

    Using this method, there will be no errors/warnings as no scripts are injected to your html.

    Here is the relevant part from the link I posted:

    Alternative async tracking snippet

    While the JavaScript tracking snippet described above ensures the script will be loaded and executed asynchronously on all browsers, it has the disadvantage of not allowing modern browsers to preload the script.

    The alternative async tracking snippet below adds support for preloading, which will provide a small performance boost on modern browsers, but can degrade to synchronous loading and execution on IE 9 and older mobile browsers that do not recognize the async script attribute. Only use this tracking snippet if your visitors primarily use modern browsers to access your site.

    
    
    
    
    

提交回复
热议问题