v1.13.1-data: links/redirects, captions, partner picks, Plausible reports, migration baseline #28

Merged
ngu-git-admin merged 16 commits from data into main 2026-10-06 10:10:57 +01:00
3 changed files with 165 additions and 1 deletions
Showing only changes of commit aaab1a945c - Show all commits

View file

@ -32,7 +32,7 @@ In dev, Vite proxies `/api` to the target set in `vite.config.ts`, so the API mu
- Debugging: check `journalctl -u ngu-api -n 40 --no-pager` first. Make sure rsync ran from the repo (not the deployed copy) before restarting the service. - Debugging: check `journalctl -u ngu-api -n 40 --no-pager` first. Make sure rsync ran from the repo (not the deployed copy) before restarting the service.
- Never run commands against the production server or database unless explicitly asked. - Never run commands against the production server or database unless explicitly asked.
- Uploaded media: `UPLOADS_DIR=/var/lib/ngu/uploads`, served by nginx at `/uploads/`. Setup and the nginx block are in `docs/media-uploads.md`. - Uploaded media: `UPLOADS_DIR=/var/lib/ngu/uploads`, served by nginx at `/uploads/`. Setup and the nginx block are in `docs/media-uploads.md`.
- Analytics: self-hosted Plausible, proxied by nginx at `/pa/`. The API adds its script to every page (`analyticsTag` in `server/src/pageMeta.js`), counting under the hostname of `site.json`'s `url`, so each deployment reports as itself. Nothing in `index.html` or the app. Daily and weekly reports go to Discord when Panel → Site settings → Reports turns them on and `PLAUSIBLE_API_KEY` and `DISCORD_WEBHOOK_URL` are set (`server/src/analyticsReport.js`). - Analytics: self-hosted Plausible, proxied by nginx at `/pa/`. The API adds its script to every page (`analyticsTag` in `server/src/pageMeta.js`), counting under the hostname of `site.json`'s `url`, so each deployment reports as itself. Nothing in `index.html` or the app. Daily and weekly reports go to Discord when Panel → Site settings → Reports turns them on and `PLAUSIBLE_API_KEY` and `DISCORD_WEBHOOK_URL` are set (`server/src/analyticsReport.js`): `docs/analytics-reports.md`.
- Page HTML goes through the API, which fills in each page's title, description, preview image and structured data (`server/src/pageMeta.js`), and serves `/sitemap.xml`. It reads `SITE_CONFIG` (site.json) and `SITE_INDEX` (the built index.html), with Panel → Site settings (the `site_settings` table) laid over site.json, and serves `/robots.txt` from the same merge. nginx setup: `docs/seo.md`. - Page HTML goes through the API, which fills in each page's title, description, preview image and structured data (`server/src/pageMeta.js`), and serves `/sitemap.xml`. It reads `SITE_CONFIG` (site.json) and `SITE_INDEX` (the built index.html), with Panel → Site settings (the `site_settings` table) laid over site.json, and serves `/robots.txt` from the same merge. nginx setup: `docs/seo.md`.
- Video clips (Tools, admins and up): yt-dlp downloads our YouTube/Facebook videos into `CLIPS_DIR`, ffmpeg cuts them; one job at a time, files swept after a day (`server/src/clips.js`). Needs yt-dlp and ffmpeg on the VPS: `docs/video-clips.md`. - Video clips (Tools, admins and up): yt-dlp downloads our YouTube/Facebook videos into `CLIPS_DIR`, ffmpeg cuts them; one job at a time, files swept after a day (`server/src/clips.js`). Needs yt-dlp and ffmpeg on the VPS: `docs/video-clips.md`.
- Newsletter countdown pictures: `/api/countdown/:id.png` (`server/src/routes/countdown.js`), drawn with sharp when an email is opened; it uses the VPS's fonts, so install Poppins there (eg. into `/usr/local/share/fonts`, then `fc-cache`) or it falls back to a plain sans. The social bar's icons are static files in `public/email/`. - Newsletter countdown pictures: `/api/countdown/:id.png` (`server/src/routes/countdown.js`), drawn with sharp when an email is opened; it uses the VPS's fonts, so install Poppins there (eg. into `/usr/local/share/fonts`, then `fc-cache`) or it falls back to a plain sans. The social bar's icons are static files in `public/email/`.

162
docs/analytics-reports.md Normal file
View file

