Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ REAL_DATA_DIR=data pnpm vitest run src/lib/server/import/realData.test.ts # op
## What not to do

- Don't edit, rename, re-save or "clean" the spreadsheets. Don't commit them, fixtures derived from them, or SQLite caches.
- Don't add a chart library or third-party scripts/fonts without updating the CSP in `vite.config.ts` and this file. Map tiles (Esri World Imagery, OpenStreetMap) are the only allowed external images (`img-src`); a new tile host needs the CSP, the e2e tile stub in `e2e/pages.spec.ts` and the attribution.
- Don't add a chart library or third-party scripts/fonts without updating the CSP in `vite.config.ts` and this file. Allowed third parties: GA4 gtag (`www.googletagmanager.com` / `*.google-analytics.com` / `*.analytics.google.com`; Measurement ID `G-VZF5DR37YH`, loaded only on `data.physioshark.org` / `physioshark.org` via `GoogleAnalytics.svelte`) and map tiles (Esri World Imagery, OpenStreetMap — the only external images besides GA beacons; a new tile host needs the CSP, the e2e tile stub in `e2e/pages.spec.ts` and the attribution).
- Don't hard-code numbers in page copy; compute them in the load function.
- Don't claim causation from the correlations; don't overstate the human-pressure results (the source paper found no significant effect).
- Don't reintroduce dual-axis charts or per-point value labels.
Expand Down
2 changes: 2 additions & 0 deletions docs/DEPLOY.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ The container listens on `127.0.0.1:${HOST_PORT:-8098}` by default. Put it behin

Set `ORIGIN` in `.env` to the public URL if the proxy does not send `X-Forwarded-Proto`.

GA4 (`G-VZF5DR37YH`, shared Physioshark stream with physioshark.org) loads only when the request host is `data.physioshark.org` (or `physioshark.org`). Localhost and CI never send hits. Filter reports by **Page hostname** if you need data-site traffic alone.

## 3. Updating the data

- **New or corrected workbook:** copy it into `./data`. The app notices within a minute and rebuilds its cache; no restart needed. Check `/data-quality` afterwards.
Expand Down
4 changes: 4 additions & 0 deletions src/app.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
declare global {
/** package.json version, injected by Vite (`define` in vite.config.ts). */
const __APP_VERSION__: string;
interface Window {
dataLayer?: unknown[];
gtag?: (...args: unknown[]) => void;
}
namespace App {
// interface Error {}
// interface Locals {}
Expand Down
45 changes: 45 additions & 0 deletions src/lib/components/GoogleAnalytics.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<script lang="ts">
/**
* GA4 for the Physioshark web stream (G-VZF5DR37YH), shared with physioshark.org.
* Enabled only when the request host is a production Physioshark hostname so
* localhost / CI / preview never send fixture traffic.
*/
import { afterNavigate } from '$app/navigation';
import { page } from '$app/state';

const MEASUREMENT_ID = 'G-VZF5DR37YH';

let { enabled }: { enabled: boolean } = $props();

// The usual inline gtag snippet can't run here: the CSP only lets SvelteKit's own nonce'd
// inline scripts through. So the command queue is set up from this bundled code instead,
// before gtag.js (an allowed external script) arrives and drains it.
function setup() {
if (window.gtag) return;
window.dataLayer = window.dataLayer || [];
window.gtag = function gtag() {
// gtag.js only reads Arguments objects from the queue, not arrays.
window.dataLayer!.push(arguments);
};
window.gtag('js', new Date());
window.gtag('config', MEASUREMENT_ID, { send_page_view: false });
}

// Runs in the browser only: after hydration and after every client-side navigation.
afterNavigate(() => {
if (!enabled) return;
setup();
const url = page.url;
window.gtag!('event', 'page_view', {
page_path: `${url.pathname}${url.search}`,
page_location: url.href,
page_title: document.title
});
});
</script>

<svelte:head>
{#if enabled}
<script async src="https://www.googletagmanager.com/gtag/js?id={MEASUREMENT_ID}"></script>
{/if}
</svelte:head>
8 changes: 6 additions & 2 deletions src/routes/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { dataset } from '#lib/server/data';
import type { LayoutServerLoad } from './$types';

export const load: LayoutServerLoad = () => {
/** Hostnames that load GA4 (Physioshark stream G-VZF5DR37YH). */
const GA_HOSTS = new Set(['data.physioshark.org', 'physioshark.org']);

export const load: LayoutServerLoad = ({ url }) => {
const d = dataset();
return {
importedAt: d.meta.imported_at ?? null,
hasData: d.captures.length > 0,
seasonRange: d.seasons.length ? [d.seasons[0], d.seasons[d.seasons.length - 1]] : null,
external: d.meta.external ? (JSON.parse(d.meta.external) as Record<string, string | null>) : {},
version: d.meta.importer_version ?? ''
version: d.meta.importer_version ?? '',
ga: GA_HOSTS.has(url.hostname)
};
};
3 changes: 3 additions & 0 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { page } from '$app/state';
import { NAV } from '#lib/nav';
import { date, season } from '#lib/format';
import GoogleAnalytics from '#lib/components/GoogleAnalytics.svelte';
import PhysiosharkIcon from '#lib/components/PhysiosharkIcon.svelte';
import type { LayoutProps } from './$types';

Expand Down Expand Up @@ -56,6 +57,8 @@
);
</script>

<GoogleAnalytics enabled={data.ga} />

<svelte:head>
<title>{title}</title>
<meta name="description" content={description} />
Expand Down
16 changes: 12 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,31 @@ export default defineConfig({
},
adapter: adapter({ out: 'build' }),
// Per-request nonces for SSR pages; the pre-paint theme script in app.html carries
// %sveltekit.nonce%. No third-party scripts or fonts; map tiles are the only external images.
// %sveltekit.nonce%. Third-party: GA4 (gtag) and map tiles (images only).
csp: {
mode: 'auto',
directives: {
'default-src': ['self'],
'script-src': ['self'],
'script-src': ['self', 'https://www.googletagmanager.com'],
// Svelte `style:` directives use inline styles.
'style-src': ['self', 'unsafe-inline'],
// Map tiles for the interactive maps (#29): Esri imagery and OpenStreetMap.
// GA4 beacons may use image pings.
'img-src': [
'self',
'data:',
'https://server.arcgisonline.com',
'https://tile.openstreetmap.org'
'https://tile.openstreetmap.org',
'https://www.google-analytics.com',
'https://www.googletagmanager.com'
],
'font-src': ['self', 'data:'],
'connect-src': ['self'],
'connect-src': [
'self',
'https://*.google-analytics.com',
'https://*.analytics.google.com',
'https://*.googletagmanager.com'
],
'object-src': ['none'],
'base-uri': ['self'],
'form-action': ['self'],
Expand Down
Loading