How to store ‘last seen’ for users in Phoenix
This week, I worked on some under-the-hood improvements to Plausible to give me better insights into my userbase. One of these was to store a last_seen
timestamp for all users. This is a private piece of data that I use to determine:
- How many users are actively logging on and checking their analytics
- What is the average usage frequency of Plausible?
- When should I consider an account as ‘rotting’? Meaning I’m about to lose them as a user/customer
Let’s see how this can be achieved in Phoenix. First, we’ll start with the schema.
Schema
We’ll need to generate a migration for the last_seen
column:
$ mix ecto.gen.migration add_last_seen_to_users
and the migration itself:
defmodule Plausible.Repo.Migrations.AddLastSeenToUsers do
use Ecto.Migration
def change do
alter table(:users) do
add :last_seen, :naive_datetime, default: fragment("now()")
end
end
end
At...