diff --git a/lib/plausible_web/controllers/api/stats_controller.ex b/lib/plausible_web/controllers/api/stats_controller.ex index 499f4f7627..26fefc22ef 100644 --- a/lib/plausible_web/controllers/api/stats_controller.ex +++ b/lib/plausible_web/controllers/api/stats_controller.ex @@ -427,6 +427,8 @@ defmodule PlausibleWeb.Api.StatsController do percent_change(old_count, new_count) end + defp percent_change(nil, _new_count), do: nil + defp percent_change(old_count, new_count) do cond do old_count == 0 and new_count > 0 -> @@ -1007,7 +1009,7 @@ defmodule PlausibleWeb.Api.StatsController do end end - defp calculate_cr(nil, _converted_visitors), do: 100.0 + defp calculate_cr(nil, _converted_visitors), do: nil defp calculate_cr(unique_visitors, converted_visitors) do if unique_visitors > 0, diff --git a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs index c6b295abe9..b41da6896b 100644 --- a/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs +++ b/test/plausible_web/controllers/api/stats_controller/top_stats_test.exs @@ -419,5 +419,29 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do assert %{"name" => "Conversion rate", "value" => 33.3, "change" => 100} in res["top_stats"] end + + test "returns conversion rate with change=nil when comparison mode disallowed", %{ + conn: conn, + site: site + } do + populate_stats(site, [ + build(:pageview, user_id: @user_id), + build(:pageview, user_id: @user_id), + build(:pageview), + build(:event, name: "Signup") + ]) + + filters = Jason.encode!(%{goal: "Signup"}) + + conn = + get( + conn, + "/api/stats/#{site.domain}/top-stats?period=all&filters=#{filters}" + ) + + res = json_response(conn, 200) + + assert %{"name" => "Conversion rate", "value" => 33.3, "change" => nil} in res["top_stats"] + end end end