Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Fix weekly interval bug (#3686)

RobertJoonas7536f0285cde
diff --git a/lib/plausible/stats/fragments.ex b/lib/plausible/stats/fragments.ex
index 19808288d2..a4caea33b5 100644
--- a/lib/plausible/stats/fragments.ex
+++ b/lib/plausible/stats/fragments.ex
@@ -92,7 +92,7 @@ defmodule Plausible.Stats.Fragments do
quote do
weekstart_not_before(
to_timezone(unquote(date), unquote(timezone)),
- unquote(not_before)
+ to_timezone(unquote(not_before), unquote(timezone))
)
end
end
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.exs
index fa1b2bf119..2174943ef6 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
@@ -688,6 +688,31 @@ defmodule PlausibleWeb.Api.StatsController.MainGraphTest do
}
end
+ test "returns stats for the first week of the month when site timezone is ahead of UTC", %{
+ conn: conn,
+ site: site
+ } do
+ site =
+ site
+ |> Plausible.Site.changeset(%{timezone: "Europe/Copenhagen"})
+ |> Plausible.Repo.update!()
+
+ populate_stats(site, [
+ build(:pageview, timestamp: ~N[2023-03-01 12:00:00])
+ ])
+
+ conn =
+ get(
+ conn,
+ "/api/stats/#{site.domain}/main-graph?period=month&metric=visitors&date=2023-03-01&interval=week"
+ )
+
+ %{"labels" => labels, "plot" => plot} = json_response(conn, 200)
+
+ assert List.first(plot) == 1
+ assert List.first(labels) == "2023-03-01"
+ end
+
test "shows half-perfect week-split month on week scale with full week indicators", %{
conn: conn,
site: site