Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Prevent user id collisions in session tracking (#744)
Uku Taht8a455dd191bf
diff --git a/lib/plausible/session/store.ex b/lib/plausible/session/store.exindex ae48d02408..b74c2c130c 100644--- a/lib/plausible/session/store.ex+++ b/lib/plausible/session/store.ex@@ -30,7 +30,7 @@ defmodule Plausible.Session.Store doon: s.session_id == ls.session_id and s.timestamp == ls.timestamp,order_by: s.timestamp)- |> Enum.map(fn s -> {s.user_id, s} end)+ |> Enum.map(fn s -> {{s.domain, s.user_id}, s} end)|> Enum.into(%{})rescue_e -> %{}@@ -48,7 +48,11 @@ defmodule Plausible.Session.Store do_from,%{sessions: sessions, buffer: buffer} = state) do- found_session = sessions[event.user_id] || (prev_user_id && sessions[prev_user_id])+ session_key = {event.domain, event.user_id}++ found_session =+ sessions[session_key] || (prev_user_id && sessions[{event.domain, prev_user_id}])+active = is_active?(found_session, event)updated_sessions =@@ -56,40 +60,41 @@ defmodule Plausible.Session.Store dofound_session && active ->new_session = update_session(found_session, event)buffer.insert([%{new_session | sign: 1}, %{found_session | sign: -1}])- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)found_session && !active ->new_session = new_session_from_event(event)buffer.insert([new_session])- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)true ->new_session = new_session_from_event(event)buffer.insert([new_session])- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)end- session_id = updated_sessions[event.user_id].session_id+ session_id = updated_sessions[session_key].session_id{:reply, session_id, %{state | sessions: updated_sessions}}enddef reconcile_event(sessions, event) do- found_session = sessions[event.user_id]+ session_key = {event.domain, event.user_id}+ found_session = sessions[session_key]active = is_active?(found_session, event)updated_sessions =cond dofound_session && active ->new_session = update_session(found_session, event)- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)found_session && !active ->new_session = new_session_from_event(event)- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)true ->new_session = new_session_from_event(event)- Map.put(sessions, event.user_id, new_session)+ Map.put(sessions, session_key, new_session)endupdated_sessionsdiff --git a/test/support/test_utils.ex b/test/support/test_utils.exindex 6b4c7706da..f306c7e008 100644--- a/test/support/test_utils.ex+++ b/test/support/test_utils.ex@@ -87,7 +87,7 @@ defmodule Plausible.TestUtils doevents =Enum.map(events, fn event ->- Map.put(event, :session_id, sessions[event.user_id].session_id)+ Map.put(event, :session_id, sessions[{event.domain, event.user_id}].session_id)end)Plausible.ClickhouseRepo.insert_all(