Skip to content

Commit

Permalink
Turn Revenue Goals into Custom Events if the plan doesn't support them (
Browse files Browse the repository at this point in the history
#3768)

* Turn Revenue Goals into Custom Events if the plan runs out

* Tag test with full_build
  • Loading branch information
aerosol authored Feb 12, 2024
1 parent 499be9f commit f5129f1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/plausible_web/controllers/api/stats_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -1144,7 +1144,8 @@ defmodule PlausibleWeb.Api.StatsController do

metrics =
on_full_build do
if Enum.any?(site.goals, &Plausible.Goal.Revenue.revenue?/1) do
if Enum.any?(site.goals, &Plausible.Goal.Revenue.revenue?/1) and
Plausible.Billing.Feature.RevenueGoals.enabled?(site) do
[:visitors, :events] ++ @revenue_metrics
else
[:visitors, :events]
Expand Down
1 change: 1 addition & 0 deletions lib/plausible_web/live/goal_settings.ex
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ defmodule PlausibleWeb.Live.GoalSettings do
goals={@displayed_goals}
domain={@domain}
filter_text={@filter_text}
site={@site}
/>
</div>
"""
Expand Down
19 changes: 16 additions & 3 deletions lib/plausible_web/live/goal_settings/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ defmodule PlausibleWeb.Live.GoalSettings.List do
attr(:goals, :list, required: true)
attr(:domain, :string, required: true)
attr(:filter_text, :string)
attr(:site, Plausible.Site, required: true)

def render(assigns) do
revenue_goals_enabled? = Plausible.Billing.Feature.RevenueGoals.enabled?(assigns.site)
assigns = assign(assigns, :revenue_goals_enabled?, revenue_goals_enabled?)

~H"""
<div>
<div class="border-t border-gray-200 pt-4 sm:flex sm:items-center sm:justify-between">
Expand Down Expand Up @@ -56,14 +60,23 @@ defmodule PlausibleWeb.Live.GoalSettings.List do
<span class="text-sm font-medium text-gray-900 dark:text-gray-100 w-3/4">
<div class="flex">
<span class="truncate">
<%= goal %>
<br />
<%= if not @revenue_goals_enabled? && goal.currency do %>
<div class="text-gray-600 flex items-center">
<Heroicons.lock_closed class="w-4 h-4 mr-1 inline" />
<span><%= goal %></span>
</div>
<% else %>
<%= goal %>
<% end %>
<span class="text-sm text-gray-400 block mt-1 font-normal">
<span :if={goal.page_path}>Pageview</span>
<span :if={goal.event_name && !goal.currency}>Custom Event</span>
<span :if={goal.currency}>
<span :if={goal.currency && @revenue_goals_enabled?}>
Revenue Goal
</span>
<span :if={goal.currency && not @revenue_goals_enabled?} class="text-red-600">
Unlock Revenue Goals by upgrading to a business plan
</span>
<span :if={not Enum.empty?(goal.funnels)}> - belongs to funnel(s)</span>
</span>
</span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,50 @@ defmodule PlausibleWeb.Api.StatsController.ConversionsTest do
]
end

@tag :full_build_only
test "returns revenue goals as custom events if the plan doesn't cover the feature", %{
conn: conn,
site: site,
user: user
} do
user
|> Plausible.Auth.User.end_trial()
|> Plausible.Repo.update!()

populate_stats(site, [
build(:event,
name: "Payment",
revenue_reporting_amount: Decimal.new("200100300.123"),
revenue_reporting_currency: "USD"
),
build(:event,
name: "Payment",
revenue_reporting_amount: Decimal.new("300100400.123"),
revenue_reporting_currency: "USD"
),
build(:event,
name: "Payment",
revenue_reporting_amount: Decimal.new("0"),
revenue_reporting_currency: "USD"
),
build(:event, name: "Payment", revenue_reporting_amount: nil),
build(:event, name: "Payment", revenue_reporting_amount: nil)
])

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

conn = get(conn, "/api/stats/#{site.domain}/conversions?period=day")

assert json_response(conn, 200) == [
%{
"name" => "Payment",
"visitors" => 5,
"events" => 5,
"conversion_rate" => 100.0
}
]
end

@tag :full_build_only
test "returns revenue metrics as nil for non-revenue goals", %{
conn: conn,
Expand Down
18 changes: 18 additions & 0 deletions test/plausible_web/live/goal_settings_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ defmodule PlausibleWeb.Live.GoalSettingsTest do
assert resp =~ "Revenue Goal"
end

@tag :full_build_only
test "lists Revenue Goals with feature availability annotation if the plan does not cover them",
%{conn: conn, user: user, site: site} do
{:ok, [_, _, g3]} = setup_goals(site)

user
|> Plausible.Auth.User.end_trial()
|> Plausible.Repo.update!()

conn = get(conn, "/#{site.domain}/settings/goals")

resp = html_response(conn, 200)

assert g3.currency
assert resp =~ to_string(g3)
assert resp =~ "Unlock Revenue Goals by upgrading to a business plan"
end

test "lists goals with delete actions", %{conn: conn, site: site} do
{:ok, goals} = setup_goals(site)
conn = get(conn, "/#{site.domain}/settings/goals")
Expand Down

0 comments on commit f5129f1

Please sign in to comment.