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
Make "last N months" return date ranges consistent with "last N days" (#5677)
Adrian Gruntkowskif94aebc00b07
diff --git a/lib/plausible/stats/filters/query_parser.ex b/lib/plausible/stats/filters/query_parser.exindex 2fdf6b0149..fbb218c4c1 100644--- a/lib/plausible/stats/filters/query_parser.ex+++ b/lib/plausible/stats/filters/query_parser.ex@@ -251,8 +251,8 @@ defmodule Plausible.Stats.Filters.QueryParser do{:ok, DateTimeRange.new!(first, last, site.timezone)}{n, "mo"} when n > 0 and n <= 100 ->- last = date |> Date.end_of_month()- first = last |> Date.shift(month: -n + 1) |> Date.beginning_of_month()+ last = date |> Date.shift(month: -1) |> Date.end_of_month()+ first = date |> Date.shift(month: -n) |> Date.beginning_of_month(){:ok, DateTimeRange.new!(first, last, site.timezone)}_ ->diff --git a/lib/plausible/stats/goal_suggestions.ex b/lib/plausible/stats/goal_suggestions.exindex 2cc20e0808..4747e1753c 100644--- a/lib/plausible/stats/goal_suggestions.ex+++ b/lib/plausible/stats/goal_suggestions.ex@@ -37,7 +37,16 @@ defmodule Plausible.Stats.GoalSuggestions dolimit = Keyword.get(opts, :limit, 25)- params = %{"with_imported" => "true", "period" => "6mo"}+ to_date = Date.utc_today()+ from_date = Date.shift(to_date, month: -6)++ params = %{+ "with_imported" => "true",+ "period" => "custom",+ "from" => Date.to_iso8601(from_date),+ "to" => Date.to_iso8601(to_date)+ }+query = Query.from(site, params)native_q =diff --git a/lib/plausible/stats/legacy/legacy_query_builder.ex b/lib/plausible/stats/legacy/legacy_query_builder.exindex f888ea66d0..0f37a82214 100644--- a/lib/plausible/stats/legacy/legacy_query_builder.ex+++ b/lib/plausible/stats/legacy/legacy_query_builder.ex@@ -108,8 +108,8 @@ defmodule Plausible.Stats.Legacy.QueryBuilder dodefp put_period(query, site, %{"period" => "month"} = params) dodate = parse_single_date(query, params)- start_date = Timex.beginning_of_month(date)- end_date = Timex.end_of_month(date)+ start_date = Date.beginning_of_month(date)+ end_date = Date.end_of_month(date)datetime_range =DateTimeRange.new!(start_date, end_date, site.timezone)@@ -121,11 +121,12 @@ defmodule Plausible.Stats.Legacy.QueryBuilder dodefp put_period(query, site, %{"period" => "6mo"} = params) doend_date =parse_single_date(query, params)- |> Timex.end_of_month()+ |> Date.shift(month: -1)+ |> Date.end_of_month()start_date =Date.shift(end_date, month: -5)- |> Timex.beginning_of_month()+ |> Date.beginning_of_month()datetime_range =DateTimeRange.new!(start_date, end_date, site.timezone)@@ -137,11 +138,12 @@ defmodule Plausible.Stats.Legacy.QueryBuilder dodefp put_period(query, site, %{"period" => "12mo"} = params) doend_date =parse_single_date(query, params)- |> Timex.end_of_month()+ |> Date.shift(month: -1)+ |> Date.end_of_month()start_date =Date.shift(end_date, month: -11)- |> Timex.beginning_of_month()+ |> Date.beginning_of_month()datetime_range =DateTimeRange.new!(start_date, end_date, site.timezone)diff --git a/test/plausible/stats/query_parser_test.exs b/test/plausible/stats/query_parser_test.exsindex ca32279020..1f65514ca6 100644--- a/test/plausible/stats/query_parser_test.exs+++ b/test/plausible/stats/query_parser_test.exs@@ -40,20 +40,20 @@ defmodule Plausible.Stats.Filters.QueryParserTest dolast: DateTime.new!(~D[2021-05-31], ~T[23:59:59], "Etc/UTC")}@date_range_3mo %DateTimeRange{- first: DateTime.new!(~D[2021-03-01], ~T[00:00:00], "Etc/UTC"),- last: DateTime.new!(~D[2021-05-31], ~T[23:59:59], "Etc/UTC")+ first: DateTime.new!(~D[2021-02-01], ~T[00:00:00], "Etc/UTC"),+ last: DateTime.new!(~D[2021-04-30], ~T[23:59:59], "Etc/UTC")}@date_range_6mo %DateTimeRange{- first: DateTime.new!(~D[2020-12-01], ~T[00:00:00], "Etc/UTC"),- last: DateTime.new!(~D[2021-05-31], ~T[23:59:59], "Etc/UTC")+ first: DateTime.new!(~D[2020-11-01], ~T[00:00:00], "Etc/UTC"),+ last: DateTime.new!(~D[2021-04-30], ~T[23:59:59], "Etc/UTC")}@date_range_year %DateTimeRange{first: DateTime.new!(~D[2021-01-01], ~T[00:00:00], "Etc/UTC"),last: DateTime.new!(~D[2021-12-31], ~T[23:59:59], "Etc/UTC")}@date_range_12mo %DateTimeRange{- first: DateTime.new!(~D[2020-06-01], ~T[00:00:00], "Etc/UTC"),- last: DateTime.new!(~D[2021-05-31], ~T[23:59:59], "Etc/UTC")+ first: DateTime.new!(~D[2020-05-01], ~T[00:00:00], "Etc/UTC"),+ last: DateTime.new!(~D[2021-04-30], ~T[23:59:59], "Etc/UTC")}@default_include %{diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exsindex 02d58a32a8..7393179b10 100644--- a/test/plausible/stats/query_test.exs+++ b/test/plausible/stats/query_test.exs@@ -72,16 +72,16 @@ defmodule Plausible.Stats.QueryTest dotest "parses 6 month format", %{site: site} doq = Query.from(site, %{"period" => "6mo"}, %{}, @now)- assert q.utc_time_range.first == ~U[2023-12-01 05:00:00Z]- assert q.utc_time_range.last == ~U[2024-06-01 03:59:59Z]+ assert q.utc_time_range.first == ~U[2023-11-01 04:00:00Z]+ assert q.utc_time_range.last == ~U[2024-05-01 03:59:59Z]assert q.interval == "month"endtest "parses 12 month format", %{site: site} doq = Query.from(site, %{"period" => "12mo"}, %{}, @now)- assert q.utc_time_range.first == ~U[2023-06-01 04:00:00Z]- assert q.utc_time_range.last == ~U[2024-06-01 03:59:59Z]+ assert q.utc_time_range.first == ~U[2023-05-01 04:00:00Z]+ assert q.utc_time_range.last == ~U[2024-05-01 03:59:59Z]assert q.interval == "month"enddiff --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 2528ed4ef2..b2ad7103c2 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@@ -403,7 +403,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AggregateTest doget(conn, "/api/v1/stats/aggregate", %{"site_id" => site.domain,"period" => "6mo",- "date" => "2021-03-01",+ "date" => "2021-04-11","metrics" => "pageviews,visitors,bounce_rate,visit_duration","compare" => "previous_period"})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.exsindex 5c5f819bd4..f128963887 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@@ -87,7 +87,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest doconn =get(conn, "/api/v1/stats/timeseries", %{"site_id" => site.domain,- "date" => "2021-06-30",+ "date" => "2021-07-15","period" => "6mo","interval" => "date"})@@ -365,7 +365,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest doget(conn, "/api/v1/stats/timeseries", %{"site_id" => site.domain,"period" => "6mo",- "date" => "2021-01-01"+ "date" => "2021-02-10"})assert json_response(conn, 200) == %{@@ -392,7 +392,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.TimeseriesTest doget(conn, "/api/v1/stats/timeseries", %{"site_id" => site.domain,"period" => "12mo",- "date" => "2021-01-01"+ "date" => "2021-02-01"})assert json_response(conn, 200) == %{diff --git a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exsindex e8b7ec75e0..0c9ebc1398 100644--- a/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/main_graph_test.exs@@ -255,10 +255,10 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest dotest "displays visitors for 6 months with imported data", %{conn: conn, site: site} dopopulate_stats(site, [- build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),- build(:pageview, timestamp: ~N[2021-06-30 00:00:00]),- build(:imported_visitors, date: ~D[2021-01-01]),- build(:imported_visitors, date: ~D[2021-06-30])+ build(:pageview, timestamp: ~N[2020-12-01 00:00:00]),+ build(:pageview, timestamp: ~N[2021-05-31 00:00:00]),+ build(:imported_visitors, date: ~D[2020-12-01]),+ build(:imported_visitors, date: ~D[2021-05-31])])conn =@@ -277,8 +277,8 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest dotest "displays visitors for 6 months with only imported data", %{conn: conn, site: site} dopopulate_stats(site, [- build(:imported_visitors, date: ~D[2021-01-01]),- build(:imported_visitors, date: ~D[2021-06-30])+ build(:imported_visitors, date: ~D[2020-12-01]),+ build(:imported_visitors, date: ~D[2021-05-31])])conn =@@ -297,10 +297,10 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest dotest "displays visitors for 12 months with imported data", %{conn: conn, site: site} dopopulate_stats(site, [- build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),- build(:pageview, timestamp: ~N[2021-12-31 00:00:00]),- build(:imported_visitors, date: ~D[2021-01-01]),- build(:imported_visitors, date: ~D[2021-12-31])+ build(:pageview, timestamp: ~N[2020-12-01 00:00:00]),+ build(:pageview, timestamp: ~N[2021-11-30 00:00:00]),+ build(:imported_visitors, date: ~D[2020-12-01]),+ build(:imported_visitors, date: ~D[2021-11-30])])conn =@@ -319,8 +319,8 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest dotest "displays visitors for 12 months with only imported data", %{conn: conn, site: site} dopopulate_stats(site, [- build(:imported_visitors, date: ~D[2021-01-01]),- build(:imported_visitors, date: ~D[2021-12-31])+ build(:imported_visitors, date: ~D[2020-12-01]),+ build(:imported_visitors, date: ~D[2021-11-30])])conn =@@ -848,14 +848,14 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest doinsert(:goal, site: site, event_name: "Signup")populate_stats(site, [- build(:event, name: "Different", timestamp: ~N[2020-01-10 00:00:00]),+ build(:event, name: "Different", timestamp: ~N[2019-12-10 00:00:00]),+ build(:event, name: "Signup", timestamp: ~N[2020-01-10 00:00:00]),build(:event, name: "Signup", timestamp: ~N[2020-02-10 00:00:00]),build(:event, name: "Signup", timestamp: ~N[2020-03-10 00:00:00]),- build(:event, name: "Signup", timestamp: ~N[2020-04-10 00:00:00]),- build(:pageview, timestamp: ~N[2021-05-10 00:00:00]),- build(:event, name: "Signup", timestamp: ~N[2021-06-11 04:00:00]),- build(:event, name: "Signup", timestamp: ~N[2021-07-11 00:00:00]),- build(:event, name: "Signup", timestamp: ~N[2021-08-11 00:00:00])+ build(:pageview, timestamp: ~N[2021-04-10 00:00:00]),+ build(:event, name: "Signup", timestamp: ~N[2021-05-11 04:00:00]),+ build(:event, name: "Signup", timestamp: ~N[2021-06-11 00:00:00]),+ build(:event, name: "Signup", timestamp: ~N[2021-07-11 00:00:00])])filters = Jason.encode!([[:is, "event:goal", ["Signup"]]])@@ -1008,11 +1008,11 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest dotest "displays visitors for 6mo on a day scale", %{conn: conn, site: site} dopopulate_stats(site, [- build(:pageview, timestamp: ~N[2021-01-01 00:00:00]),- build(:pageview, timestamp: ~N[2021-01-15 00:00:00]),+ build(:pageview, timestamp: ~N[2020-12-01 00:00:00]),+ build(:pageview, timestamp: ~N[2020-12-15 00:00:00]),+ build(:pageview, timestamp: ~N[2020-12-15 00:00:00]),build(:pageview, timestamp: ~N[2021-01-15 00:00:00]),- build(:pageview, timestamp: ~N[2021-02-15 00:00:00]),- build(:pageview, timestamp: ~N[2021-06-30 01:00:00])+ build(:pageview, timestamp: ~N[2021-05-31 01:00:00])])conn =@@ -1023,7 +1023,7 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest doassert %{"plot" => plot} = json_response(conn, 200)- assert Enum.count(plot) == 181+ assert Enum.count(plot) == 182assert List.first(plot) == 1assert Enum.at(plot, 14) == 2assert Enum.at(plot, 45) == 1diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exsindex 5ee37dfd82..0f1c079c58 100644--- a/test/plausible_web/controllers/stats_controller_test.exs+++ b/test/plausible_web/controllers/stats_controller_test.exs@@ -850,7 +850,7 @@ defmodule PlausibleWeb.StatsControllerTest dotest "exports 6 months of data in zipped csvs", %{conn: conn, site: site} dopopulate_exported_stats(site)- conn = get(conn, "/" <> site.domain <> "/export?period=6mo&date=2021-10-20")+ conn = get(conn, "/" <> site.domain <> "/export?period=6mo&date=2021-11-20")assert_zip(conn, "6m")endend