↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Homemade analytics with Ecto and Elixir

The article explains a small analytics system built for the Dashbit website. It avoids cookies, stores page access counts by date and path, and uses Ecto upserts to increment counters when the same page is accessed again.

Reading notes
#

  • The goal is to avoid tracking users as much as possible, so the site uses no cookies.
  • The system still records which pages are frequently accessed.
  • Each access is stored by date and path, with the date and path forming a composite primary key.
  • An Ecto upsert increments the counter when a matching row already exists.
  • The first version depends on the database accepting every write.
  • To reduce pressure on the database, writes move to separate Elixir processes.
  • A worker process collects page visits and writes them after a delay.
  • The worker traps exits so it can flush pending writes on shutdown.
  • The delay is randomized between 10 and 20 seconds to avoid many processes writing at once.
  • A Registry maps each page path to its worker process.
  • A DynamicSupervisor starts workers on demand.
  • If two requests race to start the same worker, the code handles the already-started case.
  • The worker registers itself with the Registry when it starts.
  • The bump function is called after a successful 200 response, using the request path.
  • The article notes that with many pages, the design could be changed so workers stop after each upsert.