Tracking time users spent in the channel's room

Hello, I’ve implemented separated rooms for events using phoenix channels.
I need to calculate how much time each user spent in the room.
First quick solution was to write records to database on each join/terminate handlers in the channel’s handler.
It will be like:
“join”, action_at
“left”, action_at
“join”, action_at
“left”, action_at

And than calculate diffs between pairs left-join in second and summarize it later…
Possible problem:

  1. any fails on left or join could create gaps
    “join”, “join” → “left”-> “join” → “left” → “left”…
    It could be solved by keeping only first record in chain.
  2. Too much writes to database as a potential bottleneck. We can go with this solution and turn on optionally for some events to prove a concept and that this feature are ok, but I would like to find better way to do that w/o real-time updates of db.

I thought about separated process/genserver that will colllect all of that info and will save it once event finished.
I am not sure, how to keep data safe if genserver fails.
Another idea was to use some no-sql key-value storage and after completion event → aggregate data and save somewhere in participants table at field “time_online”.

I need an advice/help how to achieve what I want.