From ecbc68f05bbb4598ec7819b73a9b6dffb0258dee Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Mon, 16 Aug 2021 11:58:36 +0300 Subject: [PATCH] Fix entry pages --- assets/js/dashboard/stats/visitor-graph.js | 6 ++--- lib/plausible/stats/base.ex | 25 ------------------- lib/plausible/stats/breakdown.ex | 13 ++++++++++ lib/plausible/stats/filters.ex | 4 ++- .../controllers/api/stats_controller.ex | 4 +-- 5 files changed, 21 insertions(+), 31 deletions(-) diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js index ae60d43d5..c72b7a9a2 100644 --- a/assets/js/dashboard/stats/visitor-graph.js +++ b/assets/js/dashboard/stats/visitor-graph.js @@ -270,10 +270,10 @@ class LineGraph extends React.Component { } topStatTooltip(stat) { - if (typeof(stat.count) == 'number') { + if (typeof(stat.value) == 'number') { let name = stat.name.toLowerCase() - name = stat.count === 1 ? name.slice(0, -1) : name - return stat.count.toLocaleString() + ' ' + name + name = stat.value === 1 ? name.slice(0, -1) : name + return stat.value.toLocaleString() + ' ' + name } } diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex index e4746537f..38d5eac6c 100644 --- a/lib/plausible/stats/base.ex +++ b/lib/plausible/stats/base.ex @@ -73,9 +73,6 @@ defmodule Plausible.Stats.Base do nil -> q - - _ -> - raise "Unknown goal type" end Enum.reduce(query.filters, q, fn {filter_key, filter_value}, query -> @@ -121,28 +118,6 @@ defmodule Plausible.Stats.Base do where: s.timestamp >= ^first_datetime and s.start < ^last_datetime ) - sessions_q = - case query.filters["event:page"] do - {:is, page} -> - from(e in sessions_q, where: e.entry_page == ^page) - - {:is_not, page} -> - from(e in sessions_q, where: e.entry_page != ^page) - - {:matches, glob_expr} -> - regex = page_regex(glob_expr) - from(s in sessions_q, where: fragment("match(?, ?)", s.entry_page, ^regex)) - - {:member, list} -> - from(e in sessions_q, where: e.entry_page in ^list) - - nil -> - sessions_q - - _ -> - raise "Unknown filter type" - end - Enum.reduce(Filters.visit_props(), sessions_q, fn prop_name, sessions_q -> filter = query.filters["visit:" <> prop_name] prop_name = Map.get(@api_prop_name_to_db, prop_name, prop_name) diff --git a/lib/plausible/stats/breakdown.ex b/lib/plausible/stats/breakdown.ex index 7e66a8eae..66a039bfb 100644 --- a/lib/plausible/stats/breakdown.ex +++ b/lib/plausible/stats/breakdown.ex @@ -158,6 +158,7 @@ defmodule Plausible.Stats.Breakdown do defp breakdown_sessions(site, query, property, metrics, {limit, page}) do offset = (page - 1) * limit + query = treat_page_filter_as_entry_page(query) from(s in query_sessions(site, query), order_by: [desc: fragment("uniq(?)", s.user_id), asc: fragment("min(?)", s.start)], @@ -168,9 +169,21 @@ defmodule Plausible.Stats.Breakdown do |> filter_converted_sessions(site, query) |> do_group_by(property) |> select_metrics(metrics) + |> IO.inspect() |> ClickhouseRepo.all() end + defp treat_page_filter_as_entry_page(query) do + case query.filters["event:page"] do + nil -> + query + + filter -> + new_filters = Map.put(query.filters, "visit:entry_page", filter) + %Query{query | filters: new_filters} + end + end + defp filter_converted_sessions(db_query, site, query) do if query.filters["event:name"] || query.filters["event:page"] || query.filters["visit:goal"] do converted_sessions = diff --git a/lib/plausible/stats/filters.ex b/lib/plausible/stats/filters.ex index 7569be821..7527da540 100644 --- a/lib/plausible/stats/filters.ex +++ b/lib/plausible/stats/filters.ex @@ -11,7 +11,9 @@ defmodule Plausible.Stats.Filters do "browser_version", "os", "os_version", - "country" + "country", + "entry_page", + "exit_page" ] @event_props [ diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 10e026095..5cf3544e9 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -334,8 +334,8 @@ defmodule PlausibleWeb.Api.StatsController do metrics = ["visitors", "visits", "visit_duration"] entry_pages = - Stats.breakdown(site, query, "event:page", metrics, pagination) - |> transform_keys(%{"page" => "name", "visits" => "entries", "visitors" => "count"}) + Stats.breakdown(site, query, "visit:entry_page", metrics, pagination) + |> transform_keys(%{"entry_page" => "name", "visits" => "entries", "visitors" => "count"}) json(conn, entry_pages) end