Skip to content
Plausible Analytics · Chapter 4 of 14 Day 2: Under the Hood

Day 2: Under the Hood

Goal: You send pageviews to Plausible by hand, follow them all the way into ClickHouse, recognize dropped events by a header and understand how requests turn into visitors and sessions. · Duration: approx. 60 min · Prerequisite: Day 1

Briefing

  • The tracking script does nothing mysterious: for every pageview it sends a POST to /api/event with name, URL and domain. You can do that with curl too.
  • Plausible almost always answers with 202 Accepted – even when it drops the event. Whether it was counted is revealed by the header x-plausible-dropped.
  • Storage happens in ClickHouse: one row per event in events_v2, one row per visit in sessions_v2. Plausible buffers and writes every few seconds.
  • A visitor (user_id) is a number that Plausible computes from IP address, user agent, domain and a salt that changes daily. Plausible assigns a new session (session_id) on a visitor’s first event. The IP itself is not stored (Day 7).
 Browser/curl ── POST /api/event {name, url, domain} ──► plausible
                                                          │ domain known? bot?
                         202 + x-plausible-dropped: 1 ◄───┤ no → dropped
                                                          │ yes → buffer (~3 s)
                          events_v2  (1 row per event)   ◄┤
                          sessions_v2 (1 row per visit)  ◄┘  ClickHouse

Drill 1: Two databases

# on argo
cd ~/plausible/ce
docker compose exec -T plausible_db psql -U postgres -d plausible_db \
  -tAc "select count(*) from information_schema.tables where table_schema='public'"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "show tables" | grep -E '^(events|sessions)'

Expected:

50
events
events_v2
sessions
sessions_v2
sessions_v2_tmp_versioned

50 tables in PostgreSQL for accounts, sites, goals, invitations, background jobs. In ClickHouse only events_v2 and sessions_v2 count; events and sessions are legacy from version 1, plus there are tables for imported data (Day 6).

Save your progress? Sign up for free

Drill 2: A pageview by hand

A browser user agent and a made-up IP from the documentation range 203.0.113.0/24:

# on argo
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0"
curl -s -w ' %{http_code}\n' -X POST http://plausible.lab:8000/api/event \
  -H "User-Agent: $UA" -H 'X-Forwarded-For: 203.0.113.10' \
  -H 'Content-Type: application/json' \
  -d '{"name":"pageview","url":"http://itdrills.com/books/","domain":"itdrills.com"}'
sleep 5
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select name, hostname, pathname, user_id from events_v2 format TSV"

Expected:

ok 202
pageview    itdrills.com    /books/ 17507384468120316438

ok and 202 only mean: accepted. Only the row in events_v2 proves that the event was counted. Your user_id will be a different one.

Save your progress? Sign up for free

Drill 3: What Plausible drops

Three requests that do not count – two of them with the same friendly answer.

# on argo
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0"
curl -s -i -X POST http://plausible.lab:8000/api/event -H "User-Agent: $UA" \
  -H 'Content-Type: application/json' \
  -d '{"name":"pageview","url":"http://falsch.example/","domain":"falsch.example"}' \
  | grep -iE '^HTTP|dropped'
curl -s -i -X POST http://plausible.lab:8000/api/event \
  -H 'User-Agent: Googlebot/2.1 (+http://www.google.com/bot.html)' \
  -H 'Content-Type: application/json' \
  -d '{"name":"pageview","url":"http://itdrills.com/","domain":"itdrills.com"}' \
  | grep -iE '^HTTP|dropped'
curl -s -w ' %{http_code}\n' -X POST http://plausible.lab:8000/api/event \
  -H "User-Agent: $UA" -H 'Content-Type: application/json' -d '{"name":"pageview"}'

Expected:

HTTP/1.1 202 Accepted
x-plausible-dropped: 1
HTTP/1.1 202 Accepted
x-plausible-dropped: 1
{"errors":{"domain":["can't be blank"],"url":["is required"]}} 400

An unknown domain (falsch.example, “wrong.example”) and a known bot are dropped silently – the browser is not supposed to see an error. Only broken requests get 400 with a reason. If you are debugging “why does nothing count?”, look at x-plausible-dropped first.

Save your progress? Sign up for free

Drill 4: What Plausible does not drop

# on argo
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0"
curl -s -i -X POST http://plausible.lab:8000/api/event \
  -H 'Content-Type: application/json' \
  -d '{"name":"pageview","url":"http://itdrills.com/curl/","domain":"itdrills.com"}' \
  | grep -ciE 'dropped'
D='{"name":"pageview","url":"http://www.itdrills.com/www/","domain":"www.itdrills.com"}'
curl -s -i -X POST http://plausible.lab:8000/api/event -H "User-Agent: $UA" \
  -H 'Content-Type: application/json' -d "$D" | grep -ciE 'dropped'
sleep 5
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select hostname, pathname from events_v2 where pathname in ('/curl/','/www/')
           order by pathname format TSV"

Expected:

0
0
itdrills.com    /curl/
itdrills.com    /www/

Two surprises: curl with its default user agent does not count as a bot. A monitoring tool that fetches pages with curl still does not count – it does not run JavaScript. A curl straight to /api/event, on the other hand, counts just fine. And www. gets stripped: to Plausible, www.itdrills.com and itdrills.com are the same site.

Save your progress? Sign up for free

Drill 5: Visitors and sessions

Two more requests: one from the same “IP” as in Drill 2, one from a new one.

