}
diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex
index 215e9fe89..f707de5a2 100644
--- a/lib/plausible/stats/clickhouse.ex
+++ b/lib/plausible/stats/clickhouse.ex
@@ -171,14 +171,8 @@ defmodule Plausible.Stats.Clickhouse do
def top_referrers_for_goal(site, query, limit, page) do
offset = (page - 1) * limit
- converted_sessions =
- from(e in base_query(site, query),
- select: %{session_id: e.session_id})
-
ClickhouseRepo.all(
- from s in Plausible.ClickhouseSession,
- join: cs in subquery(converted_sessions),
- on: s.session_id == cs.session_id,
+ from s in base_query_w_sessions(site, query),
where: s.referrer_source != "",
group_by: s.referrer_source,
order_by: [desc: fragment("count")],
@@ -611,7 +605,7 @@ defmodule Plausible.Stats.Clickhouse do
%{goal => [key]}
else
ClickhouseRepo.all(
- from [e, meta] in base_query_w_sessions_bare(site, query),
+ from [e, meta: meta] in base_query_w_sessions_bare(site, query),
select: {e.name, meta.key},
distinct: true
) |> Enum.reduce(%{}, fn {goal_name, meta_key}, acc ->
@@ -647,7 +641,7 @@ defmodule Plausible.Stats.Clickhouse do
)
else
ClickhouseRepo.all(
- from [e, meta] in base_query_w_sessions(site, query),
+ from [e, meta: meta] in base_query_w_sessions(site, query),
group_by: meta.value,
order_by: [desc: fragment("count")],
select: %{
@@ -857,7 +851,7 @@ defmodule Plausible.Stats.Clickhouse do
q
end
- if query.filters["page"] do
+ q = if query.filters["page"] do
page = query.filters["page"]
from(e in q, where: e.pathname == ^page)
else
@@ -876,6 +870,7 @@ defmodule Plausible.Stats.Clickhouse do
from(
e in q,
inner_lateral_join: meta in fragment("meta as m"),
+ as: :meta,
where: meta.key == ^key and meta.value == ^val,
)
end
@@ -1075,6 +1070,26 @@ defmodule Plausible.Stats.Clickhouse do
q
end
+ q = if query.filters["meta"] do
+ [{key, val}] = query.filters["meta"] |> Enum.into([])
+
+ if val == "(none)" do
+ from(
+ e in q,
+ where: fragment("not has(meta.key, ?)", ^key)
+ )
+ else
+ from(
+ e in q,
+ inner_lateral_join: meta in fragment("meta as m"),
+ where: meta.key == ^key and meta.value == ^val,
+ )
+ end
+ else
+ q
+ end
+
+
q =
if path do
from(e in q, where: e.pathname == ^path)