Runtime API for backfilling historical sessions.
Walks ga_events rows with no session_id, ordered by
{visitor_id, inserted_at, id}, builds ga_sessions, and stamps each event.
It is resumable because each batch only selects unstamped events.
For large historical tables, create a temporary partial index before the rollout and drop it after the backfill finishes:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_ga_events_session_backfill
ON good_analytics.ga_events (visitor_id, inserted_at, id)
WHERE session_id IS NULL;This module does not depend on Mix, so compiled production releases can call it from application code, a release task, or a remote console.
Summary
Functions
Runs batches until no eligible events remain.
Processes one batch of unstamped historical events.
Functions
@spec run(keyword()) :: {:ok, %{events: non_neg_integer(), sessions: non_neg_integer()}}
Runs batches until no eligible events remain.
Options:
:batch_size- maximum events per batch. Defaults to5000.:since- optionalDateTimelower bound forinserted_at.
Returns {:ok, %{events: total_stamped, sessions: total_created}}.
@spec run_batch(keyword()) :: {:ok, %{events: non_neg_integer(), sessions: non_neg_integer()}}
Processes one batch of unstamped historical events.