Skip to content

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

Consolidated view email reports (#5876)

RobertJoonasf24aa4f305c9
diff --git a/lib/plausible/props.ex b/lib/plausible/props.ex
index 749d36e600..5130c95f53 100644
--- a/lib/plausible/props.ex
+++ b/lib/plausible/props.ex
@@ -171,7 +171,7 @@ defmodule Plausible.Props do
unnested_keys =
from e in Plausible.ClickhouseEventV2,
- where: ^site_id_filter(site),
+ where: ^Plausible.Sites.site_id_query_filter(site),
where: fragment("? > (NOW() - INTERVAL 6 MONTH)", e.timestamp),
select: %{key: fragment("arrayJoin(?)", field(e, :"meta.key"))}
@@ -186,21 +186,6 @@ defmodule Plausible.Props do
)
end
- on_ee do
- defp site_id_filter(%Plausible.Site{} = site) do
- if Plausible.Sites.consolidated?(site) do
- site_ids = Plausible.ConsolidatedView.Cache.get(site.domain)
- dynamic([e], fragment("? in ?", e.site_id, ^site_ids))
- else
- dynamic([e], e.site_id == ^site.id)
- end
- end
- else
- defp site_id_filter(%Plausible.Site{} = site) do
- dynamic([e], e.site_id == ^site.id)
- end
- end
-
defp valid?(key) do
String.length(key) in 1..@max_prop_key_length
end
diff --git a/lib/plausible/sites.ex b/lib/plausible/sites.ex
index d7079ac86b..f0f04d1f5d 100644
--- a/lib/plausible/sites.ex
+++ b/lib/plausible/sites.ex
@@ -18,16 +18,33 @@ defmodule Plausible.Sites do
@spec consolidated?(Site.t()) :: boolean()
def consolidated?(%Site{} = site), do: site.consolidated
+
+ def site_id_query_filter(%Site{} = site) do
+ if consolidated?(site) do
+ site_ids = Plausible.ConsolidatedView.Cache.get(site.domain)
+ dynamic([x], fragment("? in ?", x.site_id, ^site_ids))
+ else
+ dynamic([x], x.site_id == ^site.id)
+ end
+ end
else
@spec regular?(Site.t()) :: boolean()
def regular?(%Site{}), do: always(true)
@spec consolidated?(Site.t()) :: boolean()
def consolidated?(%Site{}), do: always(false)
+
+ def site_id_query_filter(%Site{} = site) do
+ dynamic([x], x.site_id == ^site.id)
+ end
end
- def display_name(%Site{} = site) do
- if consolidated?(site), do: "consolidated view", else: site.domain
+ def display_name(%Site{} = site, opts \\ []) do
+ if consolidated?(site) do
+ "#{if opts[:capitalize_consolidated], do: "C", else: "c"}onsolidated view"
+ else
+ site.domain
+ end
end
@shared_link_special_names ["WordPress - Shared Dashboard"]
diff --git a/lib/plausible/stats/current_visitors.ex b/lib/plausible/stats/current_visitors.ex
index 90b516d6dc..73b294ac4f 100644
--- a/lib/plausible/stats/current_visitors.ex
+++ b/lib/plausible/stats/current_visitors.ex
@@ -11,7 +11,7 @@ defmodule Plausible.Stats.CurrentVisitors do
ClickhouseRepo.one(
from e in "events_v2",
- where: e.site_id == ^site.id,
+ where: ^Plausible.Sites.site_id_query_filter(site),
where: e.timestamp >= ^first_datetime,
where: e.name != "engagement",
select: uniq(e.user_id)
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 90769b7b09..ff87f03c07 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -134,7 +134,9 @@ defmodule PlausibleWeb.Email do
base_email()
|> to(email)
|> tag("spike-notification")
- |> subject("Traffic Spike on #{site.domain}")
+ |> subject(
+ "Traffic spike on #{if Plausible.Sites.consolidated?(site), do: "your sites", else: site.domain}"
+ )
|> render("spike_notification.html", %{
site: site,
current_visitors: stats.current_visitors,
@@ -148,7 +150,9 @@ defmodule PlausibleWeb.Email do
base_email()
|> to(email)
|> tag("drop-notification")
- |> subject("Traffic Drop on #{site.domain}")
+ |> subject(
+ "Traffic drop on #{if Plausible.Sites.consolidated?(site), do: "your sites", else: site.domain}"
+ )
|> render("drop_notification.html", %{
site: site,
current_visitors: current_visitors,
diff --git a/lib/plausible_web/mjml/templates/stats_report.mjml.eex b/lib/plausible_web/mjml/templates/stats_report.mjml.eex
index 51b13f3f19..33f4f96e10 100644
--- a/lib/plausible_web/mjml/templates/stats_report.mjml.eex
+++ b/lib/plausible_web/mjml/templates/stats_report.mjml.eex
@@ -41,7 +41,7 @@
<mj-divider />
<% end %>
<mj-text mj-class="text-lg">
- <%= @site.domain %>
+ <%= Plausible.Sites.display_name(@site, capitalize_consolidated: true) %>
</mj-text>
<mj-text mj-class="text-sm" padding="0 25px 0 25px">
<%= @report_name %> Report (<%= @date_label %>)
diff --git a/lib/plausible_web/templates/email/drop_notification.html.heex b/lib/plausible_web/templates/email/drop_notification.html.heex
index b82ef89941..4b3d41a63b 100644
--- a/lib/plausible_web/templates/email/drop_notification.html.heex
+++ b/lib/plausible_web/templates/email/drop_notification.html.heex
@@ -1,8 +1,14 @@
-We've recorded {@current_visitors} visitors on
-<a href={"https://" <> @site.domain}>{@site.domain}</a>
+We've recorded {@current_visitors} visitors
+<%= if Plausible.Sites.consolidated?(@site) do %>
+ across all your sites
+<% else %>
+ on <a href={"https://" <> @site.domain}>{@site.domain}</a>
+<% end %>
in the last 12 hours.
<%= if @dashboard_link do %>
- <br /><br /> View dashboard: <a href={@dashboard_link}>{@dashboard_link}</a>
+ <br /><br /> View dashboard <a href={@dashboard_link}>here</a>.
+<% end %>
+<%= if @installation_link do %>
<br /><br /> Something looks off? Please
<a href={@installation_link}>review your installation</a>
to verify that Plausible has been integrated correctly.
diff --git a/lib/plausible_web/templates/email/spike_notification.html.heex b/lib/plausible_web/templates/email/spike_notification.html.heex
index 4db4ca3a35..32ba841876 100644
--- a/lib/plausible_web/templates/email/spike_notification.html.heex
+++ b/lib/plausible_web/templates/email/spike_notification.html.heex
@@ -1,5 +1,11 @@
There are currently <b>{@current_visitors}</b>
-visitors on <a href={"https://" <> @site.domain}><%= @site.domain %></a>. <br />
+visitors
+<%= if Plausible.Sites.consolidated?(@site) do %>
+ across all your sites.
+<% else %>
+ on <a href={"https://" <> @site.domain}>{@site.domain}</a>.
+<% end %>
+<br />
<%= if Enum.count(@sources) > 0 do %>
<br /> The top sources for current visitors:<br />
<ul>
@@ -21,7 +27,7 @@ visitors on <a href={"https://" <> @site.domain}><%= @site.domain %></a>. <br />
</ul>
<%= if @link do %>
- <br /> View dashboard: <a href={@link}>{@link}</a>
+ <br /> View dashboard <a href={@link}>here</a>
<% end %>
<br /><br /> Congrats on the spike in traffic!
<%= if ce?() do %>
diff --git a/lib/workers/schedule_email_reports.ex b/lib/workers/schedule_email_reports.ex
index 6924224055..f338072fb4 100644
--- a/lib/workers/schedule_email_reports.ex
+++ b/lib/workers/schedule_email_reports.ex
@@ -7,8 +7,7 @@ defmodule Plausible.Workers.ScheduleEmailReports do
@impl Oban.Worker
@doc """
Email reports should be sent on Monday at 9am according to the timezone
- of a site. This job runs every day at midnight to ensure that all sites
- have a scheduled job for email reports.
+ of a site.
"""
def perform(_job) do
schedule_weekly_emails()
diff --git a/lib/workers/send_email_report.ex b/lib/workers/send_email_report.ex
index 62eeabe4d8..82c2a34bdb 100644
--- a/lib/workers/send_email_report.ex
+++ b/lib/workers/send_email_report.ex
@@ -4,6 +4,8 @@ defmodule Plausible.Workers.SendEmailReport do
alias Plausible.Stats.{Query, QueryResult}
+ import Ecto.Query, only: [from: 2]
+
@weekly "weekly"
@monthly "monthly"
@@ -12,9 +14,13 @@ defmodule Plausible.Workers.SendEmailReport do
report_type = report_type(interval)
site =
- Plausible.Site
- |> Repo.get(site_id)
- |> Repo.preload(report_type)
+ from(s in Plausible.Site,
+ where: s.id == ^site_id,
+ inner_join: r in assoc(s, ^report_type),
+ inner_join: t in assoc(s, :team),
+ preload: [{^report_type, r}, {:team, t}]
+ )
+ |> Repo.one()
report = site && Map.get(site, report_type)
diff --git a/lib/workers/traffic_change_notifier.ex b/lib/workers/traffic_change_notifier.ex
index cd28cb894a..e20e5069bf 100644
--- a/lib/workers/traffic_change_notifier.ex
+++ b/lib/workers/traffic_change_notifier.ex
@@ -55,8 +55,8 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
end
defp notify_spike(notification, stats, now) do
- for recipient <- notification.recipients do
- send_spike_notification(recipient, notification.site, stats)
+ for recipient_email <- notification.recipients do
+ send_spike_notification(recipient_email, notification.site, stats)
end
notification
@@ -65,8 +65,8 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
end
defp notify_drop(notification, current_visitors, now) do
- for recipient <- notification.recipients do
- send_drop_notification(recipient, notification.site, current_visitors)
+ for recipient_email <- notification.recipients do
+ send_drop_notification(recipient_email, notification.site, current_visitors)
end
notification
@@ -74,16 +74,16 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
|> Repo.update()
end
- defp send_spike_notification(recipient, site, stats) do
+ defp send_spike_notification(recipient_email, site, stats) do
dashboard_link =
- if Repo.exists?(email_match_query(site, recipient)) do
+ if site_member?(site, recipient_email) do
Routes.stats_url(PlausibleWeb.Endpoint, :stats, site.domain, []) <>
"?__team=#{site.team.identifier}"
end
template =
PlausibleWeb.Email.spike_notification(
- recipient,
+ recipient_email,
site,
stats,
dashboard_link
@@ -92,23 +92,25 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
Plausible.Mailer.send(template)
end
- defp send_drop_notification(recipient, site, current_visitors) do
- {dashboard_link, installation_link} =
- if Repo.exists?(email_match_query(site, recipient)) do
- {
- Routes.stats_url(PlausibleWeb.Endpoint, :stats, site.domain, []) <>
- "?__team=#{site.team.identifier}",
- Routes.site_url(PlausibleWeb.Endpoint, :installation, site.domain,
- flow: PlausibleWeb.Flows.review()
- ) <> "&__team=#{site.team.identifier}"
- }
- else
- {nil, nil}
+ defp send_drop_notification(recipient_email, site, current_visitors) do
+ site_member? = site_member?(site, recipient_email)
+
+ dashboard_link =
+ if site_member? do
+ Routes.stats_url(PlausibleWeb.Endpoint, :stats, site.domain, []) <>
+ "?__team=#{site.team.identifier}"
+ end
+
+ installation_link =
+ if site_member? and Plausible.Sites.regular?(site) do
+ Routes.site_url(PlausibleWeb.Endpoint, :installation, site.domain,
+ flow: PlausibleWeb.Flows.review()
+ ) <> "&__team=#{site.team.identifier}"
end
template =
PlausibleWeb.Email.drop_notification(
- recipient,
+ recipient_email,
site,
current_visitors,
dashboard_link,
@@ -163,12 +165,14 @@ defmodule Plausible.Workers.TrafficChangeNotifier do
Map.put(stats, :pages, pages)
end
- defp email_match_query(site, recipient) do
- from tm in Plausible.Teams.Membership,
+ defp site_member?(site, recipient_email) do
+ from(tm in Plausible.Teams.Membership,
inner_join: u in assoc(tm, :user),
left_join: gm in assoc(tm, :guest_memberships),
where: tm.team_id == ^site.team_id,
where: tm.role != :guest or gm.site_id == ^site.id,
- where: u.email == ^recipient
+ where: u.email == ^recipient_email
+ )
+ |> Repo.exists?()
end
end
diff --git a/test/workers/schedule_email_reports_test.exs b/test/workers/schedule_email_reports_test.exs
index 211611b860..8a0bb63a6c 100644
--- a/test/workers/schedule_email_reports_test.exs
+++ b/test/workers/schedule_email_reports_test.exs
@@ -18,6 +18,25 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
)
end
+ on_ee do
+ test "schedules weekly report for a consolidated view" do
+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
+ new_site(team: team)
+ new_site(team: team)
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:weekly_report, site: consolidated_view, recipients: ["[email protected]"])
+
+ perform_job(ScheduleEmailReports, %{})
+
+ assert_enqueued(
+ worker: SendEmailReport,
+ args: %{site_id: consolidated_view.id, interval: "weekly"},
+ scheduled_at: ScheduleEmailReports.monday_9am(consolidated_view.timezone)
+ )
+ end
+ end
+
test "does not schedule more than one weekly report at a time" do
site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:weekly_report, site: site, recipients: ["[email protected]"])
@@ -64,6 +83,25 @@ defmodule Plausible.Workers.ScheduleEmailReportsTest do
)
end
+ on_ee do
+ test "schedules monthly report for a consolidated view" do
+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
+ new_site(team: team)
+ new_site(team: team)
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:monthly_report, site: consolidated_view, recipients: ["[email protected]"])
+
+ perform_job(ScheduleEmailReports, %{})
+
+ assert_enqueued(
+ worker: SendEmailReport,
+ args: %{site_id: consolidated_view.id, interval: "monthly"},
+ scheduled_at: ScheduleEmailReports.first_of_month_9am(consolidated_view.timezone)
+ )
+ end
+ end
+
test "does not schedule more than one monthly report at a time" do
site = new_site(domain: "test-site.com", timezone: "US/Eastern")
insert(:monthly_report, site: site, recipients: ["[email protected]"])
diff --git a/test/workers/send_email_report_test.exs b/test/workers/send_email_report_test.exs
index 5d57d57bc6..50af21368e 100644
--- a/test/workers/send_email_report_test.exs
+++ b/test/workers/send_email_report_test.exs
@@ -107,6 +107,59 @@ defmodule Plausible.Workers.SendEmailReportTest do
assert text_of_element(html_body, ".page-count") == "2"
end
+ on_ee do
+ test "includes the correct stats for consolidated view" do
+ now = NaiveDateTime.utc_now(:second)
+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
+
+ site1 =
+ new_site(
+ domain: "testsite1.com",
+ team: team,
+ inserted_at: NaiveDateTime.shift(now, day: -8)
+ )
+
+ site2 =
+ new_site(
+ domain: "testsite2.com",
+ team: team,
+ inserted_at: NaiveDateTime.shift(now, day: -8)
+ )
+
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:weekly_report, site: consolidated_view, recipients: ["[email protected]"])
+
+ populate_stats(site1, [
+ build(:pageview,
+ user_id: 123,
+ timestamp: NaiveDateTime.shift(now, day: -7),
+ referrer_source: "Google"
+ ),
+ build(:pageview, user_id: 123, timestamp: NaiveDateTime.shift(now, day: -7))
+ ])
+
+ populate_stats(site2, [
+ build(:pageview, timestamp: NaiveDateTime.shift(now, day: -7))
+ ])
+
+ perform_job(SendEmailReport, %{"site_id" => consolidated_view.id, "interval" => "weekly"})
+
+ assert_delivered_email_matches(%{
+ to: [nil: "[email protected]"],
+ html_body: html_body
+ })
+
+ assert html_body =~ "Consolidated view"
+ assert text_of_element(html_body, ".visitors") == "2"
+ assert text_of_element(html_body, ".pageviews") == "3"
+ assert text_of_element(html_body, ".referrer-name") == "Google"
+ assert text_of_element(html_body, ".referrer-count") == "1"
+ assert text_of_element(html_body, ".page-name") == "/"
+ assert text_of_element(html_body, ".page-count") == "2"
+ end
+ end
+
test "renders correct signs (+/-) and trend colors for positive percentage changes" do
now = NaiveDateTime.utc_now(:second)
week_ago = now |> NaiveDateTime.shift(day: -7)
@@ -411,5 +464,82 @@ defmodule Plausible.Workers.SendEmailReportTest do
to: [nil: "[email protected]"]
)
end
+
+ on_ee do
+ test "includes goal conversions when goals exist on a consolidate view" do
+ beginning_of_last_month =
+ Date.utc_today()
+ |> Date.shift(month: -1)
+ |> Date.beginning_of_month()
+ |> NaiveDateTime.new!(~T[00:00:00])
+
+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
+
+ [site1, site2] =
+ for _ <- 1..2 do
+ new_site(
+ team: team,
+ inserted_at: NaiveDateTime.shift(beginning_of_last_month, day: -1)
+ )
+ end
+
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:monthly_report, site: consolidated_view, recipients: ["[email protected]"])
+
+ _goal1 = insert(:goal, site: consolidated_view, event_name: "Signup")
+ _goal2 = insert(:goal, site: consolidated_view, event_name: "Purchase")
+ _goal3 = insert(:goal, site: consolidated_view, page_path: "/thank-you")
+
+ populate_stats(site1, [
+ build(:pageview,
+ user_id: 123,
+ timestamp: beginning_of_last_month
+ ),
+ build(:event,
+ user_id: 123,
+ name: "Signup",
+ timestamp: beginning_of_last_month
+ ),
+ build(:event,
+ user_id: 125,
+ name: "Purchase",
+ timestamp: NaiveDateTime.shift(beginning_of_last_month, day: 2)
+ )
+ ])
+
+ populate_stats(site2, [
+ build(:pageview,
+ user_id: 124,
+ timestamp: NaiveDateTime.shift(beginning_of_last_month, day: 1)
+ ),
+ build(:event,
+ user_id: 124,
+ name: "Signup",
+ timestamp: NaiveDateTime.shift(beginning_of_last_month, day: 1)
+ ),
+ build(:pageview,
+ user_id: 126,
+ pathname: "/thank-you",
+ timestamp: NaiveDateTime.shift(beginning_of_last_month, day: 3)
+ )
+ ])
+
+ perform_job(SendEmailReport, %{"site_id" => consolidated_view.id, "interval" => "monthly"})
+
+ assert_delivered_email_matches(%{
+ to: [nil: "[email protected]"],
+ html_body: html_body
+ })
+
+ assert html_body =~ "Consolidated view"
+
+ goal_names = find(html_body, ".goal-name") |> Enum.map(&text/1)
+ goal_conversions = find(html_body, ".goal-conversions") |> Enum.map(&text/1)
+
+ assert goal_names == ["Signup", "Purchase", "Visit /thank-you"]
+ assert goal_conversions == ["2", "1", "1"]
+ end
+ end
end
end
diff --git a/test/workers/traffic_change_notifier_test.exs b/test/workers/traffic_change_notifier_test.exs
index e602bfa00b..090e54f93a 100644
--- a/test/workers/traffic_change_notifier_test.exs
+++ b/test/workers/traffic_change_notifier_test.exs
@@ -4,6 +4,9 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
use Plausible.Teams.Test
alias Plausible.Workers.TrafficChangeNotifier
+ @view_dashboard_text "View dashboard"
+ @review_installation_text "review your installation"
+
describe "drops" do
test "does not notify anyone if we've stopped accepting traffic for the owner" do
user = new_user(team: [accept_traffic_until: Date.utc_today()])
@@ -33,7 +36,7 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil)
assert_email_delivered_with(
- subject: "Traffic Drop on #{site.domain}",
+ subject: "Traffic drop on #{site.domain}",
to: [nil: "[email protected]"]
)
end
@@ -69,16 +72,85 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil)
assert_email_delivered_with(
- subject: "Traffic Drop on #{site.domain}",
+ subject: "Traffic drop on #{site.domain}",
to: [nil: "[email protected]"]
)
assert_email_delivered_with(
- subject: "Traffic Drop on #{site.domain}",
+ subject: "Traffic drop on #{site.domain}",
to: [nil: "[email protected]"]
)
end
+ test "includes dashboard and installation links only when recipient is guest or team member" do
+ owner = new_user()
+ {:ok, team} = Plausible.Teams.get_or_create(owner)
+ site = new_site(team: team)
+ team_member = add_member(team, role: :admin)
+ viewer_guest = add_guest(site, role: :viewer, user: new_user())
+ random_email = "[email protected]"
+
+ insert(:drop_notification,
+ site: site,
+ threshold: 10,
+ recipients: [owner.email, team_member.email, viewer_guest.email, random_email]
+ )
+
+ TrafficChangeNotifier.perform(nil)
+
+ expected_subject = "Traffic drop on #{site.domain}"
+
+ four_emails =
+ for _ <- 1..4 do
+ assert_delivered_email_matches(%{
+ html_body: html_body,
+ subject: ^expected_subject,
+ to: [nil: email]
+ })
+
+ %{to: email, html_body: html_body}
+ end
+
+ {[random_recipient_email], site_member_emails} =
+ Enum.split_with(four_emails, &(&1.to == random_email))
+
+ refute random_recipient_email.html_body =~ @view_dashboard_text
+
+ Enum.each(site_member_emails, fn email ->
+ assert email.html_body =~ @view_dashboard_text
+ assert email.html_body =~ @review_installation_text
+ end)
+ end
+
+ on_ee do
+ test "does not link to site installation in a consolidated view traffic drop notification" do
+ %{email: user_email} = user = new_user()
+ {:ok, team} = Plausible.Teams.get_or_create(user)
+ new_site(team: team)
+ new_site(team: team)
+
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:drop_notification,
+ site: consolidated_view,
+ threshold: 10,
+ recipients: [user.email]
+ )
+
+ TrafficChangeNotifier.perform(nil)
+
+ assert_delivered_email_matches(%{
+ html_body: html_body,
+ subject: "Traffic drop on your sites",
+ to: [nil: ^user_email]
+ })
+
+ assert html_body =~ "across all your sites"
+ assert html_body =~ @view_dashboard_text
+ refute html_body =~ @review_installation_text
+ end
+ end
+
test "does not send notifications more than once every 12 hours" do
site = new_site()
@@ -91,7 +163,7 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil, ~N[2021-01-01 00:00:00])
assert_email_delivered_with(
- subject: "Traffic Drop on #{site.domain}",
+ subject: "Traffic drop on #{site.domain}",
to: [nil: "[email protected]"]
)
@@ -102,7 +174,7 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil, ~N[2021-01-01 12:00:01])
assert_email_delivered_with(
- subject: "Traffic Drop on #{site.domain}",
+ subject: "Traffic drop on #{site.domain}",
to: [nil: "[email protected]"]
)
end
@@ -160,16 +232,59 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil)
assert_email_delivered_with(
- subject: "Traffic Spike on #{site.domain}",
+ subject: "Traffic spike on #{site.domain}",
to: [nil: "[email protected]"]
)
assert_email_delivered_with(
- subject: "Traffic Spike on #{site.domain}",
+ subject: "Traffic spike on #{site.domain}",
to: [nil: "[email protected]"]
)
end
+ on_ee do
+ test "notifies traffic spike on consolidated view" do
+ {:ok, team} = new_user() |> Plausible.Teams.get_or_create()
+ site1 = new_site(team: team)
+ site2 = new_site(team: team)
+
+ consolidated_view = new_consolidated_view(team)
+
+ insert(:spike_notification,
+ site: consolidated_view,
+ threshold: 2,
+ recipients: ["[email protected]"]
+ )
+
+ populate_stats(site1, [
+ build(:pageview, referrer_source: "Google", pathname: "/b", timestamp: minutes_ago(1)),
+ build(:pageview, referrer_source: "Google", pathname: "/a", timestamp: minutes_ago(2))
+ ])
+
+ populate_stats(site2, [
+ build(:pageview, referrer_source: "Twitter", pathname: "/a", timestamp: minutes_ago(3))
+ ])
+
+ TrafficChangeNotifier.perform(nil)
+
+ assert_delivered_email_matches(%{
+ html_body: html_body,
+ subject: "Traffic spike on your sites",
+ to: [nil: "[email protected]"]
+ })
+
+ assert html_body =~ "across all your sites"
+
+ assert html_body =~ "The top sources for current visitors:"
+ assert html_body =~ "<b>2</b> visitors from <b>Google</b>"
+ assert html_body =~ "<b>1</b> visitor from <b>Twitter</b>"
+
+ assert html_body =~ "Your top pages being visited:"
+ assert html_body =~ "<b>2</b> visitors on <b>/a</b>"
+ assert html_body =~ "<b>1</b> visitor on <b>/b</b>"
+ end
+ end
+
test "ignores 'Direct / None' source" do
site = new_site()
@@ -300,7 +415,7 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil)
assert_email_delivered_with(
- subject: "Traffic Spike on #{site.domain}",
+ subject: "Traffic spike on #{site.domain}",
to: [nil: "[email protected]"]
)
@@ -321,7 +436,8 @@ defmodule Plausible.Workers.TrafficChangeNotifierTest do
TrafficChangeNotifier.perform(nil)
- assert_email_delivered_with(html_body: ~r/View dashboard:\s+<a href=\"http.+\/example.com/)
+ assert_delivered_email_matches(%{html_body: html_body})
+ assert html_body =~ @view_dashboard_text
end
end