Email flows (#46)

* Update activation email

* Update trial upgrade emails

* Remove feedback emails

* Remove feedback email test

* Update welcome email (and send it earlier)

* Site setup emails

* Add create site emails for trialers

* Add check stats email

* Remove dead code

* Adjust site setup emails for users who are not on trial

* BCC myself on all new emails

* Use old phrasing in the welcome email
pull/44/head
Uku Taht 7 years ago committed by GitHub
parent e3373724c7
commit ee92ed5213
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,46 @@
defmodule Mix.Tasks.SendCheckStatsEmails do
use Mix.Task
use Plausible.Repo
require Logger
@doc """
This is scheduled to run every 6 hours.
"""
def run(args) do
Application.ensure_all_started(:plausible)
execute(args)
end
def execute(args \\ []) do
q =
from(u in Plausible.Auth.User,
left_join: ce in "check_stats_emails", on: ce.user_id == u.id,
where: is_nil(ce.id),
where:
u.inserted_at > fragment("(now() at time zone 'utc') - '14 days'::interval") and
u.inserted_at < fragment("(now() at time zone 'utc') - '7 days'::interval") and
u.last_seen < fragment("(now() at time zone 'utc') - '7 days'::interval")
)
for user <- Repo.all(q) do
if Plausible.Auth.user_completed_setup?(user) do
send_check_stats_email(args, user)
end
end
end
defp send_check_stats_email(["--dry-run"], user) do
Logger.info("DRY RUN: check stats email to #{user.name}")
end
defp send_check_stats_email(_, user) do
PlausibleWeb.Email.check_stats_email(user)
|> Plausible.Mailer.deliver_now()
Repo.insert_all("check_stats_emails", [%{
user_id: user.id,
timestamp: NaiveDateTime.utc_now()
}])
end
end

@ -1,48 +0,0 @@
defmodule Mix.Tasks.SendFeedbackEmails do
use Mix.Task
use Plausible.Repo
require Logger
@doc """
This is scheduled to run every 6 hours.
"""
def run(args) do
Application.ensure_all_started(:plausible)
execute(args)
end
def execute(args \\ []) do
q =
from(u in Plausible.Auth.User,
left_join: fe in "feedback_emails", on: fe.user_id == u.id,
where: is_nil(fe.id),
where: u.inserted_at < fragment("(now() at time zone 'utc') - '30 days'::interval"),
where: u.last_seen > fragment("now() at time zone 'utc' - '7 days'::interval")
)
for user <- Repo.all(q) do
if Plausible.Auth.user_completed_setup?(user) do
send_feedback_email(args, user)
end
end
end
defp send_feedback_email(["--dry-run"], user) do
Logger.info("DRY RUN: feedback survey email to #{user.name}")
end
defp send_feedback_email(_, user) do
PlausibleWeb.Email.feedback_survey_email(user)
|> Plausible.Mailer.deliver_now()
feedback_email_sent(user)
end
defp feedback_email_sent(user) do
Repo.insert_all("feedback_emails", [%{
user_id: user.id,
timestamp: NaiveDateTime.utc_now()
}])
end
end

@ -1,64 +0,0 @@
defmodule Mix.Tasks.SendIntroEmails do
use Mix.Task
use Plausible.Repo
require Logger
@doc """
This is scheduled to run every 6 hours.
"""
def run(args) do
Application.ensure_all_started(:plausible)
execute(args)
end
def execute(args \\ []) do
q =
from(u in Plausible.Auth.User,
left_join: ie in "intro_emails", on: ie.user_id == u.id,
where: is_nil(ie.id),
where:
u.inserted_at > fragment("(now() at time zone 'utc') - '24 hours'::interval") and
u.inserted_at < fragment("(now() at time zone 'utc') - '6 hours'::interval")
)
for user <- Repo.all(q) do
if Plausible.Auth.user_completed_setup?(user) do
Logger.info("#{user.name} has completed the setup. Sending welcome email.")
send_welcome_email(args, user)
else
Logger.info("#{user.name} has not completed the setup. Sending help email.")
send_help_email(args, user)
end
end
end
defp send_welcome_email(["--dry-run"], user) do
Logger.info("DRY RUN: welcome email to #{user.name}")
end
defp send_welcome_email(_, user) do
PlausibleWeb.Email.welcome_email(user)
|> Plausible.Mailer.deliver_now()
intro_email_sent(user)
end
defp send_help_email(["--dry-run"], user) do
Logger.info("DRY RUN: help email to #{user.name}")
end
defp send_help_email(_, user) do
PlausibleWeb.Email.help_email(user)
|> Plausible.Mailer.deliver_now()
intro_email_sent(user)
end
defp intro_email_sent(user) do
Repo.insert_all("intro_emails", [%{
user_id: user.id,
timestamp: NaiveDateTime.utc_now()
}])
end
end

@ -0,0 +1,117 @@
defmodule Mix.Tasks.SendSiteSetupEmails do
use Mix.Task
use Plausible.Repo
require Logger
@doc """
This is scheduled to run every 6 hours.
"""
def run(args) do
Application.ensure_all_started(:plausible)
execute(args)
end
def execute(args \\ []) do
send_create_site_emails(args)
send_setup_help_emails(args)
send_setup_success_emails(args)
end
defp send_create_site_emails(args) do
q =
from(s in Plausible.Auth.User,
left_join: se in "create_site_emails", on: se.user_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval") and s.inserted_at < fragment("(now() at time zone 'utc') - '48 hours'::interval"),
preload: :sites
)
for user <- Repo.all(q) do
if Enum.count(user.sites) == 0 do
send_create_site_email(args, user)
end
end
end
defp send_setup_help_emails(args) do
q =
from(s in Plausible.Site,
left_join: se in "setup_help_emails", on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: :members
)
for site <- Repo.all(q) do
owner = List.first(site.members)
setup_completed = Plausible.Sites.has_pageviews?(site)
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
if !setup_completed && hours_passed > 47 do
send_setup_help_email(args, owner, site)
end
end
end
defp send_setup_success_emails(args) do
q =
from(s in Plausible.Site,
left_join: se in "setup_success_emails", on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: :members
)
for site <- Repo.all(q) do
owner = List.first(site.members)
if Plausible.Sites.has_pageviews?(site) do
send_setup_success_email(args, owner, site)
end
end
end
defp send_create_site_email(["--dry-run"], user) do
Logger.info("DRY RUN: create site email for #{user.name}")
end
defp send_create_site_email(_, user) do
PlausibleWeb.Email.create_site_email(user)
|> Plausible.Mailer.deliver_now()
Repo.insert_all("create_site_emails", [%{
user_id: user.id,
timestamp: NaiveDateTime.utc_now()
}])
end
defp send_setup_success_email(["--dry-run"], _, site) do
Logger.info("DRY RUN: setup success email for #{site.domain}")
end
defp send_setup_success_email(_, user, site) do
PlausibleWeb.Email.site_setup_success(user, site)
|> Plausible.Mailer.deliver_now()
Repo.insert_all("setup_success_emails", [%{
site_id: site.id,
timestamp: NaiveDateTime.utc_now()
}])
end
defp send_setup_help_email(["--dry-run"], _, site) do
Logger.info("DRY RUN: setup help email for #{site.domain}")
end
defp send_setup_help_email(_, user, site) do
PlausibleWeb.Email.site_setup_help(user, site)
|> Plausible.Mailer.deliver_now()
Repo.insert_all("setup_help_emails", [%{
site_id: site.id,
timestamp: NaiveDateTime.utc_now()
}])
end
end

@ -24,9 +24,9 @@ defmodule Mix.Tasks.SendTrialNotifications do
for user <- users do
case Timex.diff(user.trial_expiry_date, Timex.today(), :days) do
14 ->
7 ->
if Plausible.Auth.user_completed_setup?(user) do
send_two_week_reminder(args, user)
send_one_week_reminder(args, user)
end
1 ->
if Plausible.Auth.user_completed_setup?(user) do
@ -46,12 +46,12 @@ defmodule Mix.Tasks.SendTrialNotifications do
end
end
defp send_two_week_reminder(["--dry-run"], user) do
Logger.info("DRY RUN: 2-week trial notification email to #{user.name} [inserted=#{user.inserted_at}]")
defp send_one_week_reminder(["--dry-run"], user) do
Logger.info("DRY RUN: one week trial notification email to #{user.name} [inserted=#{user.inserted_at}]")
end
defp send_two_week_reminder(_, user) do
PlausibleWeb.Email.trial_two_week_reminder(user)
defp send_one_week_reminder(_, user) do
PlausibleWeb.Email.trial_one_week_reminder(user)
|> Plausible.Mailer.deliver_now()
end

@ -83,6 +83,8 @@ defmodule Plausible.Billing do
end
end
def on_trial?(user), do: trial_days_left(user) >= 0
def trial_days_left(user) do
Timex.diff(user.trial_expiry_date, Timex.today(), :days)
end

@ -33,6 +33,9 @@ defmodule PlausibleWeb.AuthController do
{:ok, %{name: name, email: email}} ->
case Auth.create_user(name, email) do
{:ok, user} ->
PlausibleWeb.Email.welcome_email(user)
|> Plausible.Mailer.deliver_now()
conn
|> put_session(:current_user_id, user.id)
|> redirect(to: "/password")

@ -2,22 +2,63 @@ defmodule PlausibleWeb.Email do
use Bamboo.Phoenix, view: PlausibleWeb.EmailView
import Bamboo.PostmarkHelper
def activation_email(user, link) do
new_email()
|> to(user.email)
|> from("Uku Taht <uku@plausible.io>")
|> tag("activation-email")
|> subject("Activate your Plausible free trial")
|> render("activation_email.html", name: user.name, link: link)
end
def welcome_email(user) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("welcome-email")
|> subject("Welcome to Plausible :) Plus, a quick question...")
|> subject("Welcome to Plausible")
|> render("welcome_email.html", user: user)
end
def help_email(user) do
def create_site_email(user) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("create-site-email")
|> subject("Your Plausible setup: Add your website details")
|> render("create_site_email.html", user: user)
end
def site_setup_help(user, site) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("help-email")
|> subject("Your Plausible setup")
|> render("help_email.html", user: user)
|> subject("Your Plausible setup: Waiting for the first page views")
|> render("site_setup_help_email.html", user: user, site: site)
end
def site_setup_success(user, site) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("setup-success-email")
|> subject("Plausible is now tracking your website stats")
|> render("site_setup_success_email.html", user: user, site: site)
end
def check_stats_email(user) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("check-stats-email")
|> subject("Check your Plausible website stats")
|> render("check_stats_email.html", user: user)
end
def password_reset_email(email, reset_link) do
@ -29,27 +70,20 @@ defmodule PlausibleWeb.Email do
|> render("password_reset_email.html", reset_link: reset_link)
end
def activation_email(user, link) do
new_email()
|> to(user.email)
|> from("Uku Taht <uku@plausible.io>")
|> tag("activation-email")
|> subject("Plausible activation link")
|> render("activation_email.html", name: user.name, link: link)
end
def trial_two_week_reminder(user) do
def trial_one_week_reminder(user) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("trial-two-week-reminder")
|> tag("trial-one-week-reminder")
|> subject("14 days left on your Plausible trial")
|> render("trial_two_week_reminder.html", user: user)
|> render("trial_one_week_reminder.html", user: user)
end
def trial_upgrade_email(user, day, pageviews) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("trial-upgrade-email")
|> subject("Your Plausible trial ends #{day}")
@ -59,21 +93,13 @@ defmodule PlausibleWeb.Email do
def trial_over_email(user) do
new_email()
|> to(user)
|> bcc("uku@plausible.io")
|> from("Uku Taht <uku@plausible.io>")
|> tag("trial-over-email")
|> subject("Your Plausible trial has ended")
|> render("trial_over_email.html", user: user)
end
def feedback_survey_email(user) do
new_email()
|> to(user)
|> from("Uku Taht <uku@plausible.io>")
|> tag("feedback-survey-email")
|> subject("Plausible feedback")
|> render("feedback_survey.html", user: user)
end
def feedback(from, text) do
from = if from == "", do: "anonymous@plausible.io", else: from

@ -1,5 +1,5 @@
Hi <%= @name %>,
<br /><br />
Thank you for signing up to Plausible. <a href="<%= @link %>">Click here</a> to activate your account.
Thank you for signing up for the free trial of Plausible, a simple and privacy-friendly alternative to Google Analytics.
<br /><br />
This link will expire in 24 hours. If you don't use it by then, you can request another activation link.
<a href="<%= @link %>">Click here</a> to activate your trial account. This link will expire in 24 hours.

@ -0,0 +1,14 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Plausible is tracking your website stats without compromising the user experience and the privacy of your visitors.
<br /><br />
<%= link("View your Plausible dashboard now", to: "https://plausible.io/") %> for the most valuable traffic insights at a glance.
<br /><br />
Do reply back to this email if you have any questions or need some guidance.
<br /></br>
Thanks,<br />
Uku Taht
<br /><br />
--
<br /><br />
https://plausible.io

@ -0,0 +1,14 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
You've activated your free 30-day trial of Plausible, a simple and privacy-friendly website analytics tool.
<br /><br />
<%= link("Click here", to: "https://plausible.io/sites/new") %> to add your website URL, your timezone and install our one-line JavaScript snippet to start collecting visitor statistics.
<br /><br />
Do reply back to this email if you have any questions or need some guidance.
<br /></br>
Thanks,<br />
Uku Taht
<br /><br />
--
<br /><br />
https://plausible.io

@ -1,17 +0,0 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Big thanks for giving Plausible a try at such an early stage.
<br /><br />
Now that you've had some time to use the product, I'd love to know how it's working for you. I've created a Typeform with 4 quick questions about your experience; it would help a lot if you could fill it out.
<br /><br />
<%= link("Take the survey", to: "https://plausible.typeform.com/to/szHj56") %>
<br /><br />
I will use your response to help prioritise the roadmap for the next months. You can request features and let me know about any annoyances/bugs you've encountered. Be completely honest, I can take it :)
<br /><br />
Thanks,<br />
Uku Taht<br />
Founder, Plausible Insights
<br /><br />
--
<br /><br />
https://plausible.io

@ -1,13 +0,0 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
I saw that you signed up for Plausible but the setup isn't working completely. Is there anything I can do to help?
<br /><br />
If you have any questions let me know.
<br /><br />
Thanks,<br />
Uku Taht<br />
Founder, Plausible Insights
<br /><br />
--
<br /><br />
https://plausible.io

@ -0,0 +1,18 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
<%= if Plausible.Billing.on_trial?(@user) do %>
You signed up for a free 30-day trial of Plausible, a simple and privacy-friendly website analytics tool.
<br /><br />
<% end %>
To finish your setup for <%= @site.domain %>, you need to install <%= link("this lightweight line of JavaScript code", to: "https://plausible.io/#{URI.encode_www_form(@site.domain)}/snippet") %> into your site to start collecting visitor statistics.
<br /><br />
This Plausible script is 14 times smaller than Google Analytics script so you’ll have a fast loading site while getting all the important traffic insights on one single page.
<br /><br />
Do reply back to this email if you have any questions or need some guidance.
<br /></br>
Thanks,<br />
Uku Taht
<br /><br />
--
<br /><br />
https://plausible.io

@ -0,0 +1,20 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Congrats! The Plausible script has been installed correctly on <%= @site.domain %>. Your website traffic is now being tracked without compromising the user experience and the privacy of your visitors.
<br /><br />
<%= link("Check your stats", to: "https://plausible.io/#{URI.encode_www_form(@site.domain)}") %>
<br /><br />
<%= if Plausible.Billing.on_trial?(@user) do %>
You're on a 30-day unlimited-use free trial with no obligations so do take your time to explore your simple and privacy-friendly website analytics dashboard.
<br /><br />
<% end %>
PS: Plausible is fully open-source and our public roadmap is defined by the community. <%= link("Leave your feedback", to: "https://feedback.plausible.io") %> and have your say on metrics and features we should be adding next.
<br /><br />
Do reply back to this email if you have any questions.
<br /></br>
Thanks,<br />
Uku Taht
<br /><br />
--
<br /><br />
https://plausible.io

@ -0,0 +1,13 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Time flies! Your 30-day free trial of Plausible will end next week.
<br /><br />
Over the last three weeks, I hope you got to experience the potential benefits of having website stats in a simple dashboard while respecting the privacy of your visitors, not annoying them with the cookie and privacy notices and still having a fast loading site.
<br /><br />
In order to continue receiving valuable website traffic insights at a glance, you’ll need to <%= link("Upgrade your account", to: "https://plausible.io/billing/upgrade") %>.
<br /><br />
If you have any questions or feedback for me, feel free to reply to this email.
<br /><br />
Thanks,<br />
Uku Taht<br />
https://plausible.io

@ -1,10 +1,10 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Your free 30-day trial has come to an end. Upgrade to a paid plan now to access to your stats again.<br /><br />
Your free Plausible trial has now expired. Upgrade your account to continue receiving valuable website traffic insights at a glance while respecting the privacy of your visitors and still having a fast loading site. <br /><br />
<%= link("Upgrade now", to: "https://plausible.io/billing/upgrade") %>
<%= link("Upgrade now", to: "https://plausible.io/billing/upgrade") %> <br />
We will keep recording stats for another month to give you time to upgrade. If you still haven't upgraded by then, we will stop recording.
We will keep recording stats for another month to give you time to upgrade.
<br /><br />
Thanks,<br />

@ -1,13 +0,0 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Thank you so much for trying out Plausible!
<br /><br />
This is your friendly reminder that you have <b>14 days left</b> on your Plausible free trial.
<br /><br />
I will contact you towards the end of the trial with instructions to upgrade from your free trial to a paid plan.
<br /><br />
If you have any questions or feedback for me, feel free to reply to this email.
<br /><br />
Thanks,<br />
Uku Taht<br />
https://plausible.io

@ -1,13 +1,13 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
Your free 30-day trial is ending <%= @day %>, but you can keep using Plausible by upgrading to a paid plan.
Thanks for exploring Plausible, a simple and privacy-friendly alternative to Google Analytics. Your free 30-day trial is ending <%= @day %>, but you can keep using Plausible by upgrading to a paid plan.
<br /><br />
In the last month, your account has used <%= PlausibleWeb.AuthView.delimit_integer(@pageviews) %> pageviews.
Based on that we recommend you select the <%= suggested_plan_name(@pageviews) %> plan which runs at <%= suggested_plan_cost(@pageviews) %>.
<br /><br />
<%= link("Upgrade now", to: "https://plausible.io/billing/upgrade") %>
<br /><br />
Have any questions? Just reply to this email to get in touch!
Have a question, feedback or need some guidance? Just reply to this email to get in touch!
<br /><br />
<br /><br />
Thanks,<br />

@ -1,20 +1,14 @@
Hey <%= user_salutation(@user) %>,
<br /><br />
I'm building Plausible to provide a simple and ethical approach to tracking website visitors. I'm super excited to
have you on board!
I'm building Plausible to provide a simple and ethical approach to tracking website visitors.
I'm super excited to have you on board!
<br /><br />
The hardest part about building this product is getting the word out there. I'm a solo developer without
a marketing budget, trying to make this business work. With your help, I might be able to go full-time soon.
To start collecting stats, you need to <%= link("add a site on Plausible", to: "https://plausible.io/sites/new") %>.
<br /><br />
If you could share why you switched to Plausible with your friends, it would really help me out a lot.
Truly, a single tweet or a blog post could make a huge difference for a small project like this.
Would you be up for this?
<br /><br />
PS: Also check out our <a href="https://feedback.plausible.io/">collaborative roadmapping tool</a>. If you have any feature requests, you can raise them there.
Have a question, feedback or need some guidance? Do reply back to this email.
<br /><br />
Thanks,<br />
Uku Taht,<br />
Founder, Plausible Insights
Uku Taht
<br /><br />
--
<br /><br />

@ -0,0 +1,15 @@
defmodule Plausible.Repo.Migrations.AddSetupEmails do
use Ecto.Migration
def change do
create table(:setup_help_emails) do
add :site_id, references(:sites), null: false, on_delete: :delete_all
add :timestamp, :naive_datetime
end
create table(:setup_success_emails) do
add :site_id, references(:sites), null: false, on_delete: :delete_all
add :timestamp, :naive_datetime
end
end
end

@ -0,0 +1,10 @@
defmodule Plausible.Repo.Migrations.AddCreateSiteEmails do
use Ecto.Migration
def change do
create table(:create_site_emails) do
add :user_id, references(:users), null: false, on_delete: :delete_all
add :timestamp, :naive_datetime
end
end
end

@ -0,0 +1,10 @@
defmodule Plausible.Repo.Migrations.AddCheckStatsEmails do
use Ecto.Migration
def change do
create table(:check_stats_emails) do
add :user_id, references(:users), null: false, on_delete: :delete_all
add :timestamp, :naive_datetime
end
end
end

@ -0,0 +1,43 @@
defmodule Mix.Tasks.SendCheckStatsEmailsTest do
use Plausible.DataCase
use Bamboo.Test
test "does not send an email before a week has passed" do
user = insert(:user, inserted_at: days_ago(6), last_seen: days_ago(6))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendCheckStatsEmails.execute()
assert_no_emails_delivered()
end
test "does not send an email if the user has logged in recently" do
user = insert(:user, inserted_at: days_ago(9), last_seen: days_ago(6))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendCheckStatsEmails.execute()
assert_no_emails_delivered()
end
test "sends an email after a week of signup if the user hasn't logged in" do
user = insert(:user, inserted_at: days_ago(8), last_seen: days_ago(8))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendCheckStatsEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Check your Plausible website stats"
)
end
defp days_ago(days) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(days: -days)
end
end

