@ -13,6 +13,11 @@ defmodule Mix.Tasks.SendEmailReports do
of the site . This job runs every hour to be able to send it with hourly precision .
"""
def execute ( args \\ [ ] ) do
send_weekly_emails ( )
send_monthly_emails ( )
end
defp send_weekly_emails ( ) do
sites = Repo . all (
from s in Plausible.Site ,
join : wr in Plausible.Site.WeeklyReport , on : wr . site_id == s . id ,
@ -20,19 +25,44 @@ defmodule Mix.Tasks.SendEmailReports do
where : is_nil ( se ) , # We haven't sent a report for this site on this week
where : fragment ( " EXTRACT(dow from (now() at time zone ?)) " , s . timezone ) == 1 , # It's monday in the local timezone
where : fragment ( " EXTRACT(hour from (now() at time zone ?)) " , s . timezone ) >= 9 , # It's after 9am
select : s ,
preload : [ weekly_report : wr ]
)
for site <- sites do
email = site . weekly_report . email
IO . puts ( " Sending email report for #{ site . domain } to #{ email } " )
send_report ( email , site )
query = Plausible.Stats.Query . from ( site . timezone , %{ " period " = > " 7d " } )
IO . puts ( " Sending weekly report for #{ site . domain } to #{ email } " )
send_report ( email , site , query )
weekly_report_sent ( site )
end
end
defp send_monthly_emails ( ) do
sites = Repo . all (
from s in Plausible.Site ,
join : mr in Plausible.Site.MonthlyReport , on : mr . site_id == s . id ,
left_join : se in " sent_monthly_reports " , on : se . site_id == s . id and se . year == fragment ( " EXTRACT(year from (now() at time zone ?)) " , s . timezone ) and se . month == fragment ( " EXTRACT(month from (now() at time zone ?)) " , s . timezone ) ,
where : is_nil ( se ) , # We haven't sent a report for this site this month
where : fragment ( " EXTRACT(day from (now() at time zone ?)) " , s . timezone ) == 1 , # It's the 1st of the month in the local timezone
where : fragment ( " EXTRACT(hour from (now() at time zone ?)) " , s . timezone ) >= 9 , # It's after 9am
preload : [ monthly_report : mr ]
)
for site <- sites do
email = site . monthly_report . email
last_month = Timex . now ( site . timezone ) |> Timex . shift ( months : - 1 ) |> Timex . beginning_of_month |> Timex . format! ( " {ISOdate} " )
query = Plausible.Stats.Query . from ( site . timezone , %{ " period " = > " month " , " date " = > last_month } )
IO . puts ( " Sending monthly report for #{ site . domain } to #{ email } " )
send_report ( email , site , query )
monthly_report_sent ( site )
end
end
defp send_report ( email , site ) do
query = Plausible.Stats.Query . from ( site . timezone , %{ " period " = > " 7d " } )
defp send_report ( email , site , query ) do
{ pageviews , unique_visitors } = Plausible.Stats . pageviews_and_visitors ( site , query )
{ change_pageviews , change_visitors } = Plausible.Stats . compare_pageviews_and_visitors ( site , query , { pageviews , unique_visitors } )
referrers = Plausible.Stats . top_referrers ( site , query )
@ -51,11 +81,9 @@ defmodule Mix.Tasks.SendEmailReports do
pages : pages ,
query : query
) |> Plausible.Mailer . deliver_now ( )
email_report_sent ( site )
end
defp email _report_sent( site ) do
defp weekly _report_sent( site ) do
{ year , week } = Timex . now ( site . timezone ) |> DateTime . to_date |> Timex . iso_week
Repo . insert_all ( " sent_weekly_reports " , [ %{
@ -65,4 +93,15 @@ defmodule Mix.Tasks.SendEmailReports do
timestamp : Timex . now ( )
} ] )
end
defp monthly_report_sent ( site ) do
date = Timex . now ( site . timezone ) |> DateTime . to_date
Repo . insert_all ( " sent_monthly_reports " , [ %{
site_id : site . id ,
year : date . year ,
month : date . month ,
timestamp : Timex . now ( )
} ] )
end
end