Lock traffic notifications (#3641)
* Update communication * Remove an unreachable function (mistyped) * [migration] Make accept_traffic_until a date * Fix typo * Set `accept_traffic_until` when creating a site * Update sites `accept_traffic_until` on subscription change * Add a note to yearly cancellation notification * Rephrase annual e-mail for clarity * Pass the small build test * Add email notifications * Fixup * Implement `accept_traffic_until` notification worker * Fixup - no need to test this for small build * Update moduledoc * Move moduletag * s/sent_at/sent_on * Use WHERE NOT EXISTS instead of LEFT JOIN * Use upsert when tracking notifications sent * Store sent marker before actually sending notification * Prefer to keep `accept_traffic_until` on the user record This gives us a single source of truth, addresses cases like ownership transparently, simplifies the code and enables CRM toggles. The only downside is that there's another join performed in the Sites.Cache full refresh - in this case, small refreshes are skipped - but this is fine, since the traffic will be let in anyway. * Expose `accepted_traffic_until` in the CRM * Update lib/plausible/auth/user.ex Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com> * Preload owner in CRM * Use the offset parameter in trial over e-mail contents * Format * Harden cache test --------- Co-authored-by: Adrian Gruntkowski <adrian.gruntkowski@gmail.com>pull/3655/head r2023.12.28-296
parent
66d4b3a966
commit
f755b20569
@ -0,0 +1,8 @@
|
||||
You used to have an active account with Plausible Analytics, a simple, lightweight, open source and privacy-first Google Analytics alternative.
|
||||
|
||||
We've noticed that you're still sending us stats so we're writing to inform you that we'll stop accepting stats from your sites <%= @time %>. We're an independent, bootstrapped service and we don't sell your data, so this will reduce our server costs and help keep us sustainable.
|
||||
|
||||
If you'd like to continue counting your site stats in a privacy-friendly way, please
|
||||
<a href={plausible_url()}>login to your Plausible account</a> and start a subscription.
|
||||
|
||||
Do you have any questions or need help with anything? Just reply to this email and we'll gladly help.
|
||||
@ -1,7 +1,9 @@
|
||||
Time flies! This is a reminder that your annual subscription for Plausible Analytics will expire on <%= @date %>.
|
||||
Time flies! This is a reminder that your annual subscription for Plausible Analytics will expire on <%= @next_bill_date %>.
|
||||
<br /><br />
|
||||
You need to <%= link("renew your subscription", to: PlausibleWeb.Router.Helpers.billing_url(PlausibleWeb.Endpoint, :choose_plan)) %> if you want to continue using Plausible to count your website stats in a privacy-friendly way.
|
||||
<br /><br />
|
||||
If you don't want to continue your subscription, there's no action required. You will lose access to the stats on <%= @date %>.
|
||||
If you don't want to continue your subscription, there's no action required. You will lose access to your dashboard on <%= @next_bill_date %>.
|
||||
<br /><br />
|
||||
Have a question, feedback or need some guidance? Just reply to this email to get in touch!
|
||||
|
||||
Note that we'll stop accepting stats on <%= @accept_traffic_until %>. We're an independent, bootstrapped service and we don't sell your data, so this will reduce our server costs and help keep us sustainable. If you'd like to continue counting your site stats in a privacy-friendly way, please login to your Plausible account and renew your subscription.
|
||||
|
||||
@ -0,0 +1,95 @@
|
||||
defmodule Plausible.Workers.AcceptTrafficUntil do
|
||||
@moduledoc """
|
||||
A worker meant to be run once a day that sends out e-mail notifications to site
|
||||
owners assuming:
|
||||
- their sites still receive traffic (i.e. have stats for yesterday)
|
||||
- `site.accept_traffic_until` is approaching either tomorrow or exactly in 7 days
|
||||
|
||||
Users having no sites or sites that receive no traffic, won't be notified.
|
||||
We make a tiny effort here to make sure we send the same notification at most once a day.
|
||||
"""
|
||||
use Oban.Worker, queue: :check_accept_traffic_until
|
||||
import Ecto.Query
|
||||
|
||||
alias Plausible.Auth.User
|
||||
alias Plausible.Site
|
||||
alias Plausible.Repo
|
||||
alias Plausible.ClickhouseRepo
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(_job, today \\ Date.utc_today()) do
|
||||
tomorrow = today |> Date.add(+1)
|
||||
next_week = today |> Date.add(+7)
|
||||
|
||||
# send at most one notification per user, per day
|
||||
sent_today_query =
|
||||
from s in "sent_accept_traffic_until_notifications",
|
||||
where: s.user_id == parent_as(:user).id and s.sent_on == ^today,
|
||||
select: true
|
||||
|
||||
notifications =
|
||||
Repo.all(
|
||||
from u in User,
|
||||
as: :user,
|
||||
join: sm in Site.Membership,
|
||||
on: sm.user_id == u.id,
|
||||
where: sm.role == :owner,
|
||||
where: u.accept_traffic_until == ^tomorrow or u.accept_traffic_until == ^next_week,
|
||||
where: not exists(sent_today_query),
|
||||
select: %{
|
||||
id: u.id,
|
||||
email: u.email,
|
||||
deadline: u.accept_traffic_until,
|
||||
site_ids: fragment("array_agg(?.site_id)", sm)
|
||||
},
|
||||
group_by: u.id
|
||||
)
|
||||
|
||||
for notification <- notifications do
|
||||
case {has_stats?(notification.site_ids, today), notification.deadline} do
|
||||
{true, ^tomorrow} ->
|
||||
notification
|
||||
|> store_sent(today)
|
||||
|> PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow()
|
||||
|> Plausible.Mailer.send()
|
||||
|
||||
{true, ^next_week} ->
|
||||
notification
|
||||
|> store_sent(today)
|
||||
|> PlausibleWeb.Email.approaching_accept_traffic_until()
|
||||
|> Plausible.Mailer.send()
|
||||
|
||||
_ ->
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
{:ok, Enum.count(notifications)}
|
||||
end
|
||||
|
||||
defp has_stats?(site_ids, today) do
|
||||
yesterday = Date.add(today, -1)
|
||||
|
||||
ClickhouseRepo.exists?(
|
||||
from e in "events_v2",
|
||||
where: fragment("toDate(?) >= ?", e.timestamp, ^yesterday),
|
||||
where: e.site_id in ^site_ids
|
||||
)
|
||||
end
|
||||
|
||||
defp store_sent(notification, today) do
|
||||
Repo.insert_all(
|
||||
"sent_accept_traffic_until_notifications",
|
||||
[
|
||||
%{
|
||||
user_id: notification.id,
|
||||
sent_on: today
|
||||
}
|
||||
],
|
||||
on_conflict: :nothing,
|
||||
conflict_target: [:user_id, :sent_on]
|
||||
)
|
||||
|
||||
notification
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,56 @@
|
||||
defmodule Plausible.UsersTest do
|
||||
use Plausible.DataCase, async: true
|
||||
|
||||
alias Plausible.Users
|
||||
alias Plausible.Auth.User
|
||||
alias Plausible.Repo
|
||||
|
||||
describe "update_accept_traffic_until" do
|
||||
@describetag :full_build_only
|
||||
test "update" do
|
||||
user = insert(:user) |> User.start_trial() |> Repo.update!()
|
||||
# 30 for trial + 14
|
||||
assert Date.diff(user.accept_traffic_until, Date.utc_today()) ==
|
||||
30 + User.trial_accept_traffic_until_offset_days()
|
||||
|
||||
future = Date.add(Date.utc_today(), 30)
|
||||
insert(:subscription, user: user, next_bill_date: future)
|
||||
|
||||
assert updated_user = Users.update_accept_traffic_until(user)
|
||||
|
||||
assert Date.diff(updated_user.accept_traffic_until, future) ==
|
||||
User.subscription_accept_traffic_until_offset_days()
|
||||
end
|
||||
|
||||
test "retrieve: trial + 14 days" do
|
||||
user = insert(:user)
|
||||
|
||||
assert Users.accept_traffic_until(user) ==
|
||||
Date.utc_today() |> Date.add(30 + User.trial_accept_traffic_until_offset_days())
|
||||
end
|
||||
|
||||
test "retrieve: last_bill_date + 30 days" do
|
||||
future = Date.add(Date.utc_today(), 30)
|
||||
user = insert(:user, subscription: build(:subscription, next_bill_date: future))
|
||||
|
||||
assert Users.accept_traffic_until(user) ==
|
||||
future |> Date.add(User.subscription_accept_traffic_until_offset_days())
|
||||
end
|
||||
|
||||
test "retrieve: free plan" do
|
||||
user = insert(:user, subscription: build(:subscription, paddle_plan_id: "free_10k"))
|
||||
|
||||
assert Users.accept_traffic_until(user) == ~D[2135-01-01]
|
||||
end
|
||||
|
||||
test "retrieve: invalid user (there's one in the prod DB as of today, due to some testing)" do
|
||||
user = insert(:user, trial_expiry_date: nil)
|
||||
|
||||
assert_raise RuntimeError,
|
||||
~r/Manual intervention required/,
|
||||
fn ->
|
||||
assert Users.accept_traffic_until(user) == ~D[2135-01-01]
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -0,0 +1,126 @@
|
||||
defmodule Plausible.Workers.AcceptTrafficUntilTest do
|
||||
use Plausible.DataCase, async: true
|
||||
use Bamboo.Test
|
||||
|
||||
alias Plausible.Workers.AcceptTrafficUntil
|
||||
|
||||
@moduletag :full_build_only
|
||||
|
||||
test "does not send any notifications when sites have no stats" do
|
||||
today = Date.utc_today()
|
||||
next_week = today |> Date.add(+7)
|
||||
tomorrow = today |> Date.add(+1)
|
||||
|
||||
user1 = insert(:user, accept_traffic_until: next_week)
|
||||
user2 = insert(:user, accept_traffic_until: tomorrow)
|
||||
|
||||
_site1 = insert(:site, members: [user1])
|
||||
|
||||
_site2 = insert(:site, members: [user2])
|
||||
|
||||
{:ok, 2} = AcceptTrafficUntil.perform(nil)
|
||||
|
||||
refute_notifications(user1.email)
|
||||
refute_notifications(user2.email)
|
||||
end
|
||||
|
||||
test "tomorrow: sends one e-mail" do
|
||||
tomorrow = Date.utc_today() |> Date.add(+1)
|
||||
user = insert(:user, accept_traffic_until: tomorrow)
|
||||
|
||||
:site |> insert(members: [user]) |> populate_stats([build(:pageview)])
|
||||
|
||||
{:ok, 1} = AcceptTrafficUntil.perform(nil)
|
||||
assert_final_notification(user.email)
|
||||
end
|
||||
|
||||
test "next week: sends one e-mail" do
|
||||
next_week = Date.utc_today() |> Date.add(+7)
|
||||
user = insert(:user, accept_traffic_until: next_week)
|
||||
|
||||
:site |> insert(members: [user]) |> populate_stats([build(:pageview)])
|
||||
|
||||
{:ok, 1} = AcceptTrafficUntil.perform(nil)
|
||||
assert_weekly_notification(user.email)
|
||||
end
|
||||
|
||||
test "sends combined warnings in one shot" do
|
||||
today = Date.utc_today()
|
||||
next_week = today |> Date.add(+7)
|
||||
tomorrow = today |> Date.add(+1)
|
||||
in_8_days = today |> Date.add(+8)
|
||||
|
||||
user1 = insert(:user, accept_traffic_until: next_week)
|
||||
user2 = insert(:user, accept_traffic_until: tomorrow)
|
||||
user3 = insert(:user, accept_traffic_until: in_8_days, email: "nope@example.com")
|
||||
user4 = insert(:user, accept_traffic_until: today, email: "nope2@example.com")
|
||||
|
||||
:site |> insert(members: [user1]) |> populate_stats([build(:pageview)])
|
||||
:site |> insert(members: [user2]) |> populate_stats([build(:pageview)])
|
||||
:site |> insert(members: [user3]) |> populate_stats([build(:pageview)])
|
||||
:site |> insert(members: [user4]) |> populate_stats([build(:pageview)])
|
||||
|
||||
{:ok, 2} = AcceptTrafficUntil.perform(nil)
|
||||
assert_weekly_notification(user1.email)
|
||||
assert_final_notification(user2.email)
|
||||
refute_notifications("nope@example.com")
|
||||
refute_notifications("nope2@example.com")
|
||||
end
|
||||
|
||||
test "sends multiple notifications per recipient as the time passes" do
|
||||
today = Date.utc_today()
|
||||
email = "cycle@example.com"
|
||||
user = insert(:user, email: email) |> Plausible.Auth.User.start_trial() |> Repo.update!()
|
||||
site = insert(:site, members: [user])
|
||||
|
||||
populate_stats(site, [
|
||||
build(:pageview, timestamp: today),
|
||||
build(:pageview, timestamp: Date.add(user.trial_expiry_date, 7)),
|
||||
build(:pageview, timestamp: Date.add(user.trial_expiry_date, 8)),
|
||||
build(:pageview, timestamp: Date.add(user.trial_expiry_date, 13))
|
||||
])
|
||||
|
||||
# today's worker is no-op
|
||||
{:ok, 0} = AcceptTrafficUntil.perform(nil, today)
|
||||
refute_notifications(email)
|
||||
|
||||
# trial_expiry + 7 days worker => we'll stop counting next week
|
||||
{:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 7))
|
||||
assert_weekly_notification(email)
|
||||
|
||||
# another call on the same day == no-op, e-mail already sent
|
||||
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 7))
|
||||
refute_notifications(email)
|
||||
|
||||
# another call on another day == no-op, no notification type applies
|
||||
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 8))
|
||||
refute_notifications(email)
|
||||
|
||||
# day before accept_stats_until (trial_expiry_date + 14 - 1)
|
||||
{:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 13))
|
||||
assert_final_notification(email)
|
||||
|
||||
# another one on the day before accept_stats_until (trial_expiry_date + 14 - 1) == no-op
|
||||
{:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 13))
|
||||
refute_notifications(email)
|
||||
end
|
||||
|
||||
defp assert_weekly_notification(email) when is_binary(email) do
|
||||
assert_email_delivered_with(
|
||||
to: [nil: email],
|
||||
subject: PlausibleWeb.Email.approaching_accept_traffic_until(%{email: email}).subject
|
||||
)
|
||||
end
|
||||
|
||||
defp assert_final_notification(email) when is_binary(email) do
|
||||
assert_email_delivered_with(
|
||||
to: [nil: email],
|
||||
subject:
|
||||
PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{email: email}).subject
|
||||
)
|
||||
end
|
||||
|
||||
defp refute_notifications(email) when is_binary(email) do
|
||||
refute_email_delivered_with(to: [nil: email])
|
||||
end
|
||||
end
|
||||
Loading…
Reference in New Issue