ARLTS has a public "impact board" at arlts.org/impact: how many people have visited, and how many were active today, this week, and this month. The numbers come from our analytics tool (PostHog). The board used to update once an hour. Now the user counts update every 5 minutes, and it costs nothing extra.

How it worked

A timer in the database (pg_cron) calls a little cloud function once an hour. The function asks PostHog about 10 questions (visitors, page views, videos played, and more) and writes the answers to a table. The page reads from that table, never from PostHog.

Why not run the whole thing every 5 minutes?

A few of those 10 questions are heavy: they count over all of our history, and one has hit PostHog's time limit before. Running all 10 every 5 minutes would be 12 times the load for numbers that barely move.

The fix: a quick mode

The function now takes ?quick=1. In quick mode it asks only the 2 light questions, and that's it:

-- all-time visitors select count(distinct person_id) from events where event = '$pageview' -- active today, this week, this month select count(distinct if(timestamp >= now() - interval 1 day, person_id, null)), count(distinct if(timestamp >= now() - interval 7 day, person_id, null)), count(distinct if(timestamp >= now() - interval 30 day, person_id, null)) from events where event = '$pageview' and timestamp >= now() - interval 30 day 

Another timer calls it every 5 minutes, except on the hour, when the full run already does it:

select cron.schedule('stats-quick', '5-55/5 * * * *', $$ select net.http_post(url := '<your function URL>?quick=1', ...); $$); 

Two details worth copying

  • Totals never go down. An all-time total can only go up. If the analytics tool comes back with a lower number (ad blockers, a timeout), we keep the higher one we already have.
  • A failed question doesn't wipe the number. If one question fails, that number keeps its last value and the rest update as usual.

What it costs

About 8,000 extra function calls a month, against a free allowance of 500,000, and 2 light analytics questions each time. With the 5-minute cache in front of the page, a new visitor appears on the board within about 11 minutes, down from up to an hour.

The board is at https://arlts.org/impact.