Skip to content

Commit

Permalink
Fix revenue metrics for custom named goals (plausible#4653)
Browse files Browse the repository at this point in the history
Since plausible#4415, revenue metrics
have not been correctly displayed on the dashboard if the goal name has
been renamed.
  • Loading branch information
macobo authored Oct 7, 2024
1 parent c829163 commit 4224f9d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
### Changed

### Fixed
- Revenue metrics are displayed correctly after goal has been renamed

## v2.1.3 - 2024-09-26

Expand Down
4 changes: 2 additions & 2 deletions extra/lib/plausible/stats/goal/revenue.ex
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ defmodule Plausible.Stats.Goal.Revenue do
revenue_goals_currencies =
Plausible.Repo.all(
from rg in Ecto.assoc(site, :revenue_goals),
where: rg.event_name in ^goal_filters,
where: rg.display_name in ^goal_filters,
select: rg.currency,
distinct: true
)
Expand All @@ -58,7 +58,7 @@ defmodule Plausible.Stats.Goal.Revenue do
def cast_revenue_metrics_to_money([%{goal: _goal} | _rest] = results, revenue_goals)
when is_list(revenue_goals) do
for result <- results do
if matching_goal = Enum.find(revenue_goals, &(&1.event_name == result.goal)) do
if matching_goal = Enum.find(revenue_goals, &(&1.display_name == result.goal)) do
cast_revenue_metrics_to_money(result, matching_goal.currency)
else
result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,14 @@ defmodule PlausibleWeb.Api.StatsController.CustomPropBreakdownTest do
)
])

insert(:goal, %{site: site, event_name: "Payment", currency: :EUR})

filters = Jason.encode!(%{goal: "Payment"})
insert(:goal, %{
site: site,
event_name: "Payment",
currency: :EUR,
display_name: "PaymentEUR"
})

filters = Jason.encode!(%{goal: "PaymentEUR"})

conn =
get(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1338,8 +1338,19 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do

@tag :ee_only
test "returns average and total when filtering by a revenue goal", %{conn: conn, site: site} do
insert(:goal, site: site, event_name: "Payment", currency: "USD")
insert(:goal, site: site, event_name: "AddToCart", currency: "EUR")
insert(:goal,
site: site,
event_name: "Payment",
currency: "USD",
display_name: "PaymentUSD"
)

insert(:goal,
site: site,
event_name: "AddToCart",
currency: "EUR",
display_name: "AddToCartEUR"
)

populate_stats(site, [
build(:event,
Expand All @@ -1364,7 +1375,7 @@ defmodule PlausibleWeb.Api.StatsController.TopStatsTest do
)
])

filters = Jason.encode!(%{goal: "Payment"})
filters = Jason.encode!(%{goal: "PaymentUSD"})
conn = get(conn, "/api/stats/#{site.domain}/top-stats?period=all&filters=#{filters}")
assert %{"top_stats" => top_stats} = json_response(conn, 200)

Expand Down

0 comments on commit 4224f9d

Please sign in to comment.