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
Send email notification when sites are locked at grace period end
Uku Tahtd547d0f9964e
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex 47f71c2784..e292cb06d7 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -107,13 +107,19 @@ defmodule Plausible.Billing doPaddleApi.update_subscription_preview(subscription.paddle_subscription_id, new_plan_id)end- def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: true+ def needs_to_upgrade?(%Plausible.Auth.User{trial_expiry_date: nil}), do: {true, :no_trial}def needs_to_upgrade?(user) do- if Timex.before?(user.trial_expiry_date, Timex.today()) do- !subscription_is_active?(user.subscription)- else- false+ trial_is_over = Timex.before?(user.trial_expiry_date, Timex.today())+ subscription_active = subscription_is_active?(user.subscription)++ grace_period_ended =+ user.grace_period_end && Timex.before?(user.grace_period_end, Timex.today())++ cond do+ trial_is_over && !subscription_active -> {true, :no_active_subscription}+ grace_period_ended -> {true, :grace_period_ended}+ true -> falseendenddiff --git a/lib/plausible/billing/site_locker.ex b/lib/plausible/billing/site_locker.exindex e7cbedb652..986a15f859 100644--- a/lib/plausible/billing/site_locker.ex+++ b/lib/plausible/billing/site_locker.ex@@ -2,10 +2,16 @@ defmodule Plausible.Billing.SiteLocker douse Plausible.Repodef check_sites_for(user) do- if Plausible.Billing.needs_to_upgrade?(user) do- set_lock_status_for(user, true)- else- set_lock_status_for(user, false)+ case Plausible.Billing.needs_to_upgrade?(user) do+ {true, :grace_period_ended} ->+ set_lock_status_for(user, true)+ send_grace_period_end_email(user)++ {true, _} ->+ set_lock_status_for(user, true)++ _ ->+ set_lock_status_for(user, false)endend@@ -26,4 +32,20 @@ defmodule Plausible.Billing.SiteLocker doRepo.update_all(site_q, set: [locked: status])end++ defp send_grace_period_end_email(user) do+ {_, last_cycle} = Plausible.Billing.last_two_billing_cycles(user)+ {_, last_cycle_usage} = Plausible.Billing.last_two_billing_months_usage(user)+ suggested_plan = Plausible.Billing.Plans.suggested_plan(user, last_cycle)++ template =+ PlausibleWeb.Email.dashboard_locked(+ user,+ last_cycle_usage,+ last_cycle,+ suggested_plan+ )++ Plausible.Mailer.send_email_safe(template)+ endenddiff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.exindex 698282ab5b..c132f7d52e 100644--- a/lib/plausible_web/email.ex+++ b/lib/plausible_web/email.ex@@ -142,6 +142,19 @@ defmodule PlausibleWeb.Email do})end+ def dashboard_locked(user, usage, last_cycle, suggested_plan) do+ base_email()+ |> to(user)+ |> tag("dashboard-locked")+ |> subject("[Action required] Your Plausible dashboard is now locked")+ |> render("dashboard_locked.html", %{+ user: user,+ usage: usage,+ last_cycle: last_cycle,+ suggested_plan: suggested_plan+ })+ end+def yearly_renewal_notification(user) dodate = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")diff --git a/lib/plausible_web/templates/email/dashboard_locked.html.eex b/lib/plausible_web/templates/email/dashboard_locked.html.eexnew file mode 100644index 0000000000..4030d7cf94--- /dev/null+++ b/lib/plausible_web/templates/email/dashboard_locked.html.eex@@ -0,0 +1,25 @@+Hey <%= user_salutation(@user) %>,+<br /><br />+Last week we sent a reminder that your traffic has exceeded your Plausible Analytics subscription tier two months in a row.+<br /><br />+Your dashboard is now locked. We're still counting your stats, but you no longer have access to the stats. As you have outgrown your subscription tier, we kindly ask you to upgrade your subscription to accommodate your new traffic levels.+<br /><br />+Click here to go to your site settings. In the last billing cycle (<%= date_format(@last_cycle.first) %> to <%= date_format(@last_cycle.last) %>), your account has used <%= PlausibleWeb.StatsView.large_number_format(@usage) %> billable pageviews.+<%= if @usage <= 20_000_000 do %>+Based on that we recommend you select the <%= @suggested_plan[:volume] %>/mo plan.+<br /><br />+You can upgrade your subscription using our self-serve platform. The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire.+<br /><br />+<a href="https://plausible.io/settings">Click here</a> to go to your site settings. You can upgrade your subscription tier by clicking the 'change plan' link.+<% else %>+This is more than our standard plans, so please reply back to this email to get a quote for your volume.+<% end %>+<br /><br />+The new charge will be prorated to reflect the amount you have already paid and the time until your current subscription is supposed to expire. We will unlock your dashboard immediately after your account has been upgraded to the appropriate tier.+<br /><br />+Thanks for understanding and for being a Plausible subscriber!+<br /><br />+Thanks,<br />+Uku and Marko<br />+--<br />+<%= plausible_url() %><br />diff --git a/test/plausible/billing/site_locker_test.exs b/test/plausible/billing/site_locker_test.exsindex b61812f4bd..e42a2e0d4a 100644--- a/test/plausible/billing/site_locker_test.exs+++ b/test/plausible/billing/site_locker_test.exs@@ -49,6 +49,17 @@ defmodule Plausible.Billing.SiteLockerTest dorefute Repo.reload!(site).lockedend+ test "does not lock user who has an active subscription and is on grace period" do+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: 1))+ insert(:subscription, status: "active", user: user)+ user = Repo.preload(user, :subscription)+ site = insert(:site, members: [user])++ SiteLocker.check_sites_for(user)++ refute Repo.reload!(site).locked+ end+test "locks user who cancelled subscription and the cancelled subscription has expired" douser = insert(:user)@@ -67,6 +78,31 @@ defmodule Plausible.Billing.SiteLockerTest dorefute Repo.reload!(site).lockedend+ test "locks all sites if user has active subscription but grace period has ended" do+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))+ insert(:subscription, status: "active", user: user)+ user = Repo.preload(user, :subscription)+ site = insert(:site, members: [user])++ SiteLocker.check_sites_for(user)++ assert Repo.reload!(site).locked+ end++ test "sends email if grace period has ended" do+ user = insert(:user, grace_period_end: Timex.shift(Timex.today(), days: -1))+ insert(:subscription, status: "active", user: user)+ user = Repo.preload(user, :subscription)+ insert(:site, members: [user])++ SiteLocker.check_sites_for(user)++ assert_email_delivered_with(+ to: [user],+ subject: "[Action required] Your Plausible dashboard is now locked"+ )+ end+test "locks all sites if user has no trial or active subscription" douser =insert(:user, trial_expiry_date: Timex.today() |> Timex.shift(days: -1))diff --git a/test/support/factory.ex b/test/support/factory.exindex dc2249a55d..4ffbf7070b 100644--- a/test/support/factory.ex+++ b/test/support/factory.ex@@ -112,6 +112,7 @@ defmodule Plausible.Factory dostatus: "active",next_bill_amount: "6.00",next_bill_date: Timex.today(),+ last_bill_date: Timex.today(),currency_code: "USD"}end