Load the Pulse analytics SDK after consent
React · React · intermediate · modification
Marketing asked for product analytics on the dashboard, and AppSec signed off on the Pulse SDK with SRI. `src/analytics/loadPulse.js` (new) adds the SDK's script tag once, with the integrity hash from AppSec and CORS mode, and resolves with `window.Pulse` when it loads or rejects when it fails. `src/analytics/AnalyticsLoader.jsx` (new) loads it once the visitor has consented, calls `Pulse.init` with the site key and logs a warning if loading fails. `src/App.jsx` mounts the loader with the consent flag. `src/analytics/loadPulse.test.jsx` (new) tests the script tag's attributes, the load and error paths, and the loader with and without consent.
Dashboard SPA on React 18.3 (Vite 5, function components, `StrictMode` in `main.jsx`). Tests run with `npm test`: Vitest 2 with @testing-library/react 16 in jsdom. Existing and unchanged: `useConsent()` returns `{ analytics, setAnalytics }`, where `analytics` is a boolean read from the consent cookie; it can go from `false` to `true` once while the page is open (the banner's Accept button), and withdrawing consent reloads the page. `ConsentBanner` and `Dashboard` are existing components. `VITE_PULSE_SITE_KEY` is set in `.env.development` and `.env.production`; the site key is public by design. The CSP header set by nginx includes `script-src 'self' https://cdn.jsdelivr.net`, which allows scripts from that host and carries no hashes, and `connect-src 'self' https://collect.pulse-analytics.io`, which already allows Pulse's collector (added when AppSec reviewed the vendor); the SDK sends its events only to that collector. jsDelivr serves npm files at `https://cdn.jsdelivr.net/npm/<package>@<version>/<file>` with `Access-Control-Allow-Origin: *`. Pulse SDK facts (vendor docs): executing `dist/pulse.min.js` defines `window.Pulse`; `Pulse.init({ siteKey })` starts tracking and records page views itself, client-side navigation included. 3.4.1 is the newest release on npm when this PR is opened, and the vendor publishes new releases to npm without notice to its customers.
Requirements
- When the visitor has granted analytics consent (`consent.analytics` is `true`), the app loads the Pulse browser SDK (npm package `@pulse-analytics/browser`, file `dist/pulse.min.js`) from jsDelivr and calls `Pulse.init({ siteKey })` once per page, with the site key from `VITE_PULSE_SITE_KEY`. Without consent no Pulse script is added to the page.
- Rule for third-party scripts: each one is loaded with Subresource Integrity (an `integrity` hash and `crossorigin="anonymous"`) and runs only the release AppSec reviewed. For Pulse that release is 3.4.1, and the integrity value is AppSec's sha384 hash of 3.4.1's `dist/pulse.min.js`. Moving to a newer SDK is a PR that changes the version and the hash together.
- Analytics must not break the app: if the SDK fails to load, the dashboard works as before and a warning is logged to the console.
- Unchanged: the dashboard, the consent banner and the CSP. The loader has tests that run with the existing `npm test` (Vitest).
Files touched
- src/App.jsx
- src/analytics/AnalyticsLoader.jsx
- src/analytics/loadPulse.js
- src/analytics/loadPulse.test.jsx