Skip to content

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

Send test billing upgrade notifications

Uku Taht9eff6b03ed81
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index 7fb73a0759..1e605f182a 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -1,6 +1,7 @@
defmodule Plausible.Billing do
use Plausible.Repo
alias Plausible.Billing.{Subscription, PaddleApi}
+ use Plausible.ClickhouseRepo
def active_subscription_for(user_id) do
Repo.get_by(Subscription, user_id: user_id, status: "active")
@@ -134,8 +135,46 @@ defmodule Plausible.Billing do
pageviews + custom_events
end
- def last_two_billing_months_usage(_user) do
- {1, 2}
+ defp get_usage_for_billing_cycle(user, cycle) do
+ domains = Enum.map(user.sites, & &1.domain)
+
+ ClickhouseRepo.one(
+ from e in "events",
+ where: e.domain in ^domains,
+ where: fragment("toDate(?)", e.timestamp) >= ^cycle.first,
+ where: fragment("toDate(?)", e.timestamp) <= ^cycle.last,
+ select: fragment("count(*)")
+ )
+ end
+
+ def last_two_billing_months_usage(user, today \\ Timex.today()) do
+ {first, second} = last_two_billing_cycles(user, today)
+ user = Repo.preload(user, :sites)
+
+ {
+ get_usage_for_billing_cycle(user, first),
+ get_usage_for_billing_cycle(user, second)
+ }
+ end
+
+ def last_two_billing_cycles(user, today \\ Timex.today()) do
+ last_bill_date = user.subscription.last_bill_date
+
+ normalized_last_bill_date =
+ Timex.shift(last_bill_date,
+ months: Timex.diff(today, last_bill_date, :months)
+ )
+
+ {
+ Date.range(
+ Timex.shift(normalized_last_bill_date, months: -2),
+ Timex.shift(normalized_last_bill_date, days: -1, months: -1)
+ ),
+ Date.range(
+ Timex.shift(normalized_last_bill_date, months: -1),
+ Timex.shift(normalized_last_bill_date, days: -1)
+ )
+ }
end
def usage_breakdown(user) do
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 54f6056e85..f1468a767b 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -112,15 +112,16 @@ defmodule PlausibleWeb.Email do
})
end
- def over_limit_email(user, usage) do
+ def over_limit_email(user, usage, last_cycle) do
base_email()
# Temporary testing
+ |> to(["[email protected]"])
|> tag("over-limit")
|> subject("You have outgrown your Plausible subscription tier ")
|> render("over_limit.html", %{
user: user,
- usage: usage
+ usage: usage,
+ last_cycle: last_cycle
})
end
diff --git a/lib/plausible_web/templates/email/over_limit.html.eex b/lib/plausible_web/templates/email/over_limit.html.eex
index caed5a1d41..56f6345a3d 100644
--- a/lib/plausible_web/templates/email/over_limit.html.eex
+++ b/lib/plausible_web/templates/email/over_limit.html.eex
@@ -6,7 +6,7 @@ This is a friendly reminder that your traffic has exceeded your subscription tie
<br /><br />
We don't enforce any hard limits at the moment, we're still counting your stats and you have access to your dashboard, but we kindly ask you to upgrade your subscription plan to accommodate your new traffic levels.
<br /><br />
-In the last month, your account has used <%= @usage %> billable pageviews.
+In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.first) %>), your account has used <%= @usage %> billable pageviews.
<%= if @usage <= 20_000_000 do %>
Based on that we recommend you select the <%= Plausible.Billing.Plans.suggested_plan_name(@usage) %> plan which runs at <%= Plausible.Billing.Plans.suggested_plan_cost(@usage) %>. You can also go with yearly billing to get 33% off.
diff --git a/lib/plausible_web/views/email_view.ex b/lib/plausible_web/views/email_view.ex
index d54a9520aa..5d43bd6677 100644
--- a/lib/plausible_web/views/email_view.ex
+++ b/lib/plausible_web/views/email_view.ex
@@ -20,4 +20,8 @@ defmodule PlausibleWeb.EmailView do
""
end
end
+
+ def date_format(date) do
+ Timex.format!(date, "{D} {Mshort} {YYYY}")
+ end
end
diff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.ex
index cf7e85de7e..5b7b0ca165 100644
--- a/lib/workers/check_usage.ex
+++ b/lib/workers/check_usage.ex
@@ -51,7 +51,8 @@ defmodule Plausible.Workers.CheckUsage do
{last_last_month, last_month} = billing_mod.last_two_billing_months_usage(subscriber)
if last_last_month > allowance && last_month > allowance do
- template = PlausibleWeb.Email.over_limit_email(subscriber, last_month)
+ {_, last_cycle} = billing_mod.last_two_billing_cycles(subscriber)
+ template = PlausibleWeb.Email.over_limit_email(subscriber, last_month, last_cycle)
Plausible.Mailer.send_email(template)
end
end
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index eb1fbaec6c..0e4cc8cda2 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -2,6 +2,7 @@ defmodule Plausible.BillingTest do
use Plausible.DataCase
use Bamboo.Test, shared: true
alias Plausible.Billing
+ import Plausible.TestUtils
describe "usage" do
test "is 0 with no events" do
@@ -18,6 +19,64 @@ defmodule Plausible.BillingTest do
end
end
+ describe "last_two_billing_cycles" do
+ test "billing on the 1st" do
+ last_bill_date = ~D[2021-01-01]
+ today = ~D[2021-01-02]
+
+ user = insert(:user, subscription: build(:subscription, last_bill_date: last_bill_date))
+
+ expected_cycles = {
+ Date.range(~D[2020-11-01], ~D[2020-11-30]),
+ Date.range(~D[2020-12-01], ~D[2020-12-31])
+ }
+
+ assert Billing.last_two_billing_cycles(user, today) == expected_cycles
+ end
+
+ test "in case of yearly billing, cycles are normalized as if they were paying monthly" do
+ last_bill_date = ~D[2020-09-01]
+ today = ~D[2021-02-02]
+
+ user = insert(:user, subscription: build(:subscription, last_bill_date: last_bill_date))
+
+ expected_cycles = {
+ Date.range(~D[2020-12-01], ~D[2020-12-31]),
+ Date.range(~D[2021-01-01], ~D[2021-01-31])
+ }
+
+ assert Billing.last_two_billing_cycles(user, today) == expected_cycles
+ end
+ end
+
+ describe "last_two_billing_months_usage" do
+ test "counts events from last two billing cycles" do
+ last_bill_date = ~D[2021-01-01]
+ today = ~D[2021-01-02]
+ user = insert(:user, subscription: build(:subscription, last_bill_date: last_bill_date))
+ site = insert(:site, members: [user])
+
+ create_pageviews([
+ %{domain: site.domain, timestamp: ~N[2021-01-01 00:00:00]},
+ %{domain: site.domain, timestamp: ~N[2020-12-31 00:00:00]},
+ %{domain: site.domain, timestamp: ~N[2020-11-01 00:00:00]},
+ %{domain: site.domain, timestamp: ~N[2020-10-31 00:00:00]}
+ ])
+
+ assert Billing.last_two_billing_months_usage(user, today) == {1, 1}
+ end
+
+ test "gets event count from last month and this one" do
+ user =
+ insert(:user,
+ subscription:
+ build(:subscription, last_bill_date: Timex.today() |> Timex.shift(days: -1))
+ )
+
+ assert Billing.last_two_billing_months_usage(user) == {0, 0}
+ end
+ end
+
describe "trial_days_left" do
test "is 30 days for new signup" do
user = insert(:user)
diff --git a/test/workers/check_usage_test.exs b/test/workers/check_usage_test.exs
index 88ddb94c8f..42f69eaf40 100644
--- a/test/workers/check_usage_test.exs
+++ b/test/workers/check_usage_test.exs
@@ -31,7 +31,8 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {9_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {9_000, 11_000} end)
insert(:subscription,
user: user,
@@ -48,7 +49,11 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
insert(:subscription,
user: user,
@@ -69,7 +74,11 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
insert(:subscription,
user: user,
@@ -89,7 +98,11 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
insert(:subscription,
user: user,
@@ -106,7 +119,11 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
insert(:subscription,
user: user,
@@ -127,7 +144,11 @@ defmodule Plausible.Workers.CheckUsageTest do
user: user
} do
billing_stub =
- stub(Plausible.Billing, :last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ Plausible.Billing
+ |> stub(:last_two_billing_months_usage, fn _user -> {11_000, 11_000} end)
+ |> stub(:last_two_billing_cycles, fn _user ->
+ {Date.range(Timex.today(), Timex.today()), Date.range(Timex.today(), Timex.today())}
+ end)
insert(:subscription,
user: user,