@ -1,65 +0,0 @@
defmodule Mix.Tasks.SendFeedbackEmailsTest do
use Plausible.DataCase
use Bamboo.Test
alias Mix.Tasks.SendFeedbackEmails
describe "when user does not have an active site" do
test "does not send an email ever" do
insert(:user, inserted_at: days_ago(15))
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
end
describe "when user has an active site" do
test "sends an email if the user is more than 30 days old and logged on in the last week" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_email_delivered_with(subject: "Plausible feedback")
end
test "sends the email only once" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_email_delivered_with(subject: "Plausible feedback")
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
test "does not send if user has not logged in recently" do
user = insert(:user, inserted_at: days_ago(31), last_seen: days_ago(15))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
test "does not send if user is less than a month old" do
user = insert(:user, inserted_at: days_ago(15), last_seen: days_ago(1))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
SendFeedbackEmails.execute()
assert_no_emails_delivered()
end
end
defp days_ago(days) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(days: -days)
end
end

@ -1,122 +0,0 @@
defmodule Mix.Tasks.SendIntroEmailsTest do
use Plausible.DataCase
use Bamboo.Test
describe "when user has not managed to set up a site" do
test "does not send an email 5 hours after signup" do
_user = insert(:user, inserted_at: hours_ago(5))
Mix.Tasks.SendIntroEmails.execute()
assert_no_emails_delivered()
end
test "sends a setup help email 6 hours after signup" do
user = insert(:user, inserted_at: hours_ago(6))
Mix.Tasks.SendIntroEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup"
)
end
test "sends a setup help email 23 hours after signup" do
user = insert(:user, inserted_at: hours_ago(23))
Mix.Tasks.SendIntroEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup"
)
end
test "does not send an email 24 hours after signup" do
_user = insert(:user, inserted_at: hours_ago(24))
Mix.Tasks.SendIntroEmails.execute()
assert_no_emails_delivered()
end
end
describe "when user has managed to set up their first site" do
test "does not send an email 5 hours after signup" do
_user = insert(:user, inserted_at: hours_ago(5))
Mix.Tasks.SendIntroEmails.execute()
assert_no_emails_delivered()
end
test "sends a setup help email 6 hours after signup if the user has created a site but has not received a pageview yet" do
user = insert(:user, inserted_at: hours_ago(6))
insert(:site, members: [user])
Mix.Tasks.SendIntroEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup"
)
end
test "sends a welcome email 6 hours after signup if the user has created a site and has received a pageview" do
user = insert(:user, inserted_at: hours_ago(6))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendIntroEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Welcome to Plausible :) Plus, a quick question..."
)
end
test "sends a welcome email 23 hours after signup" do
user = insert(:user, inserted_at: hours_ago(23))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendIntroEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Welcome to Plausible :) Plus, a quick question..."
)
end
test "does not send a welcome email 24 hours after signup" do
user = insert(:user, inserted_at: hours_ago(24))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendIntroEmails.execute()
assert_no_emails_delivered()
end
end
test "does not send two intro emails to the same person" do
user = insert(:user, inserted_at: hours_ago(12))
Mix.Tasks.SendIntroEmails.execute()
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendIntroEmails.execute()
assert_delivered_email(PlausibleWeb.Email.help_email(user))
refute_delivered_email(PlausibleWeb.Email.welcome_email(user))
end
defp hours_ago(hours) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(hours: -hours)
end
end

