Skip to content

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

Fix year-over-year comparisons for leap years (#4940)

Karl-Aksel Puulmann827bd1700f8b
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62a4977888..b742672843 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -23,6 +23,7 @@ All notable changes to this project will be documented in this file.
- Fix returning filter suggestions for multiple custom property values in the dashboard Filter modal
- Fix typo on login screen
- Fix Direct / None details modal not opening
+- Fix year over year comparisons being offset by a day for leap years
## v2.1.4 - 2024-10-08
diff --git a/lib/plausible/stats/comparisons.ex b/lib/plausible/stats/comparisons.ex
index 647658855b..959218afe2 100644
--- a/lib/plausible/stats/comparisons.ex
+++ b/lib/plausible/stats/comparisons.ex
@@ -99,8 +99,9 @@ defmodule Plausible.Stats.Comparisons do
defp get_comparison_date_range(source_query, %{mode: "year_over_year"} = options) do
source_date_range = Query.date_range(source_query, trim_trailing: true)
- start_date = Date.add(source_date_range.first, -365)
- end_date = source_date_range.last |> Date.add(-365)
+ start_date = source_date_range.first |> Date.shift(year: -1)
+ diff_in_days = Date.diff(source_date_range.last, source_date_range.first)
+ end_date = Date.add(start_date, diff_in_days)
Date.range(start_date, end_date)
|> maybe_match_day_of_week(source_date_range, options)
diff --git a/test/plausible/stats/comparisons_test.exs b/test/plausible/stats/comparisons_test.exs
index f291cfd6b0..bdacaff230 100644
--- a/test/plausible/stats/comparisons_test.exs
+++ b/test/plausible/stats/comparisons_test.exs
@@ -161,6 +161,36 @@ defmodule Plausible.Stats.ComparisonsTest do
end
end
+ describe "year_over_year, exact dates behavior with leap years" do
+ test "start of the year matching", %{site: site} do
+ query = Query.from(site, %{"period" => "7d", "date" => "2021-01-05"})
+ comparison_query = Comparisons.get_comparison_query(query, %{mode: "year_over_year"})
+
+ assert comparison_query.utc_time_range.first == ~U[2019-12-30 00:00:00Z]
+ assert comparison_query.utc_time_range.last == ~U[2020-01-05 23:59:59Z]
+ assert date_range_length(comparison_query) == 7
+ end
+
+ test "leap day matching", %{site: site} do
+ query = Query.from(site, %{"period" => "7d", "date" => "2021-03-03"})
+ comparison_query = Comparisons.get_comparison_query(query, %{mode: "year_over_year"})
+
+ assert comparison_query.utc_time_range.first == ~U[2020-02-25 00:00:00Z]
+ # :TRICKY: Since dates of the two months don't match precisely we cut off earlier
+ assert comparison_query.utc_time_range.last == ~U[2020-03-02 23:59:59Z]
+ assert date_range_length(comparison_query) == 7
+ end
+
+ test "end of the year matching", %{site: site} do
+ query = Query.from(site, %{"period" => "7d", "date" => "2021-11-25"})
+ comparison_query = Comparisons.get_comparison_query(query, %{mode: "year_over_year"})
+
+ assert comparison_query.utc_time_range.first == ~U[2020-11-19 00:00:00Z]
+ assert comparison_query.utc_time_range.last == ~U[2020-11-25 23:59:59Z]
+ assert date_range_length(comparison_query) == 7
+ end
+ end
+
describe "with period set to year to date" do
test "shifts back by the same number of days when mode is previous_period", %{site: site} do
query =
@@ -369,4 +399,10 @@ defmodule Plausible.Stats.ComparisonsTest do
query
end
+
+ def date_range_length(query) do
+ query
+ |> Query.date_range()
+ |> Enum.count()
+ end
end