You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Right now, if your sites Content-Security header is set to anything other than unsafe-inline most of the analytics other than Google won't run. They will be blocked by the CSP.
I've been working on a solution for these in-line scripts that would allow them to run, while still allowing a restrictive CSP as recommended. (unsafe-inline is definitely not recommended!)
I think I've come up with a better way to include analytics that doesn't involve adding in-line scripts, etc.
in /assets/scripts/features
analytics
├── index.js
├── posthog.js
└── statcounter.js
import*asparamsfrom'@params';if(params.analytics){// probably don't need this oneif(params.analytics.statcounter){import('./statcounter');}if(params.analytics.posthog){import('./posthog');}if(params.analytics.matomo){import('./matomo')l}}
Now all script elements are included in application.js and are hashed, so no in-line scripts.
To add a new kind of analytics (like posthog for instance) You would just need to add a new posthog.js file to the /assets/scripts/features/analytics directory, and update index.js with
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Question
Right now, if your sites
Content-Security
header is set to anything other thanunsafe-inline
most of the analytics other than Google won't run. They will be blocked by the CSP.I've been working on a solution for these in-line scripts that would allow them to run, while still allowing a restrictive CSP as recommended. (
unsafe-inline
is definitely not recommended!)I think I've come up with a better way to include analytics that doesn't involve adding in-line scripts, etc.
in /assets/scripts/features
analytics
├── index.js
├── posthog.js
└── statcounter.js
in `features/index.js
in
features/analytics/index.js
assets/scripts/features/statcounter.js
Now all script elements are included in
application.js
and are hashed, so noin-line
scripts.To add a new kind of analytics (like posthog for instance) You would just need to add a new
posthog.js
file to the/assets/scripts/features/analytics
directory, and updateindex.js
withAnd the posthog scripts would be included, and hashed, so executable.
What do you think?
Beta Was this translation helpful? Give feedback.
All reactions