@ -0,0 +1,98 @@
defmodule Mix.Tasks.SendSiteSetupEmailsTest do
use Plausible.DataCase
use Bamboo.Test
describe "when user has not managed to set up the site" do
test "does not send an email 47 hours after site creation" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(47))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_no_emails_delivered()
end
test "sends a setup help email 48 hours after site has been created" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(49))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Waiting for the first page views"
)
end
test "does not send an email more than 72 hours after signup" do
user = insert(:user)
insert(:site, members: [user], inserted_at: hours_ago(73))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_no_emails_delivered()
end
end
describe "when user has managed to set up their site" do
test "sends the setup completed email as soon as possible" do
user = insert(:user)
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendSiteSetupEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Plausible is now tracking your website stats"
)
end
test "sends the setup completed email after the help email has been sent" do
user = insert(:user)
site = insert(:site, members: [user], inserted_at: hours_ago(49))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Waiting for the first page views"
)
insert(:pageview, domain: site.domain)
Mix.Tasks.SendSiteSetupEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Plausible is now tracking your website stats"
)
end
end
describe "trial user who has not set up a website" do
test "does not send an email before 48h have passed" do
insert(:user, inserted_at: hours_ago(47))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_no_emails_delivered()
end
test "sends the create site email after 48h" do
user = insert(:user, inserted_at: hours_ago(49))
Mix.Tasks.SendSiteSetupEmails.execute()
assert_email_delivered_with(
to: [{user.name, user.email}],
subject: "Your Plausible setup: Add your website details"
)
end
end
defp hours_ago(hours) do
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
|> Timex.shift(hours: -hours)
end
end

