Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Add usage notifications for billing limits (#6056)

Sanne de Vries31198391b232
diff --git a/lib/plausible/billing/qouta/quota.ex b/lib/plausible/billing/qouta/quota.ex
index 2b0346da8f..3ab0741945 100644
--- a/lib/plausible/billing/qouta/quota.ex
+++ b/lib/plausible/billing/qouta/quota.ex
@@ -114,14 +114,12 @@ defmodule Plausible.Billing.Quota do
if Keyword.get(opts, :ignore_pageview_limit) do
false
else
- case usage do
- %{last_30_days: %{total: total}} ->
- margin = Keyword.get(opts, :pageview_allowance_margin)
- limit = Limits.pageview_limit_with_margin(plan.monthly_pageview_limit, margin)
- !within_limit?(total, limit)
-
- cycles_usage ->
- exceeds_last_two_usage_cycles?(cycles_usage, plan.monthly_pageview_limit)
+ if has_billing_cycles?(usage) do
+ exceeds_last_two_usage_cycles?(usage, plan.monthly_pageview_limit)
+ else
+ margin = Keyword.get(opts, :pageview_allowance_margin)
+ limit = Limits.pageview_limit_with_margin(plan.monthly_pageview_limit, margin)
+ !within_limit?(usage.last_30_days.total, limit)
end
end
end
@@ -133,9 +131,14 @@ defmodule Plausible.Billing.Quota do
:penultimate_cycle in exceeded && :last_cycle in exceeded
end
- @spec exceeded_cycles(cycles_usage(), non_neg_integer()) :: list()
- def exceeded_cycles(cycles_usage, allowed_volume) do
- limit = Limits.pageview_limit_with_margin(allowed_volume)
+ @spec exceeded_cycles(cycles_usage(), non_neg_integer(), keyword()) :: list()
+ def exceeded_cycles(cycles_usage, allowed_volume, opts \\ []) do
+ limit =
+ if Keyword.get(opts, :with_margin, true) do
+ Limits.pageview_limit_with_margin(allowed_volume)
+ else
+ allowed_volume
+ end
Enum.reduce(cycles_usage, [], fn {cycle, %{total: total}}, exceeded_cycles ->
if below_limit?(total, limit) do
@@ -163,4 +166,110 @@ defmodule Plausible.Billing.Quota do
def within_limit?(usage, limit) do
if limit == :unlimited, do: true, else: usage <= limit
end
+
+ @doc """
+ Returns whether the usage has billing cycle data.
+ """
+ def has_billing_cycles?(usage) do
+ not is_map_key(usage, :last_30_days)
+ end
+
+ @doc """
+ Determines which notification type should be shown based on current usage and limits.
+
+ Returns an atom representing the notification type, or nil if no notification should be shown.
+
+ ## Pageview limit enforcement
+
+ Pageview limit notifications use different thresholds for warnings vs enforcement:
+
+ - Warning notifications (approaching, exceeded): Check against base limit (e.g., 10k)
+ to notify users when they exceed their purchased plan allowance
+ - Enforcement (grace period & locking): The background job checks limit with 10% margin
+ (e.g., 11k) and starts a 7-day grace period when both cycles exceed this threshold. After
+ the grace period expires, dashboard access is locked.
+
+ Example 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)
+ - 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
+ 3. 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
+ """
+ def usage_notification_type(team, usage) do
+ subscription = Plausible.Teams.Billing.get_subscription(team)
+ pageview_limit = Plausible.Teams.Billing.monthly_pageview_limit(subscription)
+ site_limit = Plausible.Teams.Billing.site_limit(team)
+ team_member_limit = Plausible.Teams.Billing.team_member_limit(team)
+
+ pageview_usage = usage.monthly_pageviews
+ site_usage = usage.sites
+ team_member_usage = usage.team_members
+
+ pageview_notification =
+ if not Plausible.Teams.on_trial?(team) and has_billing_cycles?(pageview_usage) do
+ pageview_cycle_usage_notification_type(pageview_usage, pageview_limit)
+ end
+
+ cond 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.GracePeriod.manual_lock_active?(team) ->
+ :manual_lock_grace_period_active
+
+ Plausible.Teams.GracePeriod.active?(team) ->
+ :grace_period_active
+
+ pageview_notification ->
+ pageview_notification
+
+ site_usage >= site_limit and site_limit != :unlimited and
+ team_member_usage >= team_member_limit and team_member_limit != :unlimited ->
+ :site_and_team_member_limit_reached
+
+ site_usage >= site_limit and site_limit != :unlimited ->
+ :site_limit_reached
+
+ team_member_usage >= team_member_limit and team_member_limit != :unlimited ->
+ :team_member_limit_reached
+
+ true ->
+ nil
+ end
+ end
+
+ defp pageview_cycle_usage_notification_type(usage, limit) do
+ exceeded = exceeded_cycles(usage, limit, with_margin: false)
+
+ cond do
+ :penultimate_cycle in exceeded and :last_cycle in exceeded ->
+ :traffic_exceeded_sustained
+
+ :last_cycle in exceeded ->
+ :traffic_exceeded_last_cycle
+
+ is_map_key(usage, :current_cycle) and usage.current_cycle.total >= limit * 0.9 ->
+ :pageview_approaching_limit
+
+ true ->
+ nil
+ end
+ end
end
diff --git a/lib/plausible/teams/grace_period.ex b/lib/plausible/teams/grace_period.ex
index c75d24df1d..67a0c64611 100644
--- a/lib/plausible/teams/grace_period.ex
+++ b/lib/plausible/teams/grace_period.ex
@@ -91,6 +91,18 @@ defmodule Plausible.Teams.GracePeriod do
def active?(_team), do: false
+ @spec manual_lock_active?(Teams.Team.t() | nil) :: boolean()
+ @doc """
+ Returns whether the team has an active manual lock grace period.
+ Manual locks are used for enterprise customers and have no end date.
+ """
+ def manual_lock_active?(team)
+
+ def manual_lock_active?(%{grace_period: %__MODULE__{manual_lock: true, is_over: false}}),
+ do: true
+
+ def manual_lock_active?(_team), do: false
+
@spec expired?(Teams.Team.t() | nil) :: boolean()
@doc """
Returns whether the grace period has already expired for a Team. Defaults to
@@ -99,4 +111,40 @@ defmodule Plausible.Teams.GracePeriod do
def expired?(team) do
if team && team.grace_period, do: !active?(team), else: false
end
+
+ @spec expires_in(Teams.Team.t() | nil) :: {non_neg_integer(), :days | :hours} | nil
+ @doc """
+ Returns a tuple representing either the hours (if hours_left < 48) or days left
+ until the end of a grace period. Switching to hours near the end is to avoid
+ confusion with timezones.
+
+ Returns `nil` in all the following cases:
+
+ * the given team is `nil`
+ * the given team does not have a grace period
+ * the given team has a manual lock grace period
+ """
+ def expires_in(team, now \\ NaiveDateTime.utc_now(:second))
+
+ def expires_in(%Teams.Team{grace_period: %__MODULE__{end_date: %Date{} = end_date}}, now) do
+ case full_hours_left(end_date, now) do
+ hours when hours < 48 -> {hours, :hours}
+ _ -> {days_left(end_date, now), :days}
+ end
+ end
+
+ def expires_in(_, _), do: nil
+
+ defp days_left(%Date{} = end_date, now) do
+ today = NaiveDateTime.to_date(now)
+ Date.diff(end_date, today)
+ end
+
+ defp full_hours_left(%Date{} = end_date, now) do
+ end_date
+ |> NaiveDateTime.new!(~T[00:00:00])
+ |> NaiveDateTime.diff(now, :second)
+ |> max(0)
+ |> div(3600)
+ end
end
diff --git a/lib/plausible_web/components/billing/notice.ex b/lib/plausible_web/components/billing/notice.ex
index 491eaff930..401c016cf3 100644
--- a/lib/plausible_web/components/billing/notice.ex
+++ b/lib/plausible_web/components/billing/notice.ex
@@ -265,6 +265,223 @@ defmodule PlausibleWeb.Components.Billing.Notice do
"""
end
+ @doc """
+ Renders a usage notification banner based on the notification type.
+
+ This is used on the subscription settings page to show contextual
+ notifications about approaching or exceeded limits.
+ """
+ attr(:type, :atom, required: true)
+ attr(:team, :any, required: true)
+
+ def usage_notification(assigns)
+
+ def usage_notification(%{type: :pageview_approaching_limit} = assigns) do
+ ~H"""
+ <.notice title="You're close to your monthly pageview limit" 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, and we'll keep tracking your stats as usual. 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: :team_member_limit_reached} = assigns) do
+ ~H"""
+ <.notice title="You've reached your current team member limit" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4 items-start">
+ <p class="text-pretty">
+ Upgrading lets you add more as your team grows.
+ </p>
+ <.button_link href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} mt?={false}>
+ Upgrade
+ </.button_link>
+ </div>
+ </.notice>
+ """
+ end
+
+ def usage_notification(%{type: :site_limit_reached} = assigns) do
+ ~H"""
+ <.notice title="You've reached your current site limit" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4 items-start">
+ <p class="text-pretty">
+ Upgrading lets you add more sites as you grow.
+ </p>
+ <.button_link href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} mt?={false}>
+ Upgrade
+ </.button_link>
+ </div>
+ </.notice>
+ """
+ end
+
+ def usage_notification(%{type: :site_and_team_member_limit_reached} = assigns) do
+ ~H"""
+ <.notice
+ title="You've reached your current limits for team members and sites"
+ theme={:gray}
+ show_icon={false}
+ >
+ <div class="flex flex-col gap-4 items-start">
+ <p class="text-pretty">
+ Upgrading gives you room to grow.
+ </p>
+ <.button_link href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} mt?={false}>
+ Upgrade
+ </.button_link>
+ </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}>
+ <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_sustained} = assigns) do
+ ~H"""
+ <.notice title="Upgrade required due to sustained higher traffic" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4">
+ <p class="text-pretty">
+ To ensure uninterrupted access to your stats, please upgrade to a plan that fits your current usage.
+ </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: :manual_lock_grace_period_active} = assigns) do
+ ~H"""
+ <.notice title="You've outgrown your custom plan" theme={:yellow} show_icon={false}>
+ <p class="text-pretty">
+ We'll contact you by email to discuss an updated custom plan based on your current usage.
+ </p>
+ </.notice>
+ """
+ end
+
+ def usage_notification(%{type: :grace_period_active, team: team} = assigns) do
+ deadline_text =
+ case Plausible.Teams.GracePeriod.expires_in(team) do
+ {0, :hours} -> "within the hour"
+ {1, :hours} -> "within the next hour"
+ {n, :hours} -> "within the next #{n} hours"
+ {n, :days} -> "within the next #{n} days"
+ end
+
+ assigns = assign(assigns, :deadline_text, deadline_text)
+
+ ~H"""
+ <.notice title="Upgrade required due to sustained higher traffic" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4">
+ <p class="text-pretty">
+ To ensure uninterrupted access to your stats, please upgrade to a plan that fits your current usage {@deadline_text}.
+ </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: :dashboard_locked} = assigns) do
+ ~H"""
+ <.notice title="Dashboard access temporarily locked" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4">
+ <p class="text-pretty">
+ Your stats are still being tracked, but dashboard access is temporarily locked because your site exceeded your plan's pageview limit for two consecutive billing cycles. Upgrade to restore access.
+ </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: :trial_ended} = assigns) do
+ ~H"""
+ <.notice title="Your free trial has ended" theme={:gray} show_icon={false}>
+ <div class="flex flex-col gap-4 items-start">
+ <p class="text-pretty">
+ Upgrade to a monthly or yearly plan to continue accessing your sites.
+ </p>
+ <.button_link href={Routes.billing_path(PlausibleWeb.Endpoint, :choose_plan)} mt?={false}>
+ Choose a plan
+ </.button_link>
+ </div>
+ </.notice>
+ """
+ end
+
+ def usage_notification(assigns), do: ~H""
+
defp subscription_cancelled_notice_body(assigns) do
~H"""
<p>
diff --git a/lib/plausible_web/components/generic.ex b/lib/plausible_web/components/generic.ex
index 3f14b6a693..7ee6e0ce42 100644
--- a/lib/plausible_web/components/generic.ex
+++ b/lib/plausible_web/components/generic.ex
@@ -9,7 +9,7 @@ defmodule PlausibleWeb.Components.Generic do
bg: "bg-gray-100 dark:bg-gray-800",
icon: "text-gray-600 dark:text-gray-300",
title_text: "text-sm text-gray-900 dark:text-gray-100",
- body_text: "text-sm text-gray-600 dark:text-gray-300 leading-5"
+ body_text: "text-sm text-gray-800 dark:text-gray-200 leading-5"
},
yellow: %{
bg: "bg-yellow-100/60 dark:bg-yellow-900/40",
@@ -186,6 +186,7 @@ defmodule PlausibleWeb.Components.Generic do
attr(:title, :any, default: nil)
attr(:theme, :atom, default: :yellow)
attr(:dismissable_id, :any, default: nil)
+ attr(:show_icon, :boolean, default: true)
attr(:class, :string, default: "")
attr(:rest, :global)
slot(:inner_block)
@@ -204,7 +205,7 @@ defmodule PlausibleWeb.Components.Generic do
<Heroicons.x_mark class="h-4 w-4 hover:stroke-2" />
</button>
<div class="flex gap-x-3">
- <div :if={@title} class="shrink-0">
+ <div :if={@show_icon && @title} class="shrink-0">
<svg
class={"h-5 w-5 #{@theme.icon}"}
viewBox="0 0 20 20"
@@ -219,7 +220,7 @@ defmodule PlausibleWeb.Components.Generic do
</svg>
</div>
<div class="w-full flex flex-col gap-y-1.5">
- <h3 :if={@title} class={"font-medium #{@theme.title_text}"}>
+ <h3 :if={@title} class={"font-semibold #{@theme.title_text}"}>
{@title}
</h3>
<div class={"#{@theme.body_text}"}>
diff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heex
index 969c3d1364..721aba034e 100644
--- a/lib/plausible_web/templates/layout/_notice.html.heex
+++ b/lib/plausible_web/templates/layout/_notice.html.heex
@@ -6,7 +6,6 @@
<Notice.active_grace_period
:if={Plausible.Teams.GracePeriod.active?(@current_team)}
enterprise?={Plausible.Teams.Billing.enterprise_configured?(@current_team)}
- grace_period_end={grace_period_end(@current_team)}
/>
<Notice.dashboard_locked :if={Plausible.Teams.GracePeriod.expired?(@current_team)} />
diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex
index 32b471a4a1..3c90a58ada 100644
--- a/lib/plausible_web/views/layout_view.ex
+++ b/lib/plausible_web/views/layout_view.ex
@@ -248,16 +248,6 @@ defmodule PlausibleWeb.LayoutView do
end
end
- def grace_period_end(%{grace_period: %{end_date: %Date{} = date}}) do
- case Date.diff(date, Date.utc_today()) do
- 0 -> "today"
- 1 -> "tomorrow"
- n -> "within #{n} days"
- end
- end
-
- def grace_period_end(_user), do: "in the following days"
-
@doc "http://blog.plataformatec.com.br/2018/05/nested-layouts-with-phoenix/"
def render_layout(layout, assigns, do: content) do
render(layout, Map.put(assigns, :inner_layout, content))
diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exs
index a08d69b433..a7303bba35 100644
--- a/test/plausible/billing/quota_test.exs
+++ b/test/plausible/billing/quota_test.exs
@@ -1095,4 +1095,235 @@ defmodule Plausible.Billing.QuotaTest do
end
end
end
+
+ describe "usage_notification_type/2" do
+ @describetag :ee_only
+
+ test "returns :dashboard_locked when grace period has expired" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ expired_grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Date.add(Date.utc_today(), -1),
+ is_over: false,
+ manual_lock: false
+ }
+
+ team = %{team | grace_period: expired_grace_period}
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 5000},
+ last_cycle: %{total: 15_000},
+ penultimate_cycle: %{total: 14_000}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) == :dashboard_locked
+ end
+
+ test "returns :grace_period_active when both cycles exceeded with margin and grace period active" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ active_grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Date.add(Date.utc_today(), 5),
+ is_over: false,
+ manual_lock: false
+ }
+
+ team = %{team | grace_period: active_grace_period}
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 5000},
+ last_cycle: %{total: 15_000},
+ penultimate_cycle: %{total: 14_000}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :grace_period_active
+ end
+
+ test "returns :manual_lock_grace_period_active when manual lock is active" do
+ user = new_user() |> subscribe_to_enterprise_plan(monthly_pageview_limit: 10_000)
+ team = team_of(user)
+
+ manual_lock_grace_period = %Plausible.Teams.GracePeriod{
+ end_date: nil,
+ is_over: false,
+ manual_lock: true
+ }
+
+ team = %{team | grace_period: manual_lock_grace_period}
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 5000},
+ last_cycle: %{total: 15_000},
+ penultimate_cycle: %{total: 14_000}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :manual_lock_grace_period_active
+ end
+
+ test "returns :trial_ended when trial expired and no subscription" do
+ user = new_user(trial_expiry_date: Date.add(Date.utc_today(), -5))
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{last_30_days: %{total: 5000}},
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) == :trial_ended
+ end
+
+ test "returns :traffic_exceeded_sustained when both last cycles exceeded" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 5000},
+ last_cycle: %{total: 10_500},
+ penultimate_cycle: %{total: 10_200}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :traffic_exceeded_sustained
+ end
+
+ test "returns :traffic_exceeded_last_cycle when only last cycle exceeded" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 5000},
+ last_cycle: %{total: 10_100},
+ penultimate_cycle: %{total: 8000}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :traffic_exceeded_last_cycle
+ end
+
+ test "returns :pageview_approaching_limit when at 90% of limit" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 9000},
+ last_cycle: %{total: 8000}
+ },
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :pageview_approaching_limit
+ end
+
+ test "pageview notification takes precedence over site/member limits" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{
+ current_cycle: %{total: 9000}
+ },
+ sites: 10,
+ team_members: 3
+ }
+
+ # At limits for both sites and members, but pageview approaching takes precedence
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :pageview_approaching_limit
+ end
+
+ test "returns :site_and_team_member_limit_reached when both site and member limits reached" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{current_cycle: %{total: 5000}},
+ sites: 10,
+ team_members: 3
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :site_and_team_member_limit_reached
+ end
+
+ test "returns :site_limit_reached when only site limit reached" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{current_cycle: %{total: 5000}},
+ sites: 10,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) == :site_limit_reached
+ end
+
+ test "returns :team_member_limit_reached when only member limit reached" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{current_cycle: %{total: 5000}},
+ sites: 5,
+ team_members: 3
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) ==
+ :team_member_limit_reached
+ end
+
+ test "returns nil when no notification is needed" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{current_cycle: %{total: 5000}},
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) == nil
+ end
+
+ test "handles :unlimited limits correctly" do
+ user = new_user() |> subscribe_to_growth_plan()
+ team = team_of(user)
+
+ usage = %{
+ monthly_pageviews: %{current_cycle: %{total: 5000}},
+ sites: 3,
+ team_members: 2
+ }
+
+ assert Plausible.Billing.Quota.usage_notification_type(team, usage) == nil
+ end
+ end
end
diff --git a/test/plausible/teams/grace_period_test.exs b/test/plausible/teams/grace_period_test.exs
index 577a967c41..54170eba35 100644
--- a/test/plausible/teams/grace_period_test.exs
+++ b/test/plausible/teams/grace_period_test.exs
@@ -112,4 +112,79 @@ defmodule Plausible.Teams.GracePeriodTest do
refute Plausible.Teams.GracePeriod.active?(team)
refute Plausible.Teams.GracePeriod.expired?(team)
end
+
+ describe "expires_in/1" do
+ test "returns nil for no team" do
+ assert Plausible.Teams.GracePeriod.expires_in(nil) == nil
+ end
+
+ test "returns nil for team without grace period" do
+ team = new_user(team: [grace_period: nil]) |> team_of()
+ refute team.grace_period
+ assert Plausible.Teams.GracePeriod.expires_in(team) == nil
+ end
+
+ test "returns nil for team with a manual_lock grace period" do
+ team =
+ new_user(trial_expiry_date: Date.utc_today())
+ |> team_of()
+ |> Plausible.Teams.GracePeriod.start_manual_lock_changeset()
+ |> Ecto.Changeset.apply_changes()
+
+ assert team.grace_period.manual_lock
+ assert Plausible.Teams.GracePeriod.expires_in(team) == nil
+ end
+
+ test "returns diff in days when at least 48 hours left" do
+ now = ~N[2021-01-01 00:00:00]
+
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: ~D[2021-01-03],
+ is_over: false
+ }
+
+ team = new_user(team: [grace_period: grace_period]) |> team_of()
+
+ assert Plausible.Teams.GracePeriod.expires_in(team, now) == {2, :days}
+ end
+
+ test "returns diff in hours when less than 48 hours left" do
+ now = ~N[2021-01-01 00:00:01]
+
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: ~D[2021-01-03],
+ is_over: false
+ }
+
+ team = new_user(team: [grace_period: grace_period]) |> team_of()
+
+ assert Plausible.Teams.GracePeriod.expires_in(team, now) == {47, :hours}
+ end
+
+ test "returns {0, :hours} when less than an hour left" do
+ now = ~N[2021-01-01 23:00:01]
+
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: ~D[2021-01-02],
+ is_over: false
+ }
+
+ team = new_user(team: [grace_period: grace_period]) |> team_of()
+
+ assert Plausible.Teams.GracePeriod.expires_in(team, now) == {0, :hours}
+ end
+
+ test "returns {0, :hours} when end date in already in the past" do
+ now = ~N[2021-01-01 10:00:00]
+
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: ~D[2021-01-01],
+ is_over: false
+ }
+
+ team = new_user(team: [grace_period: grace_period]) |> team_of()
+
+ assert Plausible.Teams.GracePeriod.expires_in(team, now) == {0, :hours}
+ end
+ end
end
diff --git a/test/plausible_web/components/billing/notice_test.exs b/test/plausible_web/components/billing/notice_test.exs
index bf6835d17d..4448637d02 100644
--- a/test/plausible_web/components/billing/notice_test.exs
+++ b/test/plausible_web/components/billing/notice_test.exs
@@ -106,4 +106,214 @@ defmodule PlausibleWeb.Components.Billing.NoticeTest do
assert rendered =~ "[email protected]"
assert rendered =~ "upgrade your subscription"
end
+
+ describe "usage_notification/1" do
+ test "renders pageview_approaching_limit notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :pageview_approaching_limit,
+ team: team
+ )
+
+ assert rendered =~ "close to your monthly pageview limit"
+ assert rendered =~ "Occasional traffic spikes are normal"
+ assert rendered =~ "Upgrade"
+ assert rendered =~ "Learn more"
+ end
+
+ test "renders team_member_limit_reached notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :team_member_limit_reached,
+ team: team
+ )
+
+ assert rendered =~ "reached your current team member limit"
+ assert rendered =~ "Upgrade"
+ end
+
+ test "renders site_limit_reached notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :site_limit_reached,
+ team: team
+ )
+
+ assert rendered =~ "reached your current site limit"
+ assert rendered =~ "Upgrade"
+ end
+
+ test "renders site_and_team_member_limit_reached notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :site_and_team_member_limit_reached,
+ team: team
+ )
+
+ assert rendered =~ "reached your current limits for team members and sites"
+ assert rendered =~ "Upgrade"
+ end
+
+ test "renders traffic_exceeded_last_cycle notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :traffic_exceeded_last_cycle,
+ team: team
+ )
+
+ assert rendered =~ "Traffic exceeded your plan limit last cycle"
+ assert rendered =~ "Occasional traffic spikes are normal"
+ assert rendered =~ "Upgrade"
+ assert rendered =~ "Learn more"
+ end
+
+ test "renders traffic_exceeded_sustained notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :traffic_exceeded_sustained,
+ team: team
+ )
+
+ assert rendered =~ "Upgrade required due to sustained higher traffic"
+ assert rendered =~ "To ensure uninterrupted access to your stats"
+ refute rendered =~ "within the next"
+ assert rendered =~ "Upgrade"
+ assert rendered =~ "Learn more"
+ end
+
+ test "renders grace_period_active notification with hours (less than 48h)" do
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Date.add(Date.utc_today(), 1),
+ is_over: false,
+ manual_lock: false
+ }
+
+ team =
+ new_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])
+ |> team_of()
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :grace_period_active,
+ team: team
+ )
+
+ assert rendered =~ "Upgrade required due to sustained higher traffic"
+ assert rendered =~ "within the next"
+ assert rendered =~ "hours"
+ refute rendered =~ "days"
+ end
+
+ test "renders grace_period_active notification with days (more than 48h)" do
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: Date.add(Date.utc_today(), 5),
+ is_over: false,
+ manual_lock: false
+ }
+
+ team =
+ new_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])
+ |> team_of()
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :grace_period_active,
+ team: team
+ )
+
+ assert rendered =~ "Upgrade required due to sustained higher traffic"
+ assert rendered =~ "within the next 5 days"
+ assert rendered =~ "Upgrade"
+ assert rendered =~ "Learn more"
+ end
+
+ test "renders manual_lock_grace_period_active notification without countdown" do
+ grace_period = %Plausible.Teams.GracePeriod{
+ end_date: nil,
+ is_over: false,
+ manual_lock: true
+ }
+
+ team =
+ new_user(trial_expiry_date: Date.utc_today(), team: [grace_period: grace_period])
+ |> team_of()
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :manual_lock_grace_period_active,
+ team: team
+ )
+
+ assert rendered =~ "outgrown your custom plan"
+
+ assert rendered =~
+ "contact you by email to discuss an updated custom plan based on your current usage"
+
+ refute rendered =~ "within the next"
+ refute rendered =~ "days"
+ refute rendered =~ "Upgrade"
+ refute rendered =~ "Learn more"
+ end
+
+ test "renders dashboard_locked notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :dashboard_locked,
+ team: team
+ )
+
+ assert rendered =~ "Dashboard access temporarily locked"
+ assert rendered =~ "stats are still being tracked"
+ assert rendered =~ "Upgrade"
+ assert rendered =~ "Learn more"
+ end
+
+ test "renders trial_ended notification" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :trial_ended,
+ team: team
+ )
+
+ assert rendered =~ "Your free trial has ended"
+ assert rendered =~ "Choose a plan"
+ end
+
+ test "renders nothing for unknown notification type" do
+ user = new_user()
+ team = team_of(user)
+
+ rendered =
+ render_component(&Notice.usage_notification/1,
+ type: :unknown_type,
+ team: team
+ )
+
+ assert rendered == ""
+ end
+ end
end