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
Handle negative values in percent change computation gracefully (#6824)
Adrian Gruntkowski9f4c125a803b
diff --git a/lib/plausible/stats/compare.ex b/lib/plausible/stats/compare.exindex 851b18ef09..e96712692d 100644--- a/lib/plausible/stats/compare.ex+++ b/lib/plausible/stats/compare.ex@@ -32,6 +32,9 @@ defmodule Plausible.Stats.Compare doold_count == 0 and new_count > 0 ->100+ old_count == 0 and new_count < 0 ->+ -100+old_count == 0 and new_count == 0 ->0diff --git a/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs b/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exsindex b806e3750b..3f8ad40806 100644--- a/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs+++ b/test/plausible_web/controllers/api/stats_controller/custom_prop_breakdown_test.exs@@ -7,7 +7,7 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do"date_range" => Keyword.get(opts, :date_range, "day"),"filters" => Keyword.get(opts, :filters, []),"metrics" => Keyword.get(opts, :metrics, ["visitors", "events", "percentage"]),- "include" => Keyword.get(opts, :include, nil),+ "include" => Keyword.get(opts, :include, %{}),"pagination" => Keyword.get(opts, :pagination, nil),"order_by" => Keyword.get(opts, :order_by, nil)}@@ -636,6 +636,144 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do]end+ @tag :ee_only+ test "returns revenue metrics when filtering by a revenue goal in comparison mode with negative amounts (regression)",+ %{conn: conn, site: site} do+ prop_key = "logged_in"++ now = DateTime.utc_now(:second)+ day_ago = DateTime.shift(now, hour: -25)++ populate_stats(site, [+ build(:event,+ name: "Payment",+ "meta.key": [prop_key],+ "meta.value": ["true"],+ revenue_reporting_amount: Decimal.new("-12"),+ revenue_reporting_currency: "EUR",+ timestamp: now+ ),+ build(:event,+ name: "Payment",+ "meta.key": [prop_key],+ "meta.value": ["true"],+ revenue_reporting_amount: Decimal.new("-100"),+ revenue_reporting_currency: "EUR",+ timestamp: now+ ),+ build(:event,+ name: "Payment",+ "meta.key": [prop_key],+ "meta.value": ["false"],+ revenue_reporting_amount: Decimal.new("-8"),+ revenue_reporting_currency: "EUR",+ timestamp: now+ ),+ build(:event,+ name: "Payment",+ "meta.key": [prop_key],+ "meta.value": ["false"],+ revenue_reporting_amount: Decimal.new("-8"),+ revenue_reporting_currency: "EUR",+ timestamp: day_ago+ )+ ])++ insert(:goal, %{+ site: site,+ event_name: "Payment",+ currency: :EUR,+ display_name: "PaymentEUR"+ })++ response =+ query_props(conn, site, prop_key,+ filters: [["is", "event:goal", ["PaymentEUR"]]],+ date_range: "24h",+ include: %{+ "compare" => "previous_period",+ "compare_match_day_of_week" => false+ },+ metrics: [+ "visitors",+ "events",+ "conversion_rate",+ "average_revenue",+ "total_revenue"+ ]+ )++ assert response["results"] == [+ %{+ "dimensions" => ["true"],+ "metrics" => [+ 2,+ 2,+ 66.67,+ %{+ "long" => "€-56.00",+ "short" => "€-56.0",+ "value" => -56.0,+ "currency" => "EUR"+ },+ %{+ "long" => "€-112.00",+ "short" => "€-112.0",+ "value" => -112.0,+ "currency" => "EUR"+ }+ ],+ "comparison" => %{+ "change" => [100, 100, 66.7, -100, -100],+ "dimensions" => ["true"],+ "metrics" => [+ 0,+ 0,+ 0.0,+ %{"currency" => "EUR", "long" => "€0.00", "short" => "€0.0", "value" => 0.0},+ %{"currency" => "EUR", "long" => "€0.00", "short" => "€0.0", "value" => 0.0}+ ]+ }+ },+ %{+ "dimensions" => ["false"],+ "metrics" => [+ 1,+ 1,+ 33.33,+ %{+ "long" => "€-8.00",+ "short" => "€-8.0",+ "value" => -8.0,+ "currency" => "EUR"+ },+ %{"long" => "€-8.00", "short" => "€-8.0", "value" => -8.0, "currency" => "EUR"}+ ],+ "comparison" => %{+ "change" => [0, 0, -66.7, 0, 0],+ "dimensions" => ["false"],+ "metrics" => [+ 1,+ 1,+ 100.0,+ %{+ "currency" => "EUR",+ "long" => "€-8.00",+ "short" => "€-8.0",+ "value" => -8.0+ },+ %{+ "currency" => "EUR",+ "long" => "€-8.00",+ "short" => "€-8.0",+ "value" => -8.0+ }+ ]+ }+ }+ ]+ end+@tag :ee_onlytest "returns revenue metrics when filtering by many revenue goals with same currency", %{conn: conn,