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
Add exit page to sessions (#25)
Uku Taht94a20fb0a203
diff --git a/lib/plausible/ingest/session.ex b/lib/plausible/ingest/session.exindex 6b46c63cf9..cbc9dbb642 100644--- a/lib/plausible/ingest/session.ex+++ b/lib/plausible/ingest/session.ex@@ -24,13 +24,13 @@ defmodule Plausible.Ingest.Session dodef init(event) dotimer = Process.send_after(self(), :finalize, @session_timeout)- {:ok, %{first_event: event, timer: timer, is_bounce: true, last_unload: nil}}+ {:ok, %{first_event: event, last_event: event, timer: timer, is_bounce: true, last_unload: nil}}end- def handle_cast({:on_event, _event}, state) do+ def handle_cast({:on_event, event}, state) doProcess.cancel_timer(state[:timer])new_timer = Process.send_after(self(), :finalize, @session_timeout)- {:noreply, %{state | timer: new_timer, is_bounce: false, last_unload: nil}}+ {:noreply, %{state | timer: new_timer, last_event: event, is_bounce: false, last_unload: nil}}enddef handle_cast({:on_unload, timestamp}, state) do@@ -38,26 +38,28 @@ defmodule Plausible.Ingest.Session doenddef handle_info(:finalize, state) do- event = state[:first_event]+ first_event = state[:first_event]+ last_event = state[:last_event]- if !is_potential_leftover?(event) do+ if !is_potential_leftover?(first_event) dolength = if state[:last_unload] do- Timex.diff(state[:last_unload], event.timestamp, :seconds)+ Timex.diff(state[:last_unload], first_event.timestamp, :seconds)endPlausible.Session.changeset(%Plausible.Session{}, %{- hostname: event.hostname,- user_id: event.user_id,- new_visitor: event.new_visitor,- entry_page: event.pathname,+ hostname: first_event.hostname,+ user_id: first_event.user_id,+ new_visitor: first_event.new_visitor,+ entry_page: first_event.pathname,+ exit_page: last_event.pathname,is_bounce: state[:is_bounce],length: length,- referrer: event.referrer,- referrer_source: event.referrer_source,- country_code: event.country_code,- operating_system: event.operating_system,- browser: event.browser,- start: event.timestamp+ referrer: first_event.referrer,+ referrer_source: first_event.referrer_source,+ country_code: first_event.country_code,+ operating_system: first_event.operating_system,+ browser: first_event.browser,+ start: first_event.timestamp}) |> Repo.insert!enddiff --git a/lib/plausible/session/schema.ex b/lib/plausible/session/schema.exindex 7f1f54102d..ce2d108f21 100644--- a/lib/plausible/session/schema.ex+++ b/lib/plausible/session/schema.ex@@ -11,6 +11,7 @@ defmodule Plausible.Session dofield :length, :integerfield :is_bounce, :booleanfield :entry_page, :string+ field :exit_page, :stringfield :referrer, :stringfield :referrer_source, :string@@ -24,7 +25,7 @@ defmodule Plausible.Session dodef changeset(session, attrs) dosession- |> cast(attrs, [:hostname, :entry_page, :referrer, :new_visitor, :user_id, :start, :length, :is_bounce, :operating_system, :browser, :referrer_source, :country_code, :screen_size])+ |> cast(attrs, [:hostname, :entry_page, :exit_page, :referrer, :new_visitor, :user_id, :start, :length, :is_bounce, :operating_system, :browser, :referrer_source, :country_code, :screen_size])|> validate_required([:hostname, :new_visitor, :user_id, :is_bounce, :start])endenddiff --git a/priv/repo/migrations/20200113143927_add_exit_page_to_session.exs b/priv/repo/migrations/20200113143927_add_exit_page_to_session.exsnew file mode 100644index 0000000000..fae3a9c3f9--- /dev/null+++ b/priv/repo/migrations/20200113143927_add_exit_page_to_session.exs@@ -0,0 +1,9 @@+defmodule Plausible.Repo.Migrations.AddExitPageToSession do+ use Ecto.Migration++ def change do+ alter table(:sessions) do+ add :exit_page, :text+ end+ end+enddiff --git a/test/plausible/ingest/session_test.exs b/test/plausible/ingest/session_test.exsindex c8f155c948..e8c3de7777 100644--- a/test/plausible/ingest/session_test.exs+++ b/test/plausible/ingest/session_test.exs@@ -53,6 +53,17 @@ defmodule Plausible.Ingest.SessionTest dosession = capture_session(pageview.user_id)refute session.is_bounceend++ test "captures the exit page" do+ pageview = insert(:pageview)+ pageview2 = insert(:pageview, user_id: pageview.user_id, pathname: "/exit")++ Ingest.Session.on_event(pageview)+ Ingest.Session.on_event(pageview2)++ session = capture_session(pageview.user_id)+ assert session.exit_page == "/exit"+ endenddescribe "on_unload/1" do