Skip to content

Commit

Permalink
group by active visitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Apr 23, 2024
1 parent bb7c158 commit 21403a4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/plausible/stats/aggregate.ex
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,16 @@ defmodule Plausible.Stats.Aggregate do
),
timestamp: e.timestamp,
pathname: e.pathname,
session_id: e.session_id
user_id: e.user_id
}

timed_pages_q =
from e in Ecto.Query.subquery(windowed_pages_q),
group_by: [e.pathname, e.session_id],
group_by: [e.pathname, e.user_id],
where: ^Plausible.Stats.Base.dynamic_filter_condition(query, "event:page", :pathname),
where: e.next_timestamp != 0,
select: %{duration: sum(e.next_timestamp - e.timestamp)}
having: selected_as(:duration) > 0,
select: %{duration: selected_as(sum(e.next_timestamp - e.timestamp), :duration)}

time_on_page_q =
from e in Ecto.Query.subquery(timed_pages_q),
Expand Down
19 changes: 12 additions & 7 deletions lib/plausible/stats/breakdown.ex
Original file line number Diff line number Diff line change
Expand Up @@ -344,18 +344,22 @@ defmodule Plausible.Stats.Breakdown do
),
timestamp: e.timestamp,
pathname: e.pathname,
session_id: e.session_id
user_id: e.user_id
}

timed_pages_per_session_q =
timed_pages_per_active_visitor_q =
from e in subquery(windowed_pages_q),
group_by: [e.pathname, e.session_id],
group_by: [e.pathname, e.user_id],
where: e.pathname in ^pages,
where: e.next_timestamp != 0,
select: %{pathname: e.pathname, duration: sum(e.next_timestamp - e.timestamp)}
having: selected_as(:duration) > 0,
select: %{
pathname: e.pathname,
duration: selected_as(sum(e.next_timestamp - e.timestamp), :duration)
}

no_select_timed_pages_q =
from e in subquery(timed_pages_per_session_q),
from e in subquery(timed_pages_per_active_visitor_q),
group_by: e.pathname

timed_pages_q =
Expand All @@ -370,6 +374,7 @@ defmodule Plausible.Stats.Breakdown do
select: %{
page: i.page,
time_on_page: sum(i.time_on_page),
# TODO active visitors
visitors: sum(i.visitors)
}

Expand All @@ -378,7 +383,7 @@ defmodule Plausible.Stats.Breakdown do
select: %{
page: e.pathname,
time_on_page: sum(e.duration),
sessions: count(e.duration)
active_visitors: count(e.duration)
}

"timed_pages"
Expand All @@ -389,7 +394,7 @@ defmodule Plausible.Stats.Breakdown do
[t, i],
{
fragment("if(empty(?),?,?)", t.page, i.page, t.page),
(t.time_on_page + i.time_on_page) / (t.sessions + i.visitors)
(t.time_on_page + i.time_on_page) / (t.active_visitors + i.visitors)
}
)
else
Expand Down

0 comments on commit 21403a4

Please sign in to comment.