From dea210469e121b3d2da4fa3ca101fbb71f5423e0 Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Mon, 1 Apr 2024 11:45:55 +0200 Subject: [PATCH] wip --- assets/js/dashboard/util/filters.js | 4 +- lib/plausible/stats/aggregate.ex | 4 - lib/plausible/stats/base.ex | 7 +- lib/plausible/stats/filter_suggestions.ex | 1 + .../controllers/api/stats_controller.ex | 1 - .../timeseries_test.exs | 46 ++++- .../api/stats_controller/pages_test.exs | 184 ++++++++++++++++++ .../api/stats_controller/top_stats_test.exs | 1 - 8 files changed, 236 insertions(+), 12 deletions(-) diff --git a/assets/js/dashboard/util/filters.js b/assets/js/dashboard/util/filters.js index 66e7f79cc..014e1f3ac 100644 --- a/assets/js/dashboard/util/filters.js +++ b/assets/js/dashboard/util/filters.js @@ -1,6 +1,5 @@ export const FILTER_GROUPS = { 'page': ['page', 'entry_page', 'exit_page'], - 'hostname': ['hostname'], 'source': ['source', 'referrer'], 'location': ['country', 'region', 'city'], 'screen': ['screen'], @@ -8,7 +7,8 @@ export const FILTER_GROUPS = { 'os': ['os', 'os_version'], 'utm': ['utm_medium', 'utm_source', 'utm_campaign', 'utm_term', 'utm_content'], 'goal': ['goal'], - 'props': ['prop_key', 'prop_value'] + 'props': ['prop_key', 'prop_value'], + 'hostname': ['hostname'], } export const NO_CONTAINS_OPERATOR = new Set(['goal', 'screen'].concat(FILTER_GROUPS['location'])) diff --git a/lib/plausible/stats/aggregate.ex b/lib/plausible/stats/aggregate.ex index 5c773e44d..5cb228f19 100644 --- a/lib/plausible/stats/aggregate.ex +++ b/lib/plausible/stats/aggregate.ex @@ -19,8 +19,6 @@ defmodule Plausible.Stats.Aggregate do @session_metrics [:visits, :bounce_rate, :visit_duration, :views_per_visit, :sample_percent] def aggregate(site, query, metrics) do - # IO.inspect(metrics, label: :metrics) - {currency, metrics} = on_full_build do Plausible.Stats.Goal.Revenue.get_revenue_tracking_currency(site, query, metrics) @@ -39,7 +37,6 @@ defmodule Plausible.Stats.Aggregate do session_metrics = Enum.filter(metrics, &(&1 in @session_metrics)) - # |> IO.inspect(label: :session_metrics) session_task = fn -> aggregate_sessions(site, query, session_metrics) end @@ -63,7 +60,6 @@ defmodule Plausible.Stats.Aggregate do defp aggregate_events(site, query, metrics) do from(e in base_event_query(site, query), select: ^select_event_metrics(metrics)) - # |> IO.inspect(label: :agg_query) |> merge_imported(site, query, :aggregate, metrics) |> maybe_add_conversion_rate(site, query, metrics, include_imported: query.include_imported) |> ClickhouseRepo.one() diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex index 441faa30e..f7ab9bf33 100644 --- a/lib/plausible/stats/base.ex +++ b/lib/plausible/stats/base.ex @@ -48,9 +48,10 @@ defmodule Plausible.Stats.Base do q = Plausible.Stats.Sampling.add_query_hint(q, query) end - q = from(e in q, where: ^dynamic_filter_condition(query, "event:page", :pathname)) - - q = from(e in q, where: ^dynamic_filter_condition(query, "event:hostname", :hostname)) + q = + q + |> where([e], ^dynamic_filter_condition(query, "event:page", :pathname)) + |> where([e], ^dynamic_filter_condition(query, "event:hostname", :hostname)) q = case query.filters["event:name"] do diff --git a/lib/plausible/stats/filter_suggestions.ex b/lib/plausible/stats/filter_suggestions.ex index 389c0dfb7..9366541b3 100644 --- a/lib/plausible/stats/filter_suggestions.ex +++ b/lib/plausible/stats/filter_suggestions.ex @@ -236,6 +236,7 @@ defmodule Plausible.Stats.FilterSuggestions do select: e.hostname, where: fragment("? ilike ?", e.hostname, ^filter_query) ) + |> IO.inspect(label: :q) :entry_page -> from(e in q, diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 098c20333..13d59e849 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -384,7 +384,6 @@ defmodule PlausibleWeb.Api.StatsController do current_results = Stats.aggregate(site, query, metrics) - # |> IO.inspect(label: :current) prev_results = comparison_query && Stats.aggregate(site, comparison_query, metrics) diff --git a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs index 528d3584a..457a46ac7 100644 --- a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs +++ b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs @@ -918,7 +918,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do timestamp: ~N[2021-01-01 05:00:00] ), build(:pageview, - pathname: "/goobye", + pathname: "/goodbye", timestamp: ~N[2021-01-01 00:00:00] ) ]) @@ -944,6 +944,50 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest do } end + test "can filter by hostname", %{ + conn: conn, + site: site + } do + populate_stats(site, [ + build(:pageview, + user_id: @user_id, + hostname: "landing.example.com", + timestamp: ~N[2021-01-01 00:00:01] + ), + build(:pageview, + user_id: @user_id, + hostname: "example.com", + timestamp: ~N[2021-01-01 00:00:02] + ), + build(:pageview, + user_id: @user_id, + hostname: "example.com", + timestamp: ~N[2021-01-01 00:00:06] + ) + ]) + + conn = + get(conn, "/api/v1/stats/timeseries", %{ + "site_id" => site.domain, + "period" => "day", + "date" => "2021-01-01", + "filters" => "event:hostname==example.com", + "metrics" => "visitors,visits,pageviews,bounce_rate,visit_duration" + }) + + res = + json_response(conn, 200)["results"] + + assert List.first(res) == %{ + "bounce_rate" => 0, + "date" => "2021-01-01 00:00:00", + "pageviews" => 2, + "visit_duration" => 5, + "visitors" => 1, + "visits" => 1 + } + end + test "can filter by event:name", %{conn: conn, site: site} do populate_stats(site, [ build(:event, diff --git a/test/plausible_web/controllers/api/stats_controller/pages_test.exs b/test/plausible_web/controllers/api/stats_controller/pages_test.exs index 6a84c715f..048883959 100644 --- a/test/plausible_web/controllers/api/stats_controller/pages_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/pages_test.exs @@ -919,6 +919,81 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do ] end + test "calculates bounce rate and time on page for pages when filtered by hostname", %{ + conn: conn, + site: site + } do + populate_stats(site, [ + build(:pageview, + pathname: "/about", + hostname: "blog.example.com", + user_id: @user_id + 1, + timestamp: ~N[2021-01-01 00:01:00] + ), + build(:pageview, + pathname: "/about", + hostname: "example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:01:00] + ), + build(:pageview, + pathname: "/about", + hostname: "blog.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:10:00] + ), + build(:pageview, + pathname: "/about", + hostname: "blog.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:15:00] + ), + build(:pageview, + pathname: "/exit-blog", + hostname: "blog.example.com", + timestamp: ~N[2021-01-01 00:20:00], + user_id: @user_id + ), + build(:pageview, + pathname: "/about", + hostname: "example.com", + timestamp: ~N[2021-01-01 00:20:00], + user_id: @user_id + ), + build(:pageview, + pathname: "/exit", + hostname: "example.com", + timestamp: ~N[2021-01-01 00:25:00], + user_id: @user_id + ) + ]) + + filters = Jason.encode!(%{"hostname" => "blog.example.com"}) + + conn = + get( + conn, + "/api/stats/#{site.domain}/pages?period=day&date=2021-01-01&detailed=true&filters=#{filters}" + ) + + assert json_response(conn, 200) == [ + %{ + "bounce_rate" => 50, + "name" => "/about", + "pageviews" => 3, + "time_on_page" => 600.0, + "visitors" => 2 + }, + %{ + "bounce_rate" => nil, + "name" => "/exit-blog", + "pageviews" => 1, + "time_on_page" => nil, + "visitors" => 1 + } + ] + end + test "doesn't calculate time on page with only single page visits", %{conn: conn, site: site} do populate_stats(site, [ build(:pageview, pathname: "/", user_id: @user_id, timestamp: ~N[2021-01-01 00:00:00]), @@ -1258,6 +1333,68 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do ] end + test "returns top entry pages by visitors filtered by hostname", %{conn: conn, site: site} do + populate_stats(site, [ + build(:pageview, + pathname: "/page1", + hostname: "en.example.com", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page1", + hostname: "es.example.com", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page2", + hostname: "en.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page2", + hostname: "es.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:15:00] + ), + build(:pageview, + pathname: "/exit", + hostname: "es.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:16:00] + ), + build(:pageview, + pathname: "/page2", + hostname: "es.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 23:15:00] + ) + ]) + + filters = Jason.encode!(%{"hostname" => "es.example.com"}) + + conn = + get( + conn, + "/api/stats/#{site.domain}/entry-pages?period=day&date=2021-01-01&filters=#{filters}" + ) + + assert json_response(conn, 200) == [ + %{ + "visitors" => 1, + "visits" => 1, + "name" => "/page1", + "visit_duration" => 0 + }, + %{ + "visitors" => 1, + "visits" => 2, + "name" => "/page2", + "visit_duration" => 480 + } + ] + end + test "bugfix: pagination on /pages filtered by goal", %{conn: conn, site: site} do populate_stats( site, @@ -1408,6 +1545,53 @@ defmodule PlausibleWeb.Api.StatsController.PagesTest do ] end + test "returns top exit pages by visitors filtered by hostname", %{conn: conn, site: site} do + populate_stats(site, [ + build(:pageview, + pathname: "/page1", + hostname: "en.example.com", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page1", + hostname: "es.example.com", + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page1", + hostname: "en.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:00:00] + ), + build(:pageview, + pathname: "/page2", + hostname: "es.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:15:00] + ), + build(:pageview, + pathname: "/exit", + hostname: "es.example.com", + user_id: @user_id, + timestamp: ~N[2021-01-01 00:16:00] + ) + ]) + + filters = Jason.encode!(%{hostname: "es.example.com"}) + + conn = + get( + conn, + "/api/stats/#{site.domain}/exit-pages?period=day&date=2021-01-01&filters=#{filters}" + ) + + assert json_response(conn, 200) == + [ + %{"name" => "/exit", "visitors" => 1, "visits" => 1}, + %{"name" => "/page1", "visitors" => 1, "visits" => 1} + ] + end + test "returns top exit pages filtered by custom pageview props", %{conn: conn, site: site} do populate_stats(site, [ build(:pageview, diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs index e8d918867..fbb70d375 100644 --- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs @@ -663,7 +663,6 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do res = json_response(conn, 200) - # |> IO.inspect(label: :huh) assert %{"name" => "Unique visitors", "value" => 2} in res["top_stats"] assert %{"name" => "Total visits", "value" => 2} in res["top_stats"]