Skip to content

Stream web analytics data to Umami - #656

Merged
frankieroberto merged 1 commit into
mainfrom
add-umami-analytics-csp
Oct 20, 2026
Merged

frankieroberto merged 1 commit into
mainfrom
add-umami-analytics-csp

Conversation

@sjlegg

@sjlegg sjlegg commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Streams web analytics for the design history site to the NHSE-hosted Umami instance, so we can understand how the site is used. This replicates the approach in digital-prevention-services-portfolio#187: the tracker, a Content Security Policy (CSP) and a privacy notice are added together because the CSP is needed to constrain what the tracker can load, and the notice is needed to explain what is collected.

The site is hosted on GitHub Pages, where custom headers cannot be set, so the CSP is applied with a <meta> tag.

Changes

Analytics tracker (eleventy.config.js)

  • Inject the Umami script into every page using the existing analytics host (analytics.digital-prevention-services.nhs.uk) and a new website ID for this site, restricted to design-history.prevention-services.nhs.uk.
  • Place the script after application.js so it cannot delay the page's own scripts (deferred scripts run in document order).

Content Security Policy (eleventy.config.js)

  • Inject a CSP <meta> tag at the top of every page's <head>.
  • Allow the Umami host for scripts and connections, plus assets.nhs.uk for NHS fonts.
  • Allow the Vimeo player (script and frame) used on one post, and user-images.githubusercontent.com for the embedded videos on six older posts.
  • Allow inline styles, which the Vimeo embed relies on, and a hash for the one inline script in the NHS layout.

Privacy notice (app/privacy-notice.md)

  • Add a notice covering what analytics data is collected, retention, lawful basis, individual rights and contact details, tailored to this site.
  • Link it from the footer.

Redirect pages (app/redirects.njk)

  • Add a <head> so the CSP and tracker transform applies to redirect pages.
  • Remove the inline location = ... script, which the CSP would block; the existing meta refresh performs the redirect.

Validation

  • npm run build succeeds; all 595 generated HTML pages contain the CSP and the Umami script, with the script after application.js.
  • Compared a build of main against this branch for a random 5% of pages (plus all pages with videos), at 1280px and 390px, using screenshots above the footer: pixel-identical apart from the new footer link, and no CSP violations in the browser console. This caught the GitHub video host missing from the first version of the CSP.
  • ESLint and Prettier pass for the changed JavaScript. The build still reports the existing NHS Sass deprecation warning.

Notes for reviewers

  • The Vimeo embed's rendering was not covered by the screenshot comparison, so it is worth checking on vaccinations/2026/09/how-rosie-got-vaccinated.

Add the Umami tracker, a Content Security Policy and a privacy notice
for the design history site, matching the approach in
digital-prevention-services-portfolio#187.

- Inject the Umami script into every page, after application.js
- Inject a CSP meta tag into every page
- Add a privacy notice and link it from the footer
- Give redirect pages a head so the transform applies to them
@sjlegg
sjlegg requested a review from a team as a code owner October 20, 2026 11:10
@edwardhorsford
edwardhorsford temporarily deployed to dsps-design-add-umami-a-20jvjg October 20, 2026 11:11 Inactive
Comment thread eleventy.config.js
data-website-id="${analyticsWebsiteId}"
data-host-url="${analyticsHost}"
data-domains="design-history.prevention-services.nhs.uk"></script>
</head>`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit fragile, and maybe we should add an easier way to insert meta tags and scripts into the template file upstream in https://github.com/x-govuk/nhsuk-eleventy-plugin/blob/main/src/layouts/base.njk ?

In the short term we could override the base.njk template and put the tags there?

@sjlegg sjlegg Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I've made (albeit rapidly with AI) a PR to address this: x-govuk/nhsuk-eleventy-plugin#105.

In the meantime I'd rather avoid the short term approach of overriding the template as it (a) creates extra maintenance work syncing the template with the upstream template file and (b) adds quite a lot of extra duplicate code to this repo.

Would you mind approving? And let's discuss separately how to get the other PR reviewed. I'm not a frontend developer so I may not be the best person to take that forward though!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding analytics would need a cookie banner, no?

(See x-govuk/govuk-eleventy-plugin#383 (comment) for my previous thoughts on this topic!)

@sjlegg sjlegg Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulrobertlloyd Umami does not store any cookies, so no cookie banner is required. In addition our DPIA screening questionnaire does not rely on consent as the basis for processing, so user opt in is not required.

That's different to Google Analytics, where the normal way it is used requires cookies to join up user identity between pageviews, and so does require a cookie opt in under PECR. Although even with Google Analytics it's possible to use it in cookieless mode.

@frankieroberto
frankieroberto merged commit 21ad763 into main Oct 20, 2026
4 checks passed
@frankieroberto
frankieroberto deleted the add-umami-analytics-csp branch October 20, 2026 15:13

This branch was previously deployed

1 inactive deployment
dsps-design-add-umami-a-20jvjg — d77350df Deployed Oct 20, 2026 by edwardhorsford
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants