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
Wire up usage notifications (#6093)
Sanne de Vriese86549900ce3
diff --git a/lib/plausible/billing/qouta/quota.ex b/lib/plausible/billing/qouta/quota.exindex 3ab0741945..b12af4c34e 100644--- a/lib/plausible/billing/qouta/quota.ex+++ b/lib/plausible/billing/qouta/quota.ex@@ -191,23 +191,24 @@ defmodule Plausible.Billing.Quota doExample for 10k plan:- 9,000 pageviews: `:pageview_approaching_limit` (90% of base limit)- - 10,500 pageviews (1 cycle): `:traffic_exceeded_last_cycle` (over base limit)- - 10,500 + 10,200 pageviews: `:traffic_exceeded_sustained` (both cycles over base limit)+ - 10,500 pageviews (1 cycle): `:traffic_exceeded_last_cycle` or `:traffic_exceeded_current_cycle` (over base limit)+ - 10,500 + 10,200 pageviews: `:traffic_exceeded_sustained` (two consecutive cycles over base limit)- 12,000 + 11,500 pageviews + grace period active: `:grace_period_active` (over margin)- 12,000 + 11,500 pageviews + grace period expired: `:dashboard_locked` (over margin)## Priority order- 1. Dashboard locked- 2. Trial ended+ 1. Trial ended+ 2. Dashboard locked3. Manual lock grace period active (enterprise, no end date)4. Grace period active (with countdown)5. Traffic exceeded for 2 consecutive cycles- 6. Traffic exceeded for 1 cycle- 7. Pageview limit approaching- 8. Site and team member limits both reached- 9. Site limit reached- 10. Team member limit reached+ 6. Traffic exceeded for current cycle only+ 7. Traffic exceeded for last cycle only+ 8. Pageview limit approaching+ 9. Site and team member limits both reached+ 10. Site limit reached+ 11. Team member limit reached"""def usage_notification_type(team, usage) dosubscription = Plausible.Teams.Billing.get_subscription(team)@@ -225,12 +226,12 @@ defmodule Plausible.Billing.Quota doendcond do- Plausible.Teams.locked?(team) or Plausible.Teams.GracePeriod.expired?(team) ->- :dashboard_locked-not Plausible.Teams.on_trial?(team) and is_nil(subscription) ->:trial_ended+ Plausible.Teams.locked?(team) or Plausible.Teams.GracePeriod.expired?(team) ->+ :dashboard_locked+Plausible.Teams.GracePeriod.manual_lock_active?(team) ->:manual_lock_grace_period_active@@ -258,11 +259,18 @@ defmodule Plausible.Billing.Quota dodefp pageview_cycle_usage_notification_type(usage, limit) doexceeded = exceeded_cycles(usage, limit, with_margin: false)+ current_exceeded? = :current_cycle in exceeded+ last_exceeded? = :last_cycle in exceeded+ penultimate_exceeded? = :penultimate_cycle in exceeded+cond do- :penultimate_cycle in exceeded and :last_cycle in exceeded ->+ (penultimate_exceeded? and last_exceeded?) or (last_exceeded? and current_exceeded?) ->:traffic_exceeded_sustained- :last_cycle in exceeded ->+ current_exceeded? ->+ :traffic_exceeded_current_cycle++ last_exceeded? ->:traffic_exceeded_last_cycleis_map_key(usage, :current_cycle) and usage.current_cycle.total >= limit * 0.9 ->diff --git a/lib/plausible_web/components/billing/notice.ex b/lib/plausible_web/components/billing/notice.exindex c36465679c..9c6e19c36c 100644--- a/lib/plausible_web/components/billing/notice.ex+++ b/lib/plausible_web/components/billing/notice.ex@@ -71,8 +71,14 @@ defmodule PlausibleWeb.Components.Billing.Notice dodef limit_exceeded(assigns) do~H"""- <.notice {@rest} title="Notice" data-test="limit-exceeded-notice">- {account_label(@current_team)} is limited to {pretty_print_resource_limit(@limit, @resource)}. To increase this limit,+ <.notice+ {@rest}+ title={"#{account_label(@current_team)} is limited to #{pretty_print_resource_limit(@limit, @resource)}"}+ data-test="limit-exceeded-notice"+ theme={:gray}+ show_icon={false}+ >+ To add more {@resource},<PlausibleWeb.Components.Billing.upgrade_call_to_actioncurrent_team={@current_team}current_user={@current_user}@@ -344,6 +350,30 @@ defmodule PlausibleWeb.Components.Billing.Notice do"""end+ def usage_notification(%{type: :traffic_exceeded_current_cycle} = assigns) do+ ~H"""+ <.notice title="Traffic has exceeded your plan limit this cycle" theme={:gray} show_icon={false}>+ <div class="flex flex-col gap-4">+ <p class="text-pretty">+ No action is required. Occasional traffic spikes are normal, but upgrading now gives you room to grow if higher traffic continues.+ </p>+ <div class="flex gap-3 items-center">+ <.button_link href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} mt?={false}>+ Upgrade+ </.button_link>+ <.button_link+ href="https://plausible.io/docs/subscription-plans"+ theme="secondary"+ mt?={false}+ >+ Learn more+ </.button_link>+ </div>+ </div>+ </.notice>+ """+ end+def usage_notification(%{type: :traffic_exceeded_last_cycle} = assigns) do~H"""<.notice title="Traffic exceeded your plan limit last cycle" theme={:gray} show_icon={false}>diff --git a/lib/plausible_web/controllers/settings_controller.ex b/lib/plausible_web/controllers/settings_controller.exindex a75753fec3..dcff1ca414 100644--- a/lib/plausible_web/controllers/settings_controller.ex+++ b/lib/plausible_web/controllers/settings_controller.ex@@ -140,16 +140,29 @@ defmodule PlausibleWeb.SettingsController doinvoices = Plausible.Billing.paddle_api().get_invoices(subscription)+ pageview_usage = Teams.Billing.monthly_pageview_usage(team)+ site_usage = Teams.Billing.site_usage(team)+ team_member_usage = Teams.Billing.team_member_usage(team)++ usage = %{+ monthly_pageviews: pageview_usage,+ sites: site_usage,+ team_members: team_member_usage+ }++ notification_type = Plausible.Billing.Quota.usage_notification_type(team, usage)+render(conn, :subscription,layout: {PlausibleWeb.LayoutView, :settings},subscription: subscription,invoices: invoices,pageview_limit: Teams.Billing.monthly_pageview_limit(subscription),- pageview_usage: Teams.Billing.monthly_pageview_usage(team),- site_usage: Teams.Billing.site_usage(team),+ pageview_usage: pageview_usage,+ site_usage: site_usage,site_limit: Teams.Billing.site_limit(team),team_member_limit: Teams.Billing.team_member_limit(team),- team_member_usage: Teams.Billing.team_member_usage(team)+ team_member_usage: team_member_usage,+ notification_type: notification_type)enddiff --git a/lib/plausible_web/templates/settings/subscription.html.heex b/lib/plausible_web/templates/settings/subscription.html.heexindex 668b80e1fd..6764bcb159 100644--- a/lib/plausible_web/templates/settings/subscription.html.heex+++ b/lib/plausible_web/templates/settings/subscription.html.heex@@ -98,46 +98,76 @@<a id="subscription">Monthly usage</a></:title>- <PlausibleWeb.Components.Billing.render_monthly_pageview_usage- usage={@pageview_usage}- limit={@pageview_limit}- />+ <div class="flex flex-col gap-12">+ <PlausibleWeb.Components.Billing.Notice.usage_notification+ :if={+ @notification_type in [+ :pageview_approaching_limit,+ :traffic_exceeded_current_cycle,+ :traffic_exceeded_last_cycle,+ :traffic_exceeded_sustained,+ :grace_period_active,+ :manual_lock_grace_period_active,+ :dashboard_locked+ ]+ }+ type={@notification_type}+ team={@current_team}+ />+ <PlausibleWeb.Components.Billing.render_monthly_pageview_usage+ usage={@pageview_usage}+ limit={@pageview_limit}+ />+ </div></.tile><.tile docs="subscription-plans"><:title>Site and team usage</:title>- <div class="grid grid-cols-2 gap-8">- <div class="flex flex-col gap-3">- <PlausibleWeb.Components.Billing.usage_progress_bar- id="site-usage-row"- usage={@site_usage}- limit={@site_limit}- />- <div class="flex justify-between text-sm font-medium text-gray-900 dark:text-gray-100">- <span>Owned sites</span>- <span data-test-id="sites-usage">- {PlausibleWeb.TextHelpers.number_format(@site_usage)}- {if is_number(@site_limit),- do: "/ #{PlausibleWeb.TextHelpers.number_format(@site_limit)}"}- {if @site_limit == :unlimited, do: "/ Unlimited"}- </span>+ <div class="flex flex-col gap-8">+ <PlausibleWeb.Components.Billing.Notice.usage_notification+ :if={+ @notification_type in [+ :site_limit_reached,+ :team_member_limit_reached,+ :site_and_team_member_limit_reached+ ]+ }+ type={@notification_type}+ team={@current_team}+ />+ <div class="grid grid-cols-2 gap-8">+ <div class="flex flex-col gap-3">+ <PlausibleWeb.Components.Billing.usage_progress_bar+ id="site-usage-row"+ usage={@site_usage}+ limit={@site_limit}+ />+ <div class="flex justify-between text-sm font-medium text-gray-900 dark:text-gray-100">+ <span>Owned sites</span>+ <span data-test-id="sites-usage">+ {PlausibleWeb.TextHelpers.number_format(@site_usage)}+ {if is_number(@site_limit),+ do: "/ #{PlausibleWeb.TextHelpers.number_format(@site_limit)}"}+ {if @site_limit == :unlimited, do: "/ Unlimited"}+ </span>+ </div></div>- </div>- <div class="flex flex-col gap-3">- <PlausibleWeb.Components.Billing.usage_progress_bar- id="team-member-usage-row"- usage={@team_member_usage}- limit={@team_member_limit}- />- <div class="flex justify-between text-sm font-medium text-gray-900 dark:text-gray-100">- <span>Team members</span>- <span data-test-id="team-member-usage">- {PlausibleWeb.TextHelpers.number_format(@team_member_usage)}- {if is_number(@team_member_limit),- do: "/ #{PlausibleWeb.TextHelpers.number_format(@team_member_limit)}"}- {if @team_member_limit == :unlimited, do: "/ Unlimited"}- </span>+ <div class="flex flex-col gap-3">+ <PlausibleWeb.Components.Billing.usage_progress_bar+ id="team-member-usage-row"+ usage={@team_member_usage}+ limit={@team_member_limit}+ />+ <div class="flex justify-between text-sm font-medium text-gray-900 dark:text-gray-100">+ <span>Team members</span>+ <span data-test-id="team-member-usage">+ {PlausibleWeb.TextHelpers.number_format(@team_member_usage)}+ {if is_number(@team_member_limit),+ do: "/ #{PlausibleWeb.TextHelpers.number_format(@team_member_limit)}"}+ {if @team_member_limit == :unlimited, do: "/ Unlimited"}+ </span>+ </div></div></div></div>diff --git a/lib/plausible_web/templates/stats/site_locked.html.heex b/lib/plausible_web/templates/stats/site_locked.html.heexindex 6b98b437b0..8fbfc7703e 100644--- a/lib/plausible_web/templates/stats/site_locked.html.heex+++ b/lib/plausible_web/templates/stats/site_locked.html.heex@@ -1,6 +1,6 @@<div class="w-full max-w-lg mx-auto mt-8">- <div class="bg-white dark:bg-gray-800 shadow-sm sm:rounded-lg">- <div class="px-4 py-5 sm:px-8 sm:py-6">+ <div class="mx-3 bg-white dark:bg-gray-800 shadow-sm sm:rounded-lg">+ <div class="p-5 sm:px-10 sm:py-8"><div class="mx-auto flex items-center justify-center rounded-full bg-green-100 h-12 w-12"><svgclass="w-6 h-6 text-green-600"@@ -16,26 +16,26 @@</path></svg></div>- <h3 class="mt-6 text-center text-2xl leading-6 font-medium text-gray-900 dark:text-gray-200">+ <h3 class="mt-6 text-center text-lg sm:text-xl leading-6 font-medium text-gray-900 dark:text-gray-200"><%= if @conn.assigns[:only_shared_link_access_missing?] do %>- Shared Link Unavailable+ Shared link unavailable<% else %>- Dashboard Locked+ Your dashboard is unavailable<% end %></h3><%= case @conn.assigns do %><% %{only_shared_link_access_missing?: true} -> %>- <p class="mt-6 text-gray-600 dark:text-gray-300 text-center">+ <p class="mt-4 text-gray-600 dark:text-gray-300 text-center text-sm">This shared link is locked because the owner of the site does not have access to the Shared Links feature. To restore it, the owner must upgrade to a suitable plan.</p><% %{site_role: role} when role in [:owner, :billing] -> %>- <div class="mt-3 text-gray-600 dark:text-gray-300 text-center">+ <div class="mt-4 text-gray-600 dark:text-gray-300 text-center text-sm"><p>- This dashboard is locked because you don't have a valid subscription.- Please subscribe to the appropriate tier with the link below to access the stats again.+ It looks like you don't have a valid subscription for this dashboard.+ Upgrade to the appropriate plan to restore access to your analytics.</p>- <p class="mt-6 text-sm text-gray-500">+ <p class="mt-4 text-sm text-gray-600 dark:text-gray-300">You can configure your<.styled_link href={Routes.site_path(@conn, :settings_general, @site.domain)}>site settings@@ -43,22 +43,22 @@but your access to the dashboard is restricted.</p></div>- <div class="mt-6 w-full text-center">+ <div class="w-full text-center"><.button_link href={Routes.settings_path(@conn, :subscription)}>Manage my subscription</.button_link></div><% %{site_role: role} when role in [:admin, :viewer, :editor] -> %>- <div class="mt-3 text-gray-600 dark:text-gray-300 text-center">+ <div class="mt-3 text-gray-600 dark:text-gray-300 text-center text-sm"><p>- Owner of this site must upgrade their subscription plan in order to unlock the stats.+ The owner of this site must upgrade their subscription plan to unlock the stats.</p></div><div class="mt-6 w-full text-center"><.button_link href={Routes.site_path(@conn, :index)}>Back to my sites</.button_link></div><% _ -> %>- <div class="mt-3 text-gray-600 dark:text-gray-300 text-center">+ <div class="mt-3 text-gray-600 dark:text-gray-300 text-center text-sm"><p>This dashboard is currently locked and cannot be accessed.You can check back later or contact the site owner to unlock the stats.diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exsindex a7303bba35..1ab14af1d2 100644--- a/test/plausible/billing/quota_test.exs+++ b/test/plausible/billing/quota_test.exs@@ -1189,7 +1189,7 @@ defmodule Plausible.Billing.QuotaTest doassert Plausible.Billing.Quota.usage_notification_type(team, usage) == :trial_endedend- test "returns :traffic_exceeded_sustained when both last cycles exceeded" do+ test "returns :traffic_exceeded_sustained when penultimate and last cycles both exceeded" douser = new_user() |> subscribe_to_growth_plan()team = team_of(user)@@ -1207,6 +1207,60 @@ defmodule Plausible.Billing.QuotaTest do:traffic_exceeded_sustainedend+ test "returns :traffic_exceeded_sustained when last and current cycles both exceeded" do+ user = new_user() |> subscribe_to_growth_plan()+ team = team_of(user)++ usage = %{+ monthly_pageviews: %{+ current_cycle: %{total: 10_500},+ last_cycle: %{total: 10_200},+ penultimate_cycle: %{total: 5000}+ },+ sites: 3,+ team_members: 2+ }++ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==+ :traffic_exceeded_sustained+ end++ test "returns :traffic_exceeded_current_cycle when only current cycle exceeded" do+ user = new_user() |> subscribe_to_growth_plan()+ team = team_of(user)++ usage = %{+ monthly_pageviews: %{+ current_cycle: %{total: 10_500},+ last_cycle: %{total: 8000},+ penultimate_cycle: %{total: 7000}+ },+ sites: 3,+ team_members: 2+ }++ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==+ :traffic_exceeded_current_cycle+ end++ test "returns :traffic_exceeded_current_cycle when penultimate and current exceeded but not last" do+ user = new_user() |> subscribe_to_growth_plan()+ team = team_of(user)++ usage = %{+ monthly_pageviews: %{+ current_cycle: %{total: 10_500},+ last_cycle: %{total: 8000},+ penultimate_cycle: %{total: 10_200}+ },+ sites: 3,+ team_members: 2+ }++ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==+ :traffic_exceeded_current_cycle+ end+test "returns :traffic_exceeded_last_cycle when only last cycle exceeded" douser = new_user() |> subscribe_to_growth_plan()team = team_of(user)diff --git a/test/plausible_web/components/billing/notice_test.exs b/test/plausible_web/components/billing/notice_test.exsindex 4448637d02..a0976607b8 100644--- a/test/plausible_web/components/billing/notice_test.exs+++ b/test/plausible_web/components/billing/notice_test.exs@@ -12,10 +12,11 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: user,current_team: team,limit: 10,- resource: "users"+ resource: "members")- assert rendered =~ "This account is limited to 10 users. To increase this limit"+ assert rendered =~ "This account is limited to 10 members"+ assert rendered =~ "To add more members"assert rendered =~ "upgrade your subscription"assert rendered =~ "/billing/choose-plan"end@@ -28,10 +29,11 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: user,current_team: team_of(user),limit: 1,- resource: "users"+ resource: "members")- assert rendered =~ "This account is limited to a single user. To increase this limit"+ assert rendered =~ "This account is limited to a single member"+ assert rendered =~ "To add more members"assert rendered =~ "upgrade your subscription"assert rendered =~ "/billing/choose-plan"end@@ -46,10 +48,10 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: editor,current_team: team,limit: 10,- resource: "users"+ resource: "members")- assert rendered =~ "This team is limited to 10 users"+ assert rendered =~ "This team is limited to 10 members"assert rendered =~ "ask your team owner to upgrade their subscription"end@@ -62,10 +64,10 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: user,current_team: team_of(user),limit: 10,- resource: "users"+ resource: "members")- assert rendered =~ "This account is limited to 10 users"+ assert rendered =~ "This account is limited to 10 members"assert rendered =~ "upgrade your subscription"assert rendered =~ "/billing/choose-plan"end@@ -79,10 +81,11 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: user,current_team: team_of(user),limit: 10,- resource: "users"+ resource: "members")- assert rendered =~ "This account is limited to 10 users."+ assert rendered =~ "This account is limited to 10 members"+ assert rendered =~ "To add more members"assert rendered =~ "[email protected]"assert rendered =~ "upgrade your subscription"@@ -98,10 +101,11 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest docurrent_user: user,current_team: team,limit: 10,- resource: "users"+ resource: "members")- assert rendered =~ "This account is limited to 10 users."+ assert rendered =~ "This account is limited to 10 members"+ assert rendered =~ "To add more members"assert rendered =~ "[email protected]"assert rendered =~ "upgrade your subscription"@@ -166,6 +170,22 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest doassert rendered =~ "Upgrade"end+ test "renders traffic_exceeded_current_cycle notification" do+ user = new_user()+ team = team_of(user)++ rendered =+ render_component(&Notice.usage_notification/1,+ type: :traffic_exceeded_current_cycle,+ team: team+ )++ assert rendered =~ "Traffic has exceeded your plan limit this cycle"+ assert rendered =~ "Occasional traffic spikes are normal"+ assert rendered =~ "Upgrade"+ assert rendered =~ "Learn more"+ end+test "renders traffic_exceeded_last_cycle notification" douser = new_user()team = team_of(user)diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exsindex 788ad54f98..26031013a3 100644--- a/test/plausible_web/controllers/stats_controller_test.exs+++ b/test/plausible_web/controllers/stats_controller_test.exs@@ -239,8 +239,8 @@ defmodule PlausibleWeb.StatsControllerTest dolocked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()conn = get(conn, "/" <> locked_site.domain)resp = html_response(conn, 200)- assert resp =~ "Dashboard Locked"- assert resp =~ "Please subscribe to the appropriate tier with the link below"+ assert resp =~ "Your dashboard is unavailable"+ assert resp =~ "Upgrade to the appropriate plan to restore access"endtest "shows locked page if site is locked for billing role", %{conn: conn, user: user} do@@ -251,8 +251,8 @@ defmodule PlausibleWeb.StatsControllerTest doconn = get(conn, "/" <> locked_site.domain)resp = html_response(conn, 200)- assert resp =~ "Dashboard Locked"- assert resp =~ "Please subscribe to the appropriate tier with the link below"+ assert resp =~ "Your dashboard is unavailable"+ assert resp =~ "Upgrade to the appropriate plan to restore access"endtest "shows locked page if site is locked for viewer role", %{conn: conn, user: user} do@@ -263,9 +263,9 @@ defmodule PlausibleWeb.StatsControllerTest doconn = get(conn, "/" <> locked_site.domain)resp = html_response(conn, 200)- assert resp =~ "Dashboard Locked"- refute resp =~ "Please subscribe to the appropriate tier with the link below"- assert resp =~ "Owner of this site must upgrade their subscription plan"+ assert resp =~ "Your dashboard is unavailable"+ refute resp =~ "Upgrade to the appropriate plan to restore access"+ assert resp =~ "The owner of this site must upgrade their subscription plan"endtest "shows locked page for anonymous" do@@ -273,7 +273,7 @@ defmodule PlausibleWeb.StatsControllerTest dolocked_site.team |> Ecto.Changeset.change(locked: true) |> Repo.update!()conn = get(build_conn(), "/" <> locked_site.domain)resp = html_response(conn, 200)- assert resp =~ "Dashboard Locked"+ assert resp =~ "Your dashboard is unavailable"assert resp =~ "You can check back later or contact the site owner"end@@ -1433,7 +1433,7 @@ defmodule PlausibleWeb.StatsControllerTest doconn = get(conn, "/share/test-site.com/?auth=#{link.slug}")- assert html_response(conn, 200) =~ "Dashboard Locked"+ assert html_response(conn, 200) =~ "Your dashboard is unavailable"refute String.contains?(html_response(conn, 200), "Back to my sites")end@@ -1447,7 +1447,7 @@ defmodule PlausibleWeb.StatsControllerTest doconn = get(conn, "/share/test-site.com/?auth=#{link.slug}")- assert html_response(conn, 200) =~ "Shared Link Unavailable"+ assert html_response(conn, 200) =~ "Shared link unavailable"refute String.contains?(html_response(conn, 200), "Back to my sites")end@@ -1708,7 +1708,7 @@ defmodule PlausibleWeb.StatsControllerTest doscript_params = html |> get_script_params()- assert html =~ "Dashboard Locked"+ assert html =~ "Your dashboard is unavailable"assert script_params["location_override"] == PlausibleWeb.Endpoint.url() <> "/:dashboard"end@@ -1723,7 +1723,7 @@ defmodule PlausibleWeb.StatsControllerTest doscript_params = get_script_params(html)- assert html =~ "Shared Link Unavailable"+ assert html =~ "Shared link unavailable"assert script_params["location_override"] ==PlausibleWeb.Endpoint.url() <> "/share/:dashboard"