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
Fix year-over-year comparisons for leap years (#4940)
Karl-Aksel Puulmann827bd1700f8b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 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-08diff --git a/lib/plausible/stats/comparisons.ex b/lib/plausible/stats/comparisons.exindex 647658855b..959218afe2 100644--- a/lib/plausible/stats/comparisons.ex+++ b/lib/plausible/stats/comparisons.ex@@ -99,8 +99,9 @@ defmodule Plausible.Stats.Comparisons dodefp get_comparison_date_range(source_query, %{mode: "year_over_year"} = options) dosource_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.exsindex 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 doendend+ 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" dotest "shifts back by the same number of days when mode is previous_period", %{site: site} doquery =@@ -369,4 +399,10 @@ defmodule Plausible.Stats.ComparisonsTest doqueryend++ def date_range_length(query) do+ query+ |> Query.date_range()+ |> Enum.count()+ endend