@ -14,14 +14,14 @@ defmodule Mix.Tasks.SendTrialNotificationsTest do
end
describe "with site and pageviews" do
test "sends a reminder 14 days before trial ends (16 days after user signed up)" do
user = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 14))
test "sends a reminder 7 days before trial ends (16 days after user signed up)" do
user = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 7))
site = insert(:site, members: [user])
insert(:pageview, domain: site.domain)
Mix.Tasks.SendTrialNotifications.execute()
assert_delivered_email(PlausibleWeb.Email.trial_two_week_reminder(user))
assert_delivered_email(PlausibleWeb.Email.trial_one_week_reminder(user))
end
test "sends an upgrade email the day before the trial ends" do
@ -55,7 +55,7 @@ defmodule Mix.Tasks.SendTrialNotificationsTest do
end
test "does not send a notification if user has a subscription" do
user1 = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 14))
user1 = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 7))
site1 = insert(:site, members: [user1])
insert(:pageview, domain: site1.domain)
user2 = insert(:user, trial_expiry_date: Timex.now |> Timex.shift(days: 1))

@ -33,6 +33,20 @@ defmodule Plausible.BillingTest do
end
end
describe "on_trial?" do
test "is true with >= 0 trial days left" do
user = insert(:user)
assert Billing.on_trial?(user)
end
test "is false with < 0 trial days left" do
user = insert(:user, trial_expiry_date: Timex.shift(Timex.now(), days: -1))
refute Billing.on_trial?(user)
end
end
describe "needs_to_upgrade?" do
test "is false for a trial user" do
user = insert(:user)

