fix a bug in percent change calculation (#2731)

This commit is contained in:
RobertJoonas 2023-03-08 13:45:04 +02:00 committed by GitHub
parent 4b21b4e6d0
commit 4eca1aef70
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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,

View File

@ -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