# on argo
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0"
D='{"name":"pageview","url":"http://itdrills.com/books/sops/","domain":"itdrills.com"}'
for ip in 203.0.113.10 203.0.113.11; do
  curl -s -o /dev/null -X POST http://plausible.lab:8000/api/event \
    -H "User-Agent: $UA" -H "X-Forwarded-For: $ip" \
    -H 'Content-Type: application/json' -d "$D"
done
sleep 5
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select user_id, count() as aufrufe from events_v2
           where pathname like '/books%' group by user_id
           order by aufrufe desc format TSV"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select entry_page, exit_page, pageviews from sessions_v2 final
           where entry_page like '/books%' order by pageviews desc format TSV"

Expected:

17507384468120316438    2
14414375569308133861    1
/books/ /books/sops/    2
/books/sops/    /books/sops/    1

Same IP, same user agent, same day → the same visitor, the same session with entry /books/ and exit /books/sops/. The new IP is a new visitor. (The column alias aufrufe means “views”.) FINAL collapses the intermediate states of the session table (it is updated with every event).

Warning: Plausible accepts X-Forwarded-For without checking it. Anyone who sends events directly to Plausible can create as many “visitors” as they like this way. Behind a proxy, this header must come from the proxy (Day 4).

Save your progress? Sign up for free

Drill 6: The buffer

# on argo
cd ~/plausible/ce
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0"
V=$(docker compose exec -T plausible_events_db \
  clickhouse-client -d plausible_events_db --query "select count() from events_v2")
curl -s -o /dev/null -X POST http://plausible.lab:8000/api/event -H "User-Agent: $UA" \
  -H 'Content-Type: application/json' \
  -d '{"name":"pageview","url":"http://itdrills.com/puffer/","domain":"itdrills.com"}'
for t in 0 2 4 6; do
  echo "t=$t: $(docker compose exec -T plausible_events_db clickhouse-client \
    -d plausible_events_db --query 'select count() from events_v2') (vorher $V)"
  sleep 2
done

Expected:

t=0: 5 (vorher 5)
t=2: 6 (vorher 5)
t=4: 6 (vorher 5)
t=6: 6 (vorher 5)

vorher means “before”. Plausible collects events in memory and writes them in batches – ClickHouse prefers a few large inserts over many small ones. A crash during these seconds costs the buffered events; for web analytics that is acceptable.

Save your progress? Sign up for free

Exercise: Speed Drills

  1. Count all stored pageviews. Target time: 20 s
  2. Show the three most frequent pages. Target time: 30 s
  3. Send a request for /coming-soon/ and check whether it was dropped. Target time: 40 s

Solution

# on argo
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count() from events_v2 where name='pageview'"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select pathname, count() c from events_v2 group by pathname
           order by c desc, pathname limit 3 format TSV"
D='{"name":"pageview","url":"http://itdrills.com/coming-soon/","domain":"itdrills.com"}'
curl -s -i -X POST http://plausible.lab:8000/api/event -H 'User-Agent: Mozilla/5.0' \
  -H 'Content-Type: application/json' -d "$D" | grep -ciE 'dropped'

Expected:

6
/books/sops/    2
/books/ 1
/curl/  1
0

Save your progress? Sign up for free

Checkpoint

# on argo
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select uniqExact(user_id) from events_v2"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count() from events_v2 where hostname != 'itdrills.com'"

Expected:

4
0

Challenge

A monitoring tool calls /api/event every five minutes to check whether Plausible is alive – and skews the numbers in the process. Find a way to check this without storing an event, and prove it with events_v2.

Hint: Day 1 knows an endpoint that exists exactly for this – and Drill 3 a request that is accepted but never stored.

Solution

# on argo
cd ~/plausible/ce
V=$(docker compose exec -T plausible_events_db \
  clickhouse-client -d plausible_events_db --query "select count() from events_v2")
curl -s -o /dev/null -w '%{http_code}\n' http://plausible.lab:8000/api/health
sleep 5
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count() - $V from events_v2"

Expected:

200
0

/api/health checks the application, PostgreSQL and ClickHouse and stores nothing. If you absolutely want to test that events are accepted, send an event for a domain that does not exist – 202 with x-plausible-dropped: 1, no row.

Save your progress? Sign up for free

Cleanup

The test events from this day skew the statistics for itdrills.com. Delete them – that only works via SQL; the UI has no delete function for individual days.

# on argo
cd ~/plausible/ce
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "truncate table events_v2"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "truncate table sessions_v2"
docker compose exec -T plausible_events_db clickhouse-client -d plausible_events_db \
  --query "select count() from events_v2"

Expected:

0

Warning: truncate deletes all events of all sites without asking. Intended in the lab, never in production without a backup (Day 8).

Interview Note

  • How can you tell that Plausible dropped an event? 202 with x-plausible-dropped: 1 – not by the status code.
  • How does Plausible count visitors without cookies? A hash of IP, user agent, domain and a daily salt; only the hash is stored.
  • Why FINAL on sessions_v2? The table is updated per event (VersionedCollapsingMergeTree); FINAL returns the collapsed state.

Enterprise Note

  • /api/event must be publicly reachable – /api/health, the dashboard and the databases must not. Expose them separately on the reverse proxy (Day 4).
  • Event forgery (X-Forwarded-For, arbitrary user agents) is possible with any client-side analytics. For metrics with monetary value, count on the server side.
  • Raw data in ClickHouse is the big advantage of CE: your own analyses, export to a data warehouse, reconciliation with server logs.

Contents

More