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
Stats api hostname filter (#4008)
hq16fb56dc1ccb3
diff --git a/lib/plausible/stats/props.ex b/lib/plausible/stats/props.exindex 2a1fd48b80..c0f1bff9dd 100644--- a/lib/plausible/stats/props.ex+++ b/lib/plausible/stats/props.ex@@ -1,5 +1,5 @@defmodule Plausible.Stats.Props do- @event_props ["event:page", "event:name", "event:goal"]+ @event_props ["event:page", "event:name", "event:goal", "event:hostname"]@session_props ["visit:source","visit:country",diff --git a/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs b/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exsindex ae12a31b3d..672638da9c 100644--- a/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs+++ b/test/plausible_web/controllers/api/external_stats_controller/aggregate_test.exs@@ -1041,6 +1041,36 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest do}end+ test "can filter by hostname", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:pageview,+ hostname: "one.example.com",+ timestamp: ~N[2021-01-01 00:00:00]+ ),+ build(:pageview,+ hostname: "example.com",+ timestamp: ~N[2021-01-01 00:25:00]+ ),+ build(:pageview, timestamp: ~N[2021-01-01 00:00:00])+ ])++ conn =+ get(conn, "/api/v1/stats/aggregate", %{+ "site_id" => site.domain,+ "period" => "day",+ "date" => "2021-01-01",+ "metrics" => "pageviews,visitors,bounce_rate,visit_duration",+ "filters" => "event:hostname==*.example.com|example.com"+ })++ assert json_response(conn, 200)["results"] == %{+ "pageviews" => %{"value" => 2},+ "visitors" => %{"value" => 2},+ "bounce_rate" => %{"value" => 100},+ "visit_duration" => %{"value" => 0}+ }+ end+test "filtering by event:name", %{conn: conn, site: site} dopopulate_stats(site, [build(:event,diff --git a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exsindex 42c50d12c5..8d91cf080f 100644--- a/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs+++ b/test/plausible_web/controllers/api/external_stats_controller/breakdown_test.exs@@ -414,6 +414,49 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do}end+ for {property, attr} <- [+ {"visit:utm_campaign", :utm_campaign},+ {"visit:utm_source", :utm_source},+ {"visit:utm_term", :utm_term},+ {"visit:utm_content", :utm_content}+ ] do+ test "breakdown by #{property} when filtered by hostname", %{conn: conn, site: site} do+ populate_stats(site, [+ # session starts at two.example.com with utm_param=ad+ build(+ :pageview,+ [+ {unquote(attr), "ad"},+ {:user_id, @user_id},+ {:hostname, "two.example.com"},+ {:timestamp, ~N[2021-01-01 00:00:00]}+ ]+ ),+ # session continues on one.example.com without any utm_params+ build(+ :pageview,+ [+ {:user_id, @user_id},+ {:hostname, "one.example.com"},+ {:timestamp, ~N[2021-01-01 00:15:00]}+ ]+ )+ ])++ conn =+ get(conn, "/api/v1/stats/breakdown", %{+ "site_id" => site.domain,+ "period" => "day",+ "date" => "2021-01-01",+ "filters" => "event:hostname==one.example.com",+ "property" => unquote(property)+ })++ # nobody landed on one.example.com from utm_param=ad+ assert json_response(conn, 200) == %{"results" => []}+ end+ end+test "breakdown by visit:utm_medium", %{conn: conn, site: site} dopopulate_stats(site, [build(:pageview,@@ -1175,6 +1218,32 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest do}end+ test "can filter event:hostname with a wildcard", %{+ conn: conn,+ site: site+ } do+ populate_stats(site, [+ build(:pageview, hostname: "alice.example.com", pathname: "/a"),+ build(:pageview, hostname: "anna.example.com", pathname: "/a"),+ build(:pageview, hostname: "adam.example.com", pathname: "/a"),+ build(:pageview, hostname: "bob.example.com", pathname: "/b")+ ])++ conn =+ get(conn, "/api/v1/stats/breakdown", %{+ "site_id" => site.domain,+ "period" => "day",+ "property" => "event:page",+ "filters" => "event:hostname==a*.example.com"+ })++ assert json_response(conn, 200) == %{+ "results" => [+ %{"page" => "/a", "visitors" => 3}+ ]+ }+ end+test "breakdown by custom event property", %{conn: conn, site: site} dopopulate_stats(site, [build(:event,@@ -1606,6 +1675,66 @@ defmodule PlausibleWeb.Api.ExternalStatsController.BreakdownTest doendend+ test "top sources for a custom goal and filtered by hostname", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:pageview,+ hostname: "blog.example.com",+ referrer_source: "Facebook",+ user_id: @user_id+ ),+ build(:pageview,+ hostname: "app.example.com",+ pathname: "/register",+ user_id: @user_id+ ),+ build(:event,+ name: "Signup",+ hostname: "app.example.com",+ pathname: "/register",+ user_id: @user_id+ )+ ])++ conn =+ get(conn, "/api/v1/stats/breakdown", %{+ "site_id" => site.domain,+ "period" => "day",+ "property" => "visit:source",+ "filters" => "event:hostname==app.example.com"+ })++ assert json_response(conn, 200) == %{"results" => []}+ end++ test "top sources for a custom goal and filtered by hostname (2)", %{conn: conn, site: site} do+ populate_stats(site, [+ build(:pageview,+ hostname: "app.example.com",+ referrer_source: "Facebook",+ pathname: "/register",+ user_id: @user_id+ ),+ build(:event,+ name: "Signup",+ hostname: "app.example.com",+ pathname: "/register",+ user_id: @user_id+ )+ ])++ conn =+ get(conn, "/api/v1/stats/breakdown", %{+ "site_id" => site.domain,+ "period" => "day",+ "property" => "visit:source",+ "filters" => "event:hostname==app.example.com"+ })++ assert json_response(conn, 200) == %{+ "results" => [%{"source" => "Facebook", "visitors" => 1}]+ }+ end+test "event:page filter is interpreted as entry_page filter only for bounce_rate", %{conn: conn,site: sitediff --git a/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exs b/test/plausible_web/controllers/api/external_stats_controller/timeseries_test.exsindex ba9e01c84a..c6ebd537e9 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@@ -963,6 +963,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} dopopulate_stats(site, [build(:event,