NGU-Web/docs/analytics-reports.md
Zaldimmar aaab1a945c Docs: setting up the Plausible reports to Discord
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-04 21:46:32 -05:00

5.6 KiB

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:

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):

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:

sudo systemctl edit ngu-api
[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:

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:

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:

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.