2020-06-28 13:24:54 +03:00
|
|
|
defmodule Plausible.Workers.SendEmailReport do
|
|
|
|
use Plausible.Repo
|
|
|
|
use Oban.Worker, queue: :send_email_reports, max_attempts: 1
|
|
|
|
alias Plausible.Stats.Query
|
|
|
|
alias Plausible.Stats.Clickhouse, as: Stats
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
2021-04-26 11:32:18 +03:00
|
|
|
def perform(%Oban.Job{args: %{"interval" => "weekly", "site_id" => site_id}}) do
|
2020-06-28 13:24:54 +03:00
|
|
|
site = Repo.get(Plausible.Site, site_id) |> Repo.preload(:weekly_report)
|
2021-04-22 12:11:33 +03:00
|
|
|
today = Timex.now(site.timezone) |> DateTime.to_date()
|
|
|
|
date = Timex.shift(today, weeks: -1) |> Timex.end_of_week() |> Date.to_iso8601()
|
|
|
|
query = Query.from(site.timezone, %{"period" => "7d", "date" => date})
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
for email <- site.weekly_report.recipients do
|
|
|
|
unsubscribe_link =
|
2020-10-05 15:01:54 +03:00
|
|
|
PlausibleWeb.Endpoint.url() <>
|
2020-06-28 13:24:54 +03:00
|
|
|
"/sites/#{URI.encode_www_form(site.domain)}/weekly-report/unsubscribe?email=#{email}"
|
|
|
|
|
|
|
|
send_report(email, site, "Weekly", unsubscribe_link, query)
|
|
|
|
end
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2020-06-28 13:24:54 +03:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
2021-04-26 11:32:18 +03:00
|
|
|
def perform(%Oban.Job{args: %{"interval" => "monthly", "site_id" => site_id}}) do
|
2020-06-28 13:24:54 +03:00
|
|
|
site = Repo.get(Plausible.Site, site_id) |> Repo.preload(:monthly_report)
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2020-06-28 13:24:54 +03:00
|
|
|
last_month =
|
|
|
|
Timex.now(site.timezone)
|
|
|
|
|> Timex.shift(months: -1)
|
|
|
|
|> Timex.beginning_of_month()
|
|
|
|
|
|
|
|
query =
|
|
|
|
Query.from(site.timezone, %{
|
|
|
|
"period" => "month",
|
|
|
|
"date" => Timex.format!(last_month, "{ISOdate}")
|
|
|
|
})
|
|
|
|
|
|
|
|
for email <- site.monthly_report.recipients do
|
|
|
|
unsubscribe_link =
|
2020-10-05 15:01:54 +03:00
|
|
|
PlausibleWeb.Endpoint.url() <>
|
2020-06-28 13:24:54 +03:00
|
|
|
"/sites/#{URI.encode_www_form(site.domain)}/monthly-report/unsubscribe?email=#{email}"
|
|
|
|
|
|
|
|
send_report(email, site, Timex.format!(last_month, "{Mfull}"), unsubscribe_link, query)
|
|
|
|
end
|
2020-07-21 09:58:00 +03:00
|
|
|
|
2020-06-28 13:24:54 +03:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_report(email, site, name, unsubscribe_link, query) do
|
|
|
|
{pageviews, unique_visitors} = Stats.pageviews_and_visitors(site, query)
|
|
|
|
|
|
|
|
{change_pageviews, change_visitors} =
|
|
|
|
Stats.compare_pageviews_and_visitors(site, query, {pageviews, unique_visitors})
|
|
|
|
|
|
|
|
bounce_rate = Stats.bounce_rate(site, query)
|
2020-12-23 12:18:45 +03:00
|
|
|
prev_bounce_rate = Stats.bounce_rate(site, Query.shift_back(query, site))
|
2020-06-28 13:24:54 +03:00
|
|
|
change_bounce_rate = if prev_bounce_rate > 0, do: bounce_rate - prev_bounce_rate
|
2020-09-28 11:29:24 +03:00
|
|
|
referrers = Stats.top_sources(site, query, 5, 1, [])
|
2020-12-17 12:35:27 +03:00
|
|
|
pages = Stats.top_pages(site, query, 5, 1, [])
|
2020-06-28 13:24:54 +03:00
|
|
|
user = Plausible.Auth.find_user_by(email: email)
|
2021-06-16 15:00:07 +03:00
|
|
|
login_link = user && Plausible.Sites.is_member?(user.id, site)
|
2020-06-28 13:24:54 +03:00
|
|
|
|
2020-07-21 09:58:00 +03:00
|
|
|
template =
|
|
|
|
PlausibleWeb.Email.weekly_report(email, site,
|
|
|
|
unique_visitors: unique_visitors,
|
|
|
|
change_visitors: change_visitors,
|
|
|
|
pageviews: pageviews,
|
|
|
|
change_pageviews: change_pageviews,
|
|
|
|
bounce_rate: bounce_rate,
|
|
|
|
change_bounce_rate: change_bounce_rate,
|
|
|
|
referrers: referrers,
|
|
|
|
unsubscribe_link: unsubscribe_link,
|
|
|
|
login_link: login_link,
|
|
|
|
pages: pages,
|
|
|
|
query: query,
|
2021-04-13 12:08:22 +03:00
|
|
|
name: name
|
2020-07-21 09:58:00 +03:00
|
|
|
)
|
2020-06-28 13:24:54 +03:00
|
|
|
|
|
|
|
try do
|
|
|
|
Plausible.Mailer.send_email(template)
|
|
|
|
rescue
|
|
|
|
_ -> nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|