@ -7,7 +7,7 @@ defmodule PlausibleWeb.AuthControllerTest do
test "shows the register form", %{conn: conn} do
conn = get(conn, "/register")
assert html_response(conn, 200) =~ "Enter your details to get started"
assert html_response(conn, 200) =~ "Enter your details"
end
test "registering sends an activation link", %{conn: conn} do
@ -16,7 +16,7 @@ defmodule PlausibleWeb.AuthControllerTest do
email: "user@example.com"
})
assert_email_delivered_with(subject: "Plausible activation link")
assert_email_delivered_with(subject: "Activate your Plausible free trial")
end
test "user sees success page after registering", %{conn: conn} do
@ -37,6 +37,13 @@ defmodule PlausibleWeb.AuthControllerTest do
assert Plausible.Auth.find_user_by(email: "user@example.com")
end
test "sends the welcome email", %{conn: conn} do
token = Plausible.Auth.Token.sign_activation("Jane Doe", "user@example.com")
get(conn, "/claim-activation?token=#{token}")
assert_email_delivered_with(subject: "Welcome to Plausible")
end
test "redirects new user to create a password", %{conn: conn} do
token = Plausible.Auth.Token.sign_activation("Jane Doe", "user@example.com")
conn = get(conn, "/claim-activation?token=#{token}")

Loading…
Cancel
Save