Skip to content

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

Send confirmation email when user cancels their subscription

Uku Taht9ad2075d6f08
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index d09662e61a..ee133aa080 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -30,6 +30,7 @@ defmodule Plausible.Billing do
def subscription_cancelled(params) do
subscription = Repo.get_by(Subscription, paddle_subscription_id: params["subscription_id"])
+ |> Repo.preload(:user)
if subscription do
changeset =
@@ -37,7 +38,13 @@ defmodule Plausible.Billing do
status: params["status"]
})
- Repo.update(changeset)
+ case Repo.update(changeset) do
+ {:ok, updated} ->
+ PlausibleWeb.Email.cancellation_email(subscription.user)
+ |> Plausible.Mailer.send_email()
+ {:ok, updated}
+ err -> err
+ end
else
{:ok, nil}
end
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 29959255a8..d6ac2a1c6d 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -6,14 +6,9 @@ defmodule PlausibleWeb.Email do
Application.get_env(:plausible, :mailer_email)
end
- def admin_email do
- Application.get_env(:plausible, :admin_email)
- end
-
def activation_email(user, link) do
base_email()
|> to(user.email)
- |> from(mailer_email_from())
|> tag("activation-email")
|> subject("Activate your Plausible free trial")
|> render("activation_email.html", name: user.name, link: link)
@@ -22,7 +17,6 @@ defmodule PlausibleWeb.Email do
def welcome_email(user) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("welcome-email")
|> subject("Welcome to Plausible")
|> render("welcome_email.html", user: user)
@@ -31,7 +25,6 @@ defmodule PlausibleWeb.Email do
def create_site_email(user) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("create-site-email")
|> subject("Your Plausible setup: Add your website details")
|> render("create_site_email.html", user: user)
@@ -40,7 +33,6 @@ defmodule PlausibleWeb.Email do
def site_setup_help(user, site) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("help-email")
|> subject("Your Plausible setup: Waiting for the first page views")
|> render("site_setup_help_email.html", user: user, site: site)
@@ -49,7 +41,6 @@ defmodule PlausibleWeb.Email do
def site_setup_success(user, site) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("setup-success-email")
|> subject("Plausible is now tracking your website stats")
|> render("site_setup_success_email.html", user: user, site: site)
@@ -58,7 +49,6 @@ defmodule PlausibleWeb.Email do
def check_stats_email(user) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("check-stats-email")
|> subject("Check your Plausible website stats")
|> render("check_stats_email.html", user: user)
@@ -67,7 +57,6 @@ defmodule PlausibleWeb.Email do
def password_reset_email(email, reset_link) do
base_email()
|> to(email)
- |> from(mailer_email_from())
|> tag("password-reset-email")
|> subject("Plausible password reset")
|> render("password_reset_email.html", reset_link: reset_link)
@@ -76,7 +65,6 @@ defmodule PlausibleWeb.Email do
def trial_one_week_reminder(user) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("trial-one-week-reminder")
|> subject("Your Plausible trial expires next week")
|> render("trial_one_week_reminder.html", user: user)
@@ -85,7 +73,6 @@ defmodule PlausibleWeb.Email do
def trial_upgrade_email(user, day, pageviews) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("trial-upgrade-email")
|> subject("Your Plausible trial ends #{day}")
|> render("trial_upgrade_email.html", user: user, day: day, pageviews: pageviews)
@@ -94,7 +81,6 @@ defmodule PlausibleWeb.Email do
def trial_over_email(user) do
base_email()
|> to(user)
- |> from(mailer_email_from())
|> tag("trial-over-email")
|> subject("Your Plausible trial has ended")
|> render("trial_over_email.html", user: user)
@@ -103,14 +89,24 @@ defmodule PlausibleWeb.Email do
def weekly_report(email, site, assigns) do
base_email()
|> to(email)
- |> from(mailer_email_from())
|> tag("weekly-report")
|> subject("#{assigns[:name]} report for #{site.domain}")
|> render("weekly_report.html", Keyword.put(assigns, :site, site))
end
+ def cancellation_email(user) do
+ base_email()
+ |> to(user.email)
+ |> tag("cancelled-email")
+ |> subject("Your Plausible Analytics subscription has been canceled")
+ |> render("cancellation_email.html", name: user.name)
+ end
+
defp base_email() do
+ mailer_from = Application.get_env(:plausible, :mailer_email)
+
new_email()
|> put_param("TrackOpens", false)
+ |> from(mailer_from)
end
end
diff --git a/lib/plausible_web/templates/email/cancellation_email.html.eex b/lib/plausible_web/templates/email/cancellation_email.html.eex
new file mode 100644
index 0000000000..119e46001f
--- /dev/null
+++ b/lib/plausible_web/templates/email/cancellation_email.html.eex
@@ -0,0 +1,10 @@
+Hi <%= @name %>,
+<br /><br />
+Thanks for being a Plausible Analytics subscriber and I'm sorry to see you go.<br/><br/>
+
+I'd love to hear about your experience and how you think we can improve Plausible Analytics for our other subscribers (and for you if you decide to come back). Please reply back to this email with your feedback.<br /><br />
+
+If you decide you'd like to continue keeping track of your site traffic while respecting the privacy of your visitors, obviously we'd love to have you back. You can restart your subscription from your <a href="https://plausible.io/settings">account settings</a>.<br /><br />
+
+Hope to see you around!<br />
+Uku Taht
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index 9b91e3e495..ecf9f42e35 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -1,5 +1,6 @@
defmodule Plausible.BillingTest do
use Plausible.DataCase
+ use Bamboo.Test, shared: true
alias Plausible.Billing
describe "usage" do
@@ -201,6 +202,19 @@ defmodule Plausible.BillingTest do
assert res == {:ok, nil}
end
+
+ test "sends an email to confirm cancellation" do
+ user = insert(:user)
+ subscription = insert(:subscription, status: "active", user: user)
+
+ Billing.subscription_cancelled(%{
+ "alert_name" => "subscription_cancelled",
+ "subscription_id" => subscription.paddle_subscription_id,
+ "status" => "deleted"
+ })
+
+ assert_email_delivered_with(subject: "Your Plausible Analytics subscription has been canceled")
+ end
end
describe "subscription_payment_succeeded" do