@ -0,0 +1,162 @@
# Analytics reports to Discord
The API can post the site's Plausible numbers to a Discord channel
(`server/src/analyticsReport.js`, migration 065):
- **Daily**: yesterday's visitors, pageviews, bounce rate, visit length,
and the top 3 pages and sources.
- **Weekly**, on Mondays: the last 7 days (Monday to Sunday), each
total with its change from the week before, and the top 5 pages and
sources.
Each one has its own switch in Panel → Site settings → Reports, and both
are off until it's turned on. They go out at 8:00 in `REPORT_TZ`. If the
server is down then, they go out when it comes back up later that day.
A restart never posts the same report twice. Nothing else on the site
depends on this.
| Setting on the `ngu-api` service | What it is |
| --- | --- |
| `PLAUSIBLE_API_KEY` | A Plausible Stats API key (step 1) |
| `DISCORD_WEBHOOK_URL` | The channel's webhook address (step 2) |
| `PLAUSIBLE_URL` | Where the API can reach Plausible. Default `http://127.0.0.1:8000` |
| `REPORT_TZ` | The time zone for "yesterday" and 8:00. Default `America/Chicago` |
Without the first two, no reports are sent, whatever the switches say.
## 1. The Plausible API key
1. Sign in to Plausible (the self-hosted one behind `/pa/`) as an owner
of the site.
2. Account settings → API keys → New API key. Pick **Stats API** if it
asks for a type, and name it eg. `ngu-api reports`.
3. Copy the key now. Plausible shows it only once.
The site has to exist in Plausible under the same domain the pages count
under: the hostname of `site.json`'s `url`, eg. `nextgenerationofunity.org`
(not `www.` and not `https://`). It already does if the dashboard shows
visits.
The reports use the Stats API v2 (`/api/v2/query`), which needs
Plausible Community Edition 2.1 or later. Check Settings or the page
footer for the version, and upgrade an older one first.
### Where Plausible listens
`PLAUSIBLE_URL` is the address the API calls directly, not the public
`/pa/` path. nginx's proxy for `/pa/` already points at it:
```sh
grep proxy_pass /etc/nginx/snippets/plausible-proxy.conf
```
If that shows `http://127.0.0.1:8000`, leave `PLAUSIBLE_URL` unset.
Otherwise set it to that address, eg. `http://127.0.0.1:8001`, or
`https://plausible.io` for Plausible's own hosting.
## 2. The Discord webhook
1. In Discord, open the channel's settings (the gear by its name) →
Integrations → Webhooks → New Webhook.
2. Name it eg. `NGU analytics`, give it an avatar if you like, and check
that it posts to the right channel.
3. Copy Webhook URL. It looks like
`https://discord.com/api/webhooks/123456789/AbCdEf...`.
You need Manage Webhooks on the channel. Anyone who has the address can
post to the channel, so treat it as a password.
## 3. The service
Both values are secrets, so put them in a file only root can read
rather than in the unit itself (a `systemctl edit` override is readable
by every user on the box):
```sh
sudo mkdir -p /etc/ngu
sudo tee /etc/ngu/analytics-reports.env >/dev/null <<'EOF'
PLAUSIBLE_API_KEY=<the key from step 1>
DISCORD_WEBHOOK_URL=<the webhook address from step 2>
EOF
sudo chmod 600 /etc/ngu/analytics-reports.env
```
Add `PLAUSIBLE_URL=` or `REPORT_TZ=` lines to the same file if you need
them. Then point the service at it:
```sh
sudo systemctl edit ngu-api
```
```ini
[Service]
EnvironmentFile=/etc/ngu/analytics-reports.env
```
systemd reads the file as root before it starts the service, so the
service's own user doesn't need to be able to read it.
Back up the database first (migration 065 adds the switches and runs at
the next start), then:
```sh
sudo systemctl restart ngu-api
```
No nginx change is needed: the API calls Plausible and Discord itself.
## 4. Try it
Send one straight away, whatever the switches say, while the service is
running:
```sh
cd /srv/ngu-api
sudo bash -c 'set -a; . /etc/ngu/analytics-reports.env; set +a;
SITE_CONFIG=/srv/ngu-api/site.json DB_PATH=/var/lib/ngu/ngu.db \
node src/analyticsReport.js daily'
```
`Sent the daily report.` means it's in the channel. Use `weekly` for the
weekly one. Sending one by hand doesn't stop that day's scheduled
report.
## 5. Turn them on
Panel → Site settings → Reports: set **Daily analytics report** and/or
**Weekly analytics report** to On, and save. Set one back to Off (or
blank) to stop it. The change counts from the next check, within 15
minutes.
The switches are per database, so the dev server only posts if its own
database has them on and its own service has the settings. Leave the
settings off dev, or it reports as `dev.nextgenerationofunity.org`.
## Checking it
Every report is logged:
```sh
journalctl -u ngu-api --since today --no-pager | grep analytics
```
- `analytics daily report sent for 2026-10-05`: it went out.
- `Plausible answered 401`: the API key is wrong or was deleted. Make a
new one (step 1).
- `Plausible answered 400` or `404` naming the site: Plausible has no
site under that domain. Compare `site.json`'s `url` with the domain in
Plausible's site list.
- `Plausible answered 404` with no site named: Plausible is older than
2.1 and has no v2 API.
- `fetch failed` or `ECONNREFUSED`: Plausible isn't at `PLAUSIBLE_URL`
(see Where Plausible listens).
- `Discord answered 401` or `404`: the webhook was deleted or the
address is wrong. Make a new one (step 2).
- Nothing at all: check that both settings are in the service's
environment (`sudo systemctl show ngu-api -p EnvironmentFiles`), that
the switch is On, that it's past 8:00 in `REPORT_TZ`, and for the
weekly report, that it's Monday.
A failed report isn't retried that day, so a broken setup logs one line
a day, not one every 15 minutes. Fix the cause, then send it by hand
(step 4) if you still want it.

View file

@ -23,6 +23,8 @@
To send one now (eg. to try the setup), whatever the switches say: To send one now (eg. to try the setup), whatever the switches say:
DB_PATH=/var/lib/ngu/ngu.db node src/analyticsReport.js daily|weekly DB_PATH=/var/lib/ngu/ngu.db node src/analyticsReport.js daily|weekly
Setup: docs/analytics-reports.md.
═══════════════════════════════════════════════════════════════ */ ═══════════════════════════════════════════════════════════════ */
import { pathToFileURL } from "node:url"; import { pathToFileURL } from "node:url";