Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Customize email address for email reports
Uku Taht7b8b48982be9
diff --git a/lib/mix/tasks/send_email_reports.ex b/lib/mix/tasks/send_email_reports.exindex caf96fa78a..b541d1e5f7 100644--- a/lib/mix/tasks/send_email_reports.ex+++ b/lib/mix/tasks/send_email_reports.ex@@ -15,19 +15,19 @@ defmodule Mix.Tasks.SendEmailReports dodef execute(args \\ []) dosites = Repo.all(from s in Plausible.Site,- join: es in "email_settings", on: es.site_id == s.id,+ join: es in Plausible.Site.EmailSettings, on: es.site_id == s.id,left_join: se in "sent_email_reports", on: se.site_id == s.id and se.year == fragment("EXTRACT(year from (now() at time zone ?))", s.timezone) and se.week == fragment("EXTRACT(week from (now() at time zone ?))", s.timezone),where: is_nil(se), # We haven't sent a report for this site on this weekwhere: fragment("EXTRACT(dow from (now() at time zone ?))", s.timezone) == 1, # It's monday in the local timezonewhere: fragment("EXTRACT(hour from (now() at time zone ?))", s.timezone) >= 9, # It's after 9amselect: s,- preload: :members+ preload: [email_settings: es])for site <- sites do- [owner] = site.members- IO.puts("Sending email report for #{site.domain} to #{owner.email}")- send_report(owner.email, site)+ email = site.email_settings.email+ IO.puts("Sending email report for #{site.domain} to #{email}")+ send_report(email, site)endenddiff --git a/lib/plausible/site/email_settings.ex b/lib/plausible/site/email_settings.exindex cbbeab73a6..aff1b928ff 100644--- a/lib/plausible/site/email_settings.ex+++ b/lib/plausible/site/email_settings.ex@@ -1,8 +1,10 @@defmodule Plausible.Site.EmailSettings douse Ecto.Schemaimport Ecto.Changeset+ @mail_regex ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/schema "email_settings" do+ field :email, :stringbelongs_to :site, Plausible.Sitetimestamps()@@ -10,8 +12,9 @@ defmodule Plausible.Site.EmailSettings dodef changeset(settings, attrs \\ %{}) dosettings- |> cast(attrs, [:site_id])- |> validate_required([:site_id])+ |> cast(attrs, [:site_id, :email])+ |> validate_required([:site_id, :email])+ |> validate_format(:email, @mail_regex)|> unique_constraint(:site)endenddiff --git a/lib/plausible/site/schema.ex b/lib/plausible/site/schema.exindex 3bd90618f8..7680cb21a3 100644--- a/lib/plausible/site/schema.ex+++ b/lib/plausible/site/schema.ex@@ -11,6 +11,7 @@ defmodule Plausible.Site domany_to_many :members, User, join_through: Plausible.Site.Membershiphas_one :google_auth, GoogleAuth+ has_one :email_settings, Plausible.Site.EmailSettingstimestamps()enddiff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex 24569e0507..0233177277 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -40,19 +40,33 @@ defmodule PlausibleWeb.SiteController do!google_site["error"]end- weekly_report_enabled = Repo.exists?(from es in "email_settings", where: es.site_id == ^site.id)+ report_changeset = Repo.get_by(Plausible.Site.EmailSettings, site_id: site.id)+ |> Plausible.Site.EmailSettings.changeset(%{})changeset = Plausible.Site.changeset(site, %{})+conn|> assign(:skip_plausible_tracking, true)|> render("settings.html",site: site,- weekly_report_enabled: weekly_report_enabled,+ report_changeset: report_changeset,google_search_console_verified: google_search_console_verified,changeset: changeset)end++ def update_email_settings(conn, %{"website" => website, "email_settings" => email_settings}) do+ site = Sites.get_for_user!(conn.assigns[:current_user].id, website)+ Repo.get_by(Plausible.Site.EmailSettings, site_id: site.id)+ |> Plausible.Site.EmailSettings.changeset(email_settings)+ |> Repo.update!++ conn+ |> put_flash(:success, "Email address saved succesfully")+ |> redirect(to: "/#{site.domain}/settings")+ end+def update_settings(conn, %{"website" => website, "site" => site_params}) dosite = Sites.get_for_user!(conn.assigns[:current_user].id, website)changeset = site |> Plausible.Site.changeset(site_params)diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex d23159f1f3..bf03f0ccab 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -87,6 +87,7 @@ defmodule PlausibleWeb.Router dopost "/sites/:website/make-private", SiteController, :make_privatepost "/sites/:website/email-report/enable", SiteController, :enable_email_reportpost "/sites/:website/email-report/disable", SiteController, :disable_email_report+ put "/sites/:website/email-report", SiteController, :update_email_settingsget "/:website/snippet", SiteController, :add_snippetget "/:website/settings", SiteController, :settingsput "/:website/settings", SiteController, :update_settingsdiff --git a/lib/plausible_web/templates/site/settings.html.eex b/lib/plausible_web/templates/site/settings.html.eexindex 9456c31a43..a5eea93fc1 100644--- a/lib/plausible_web/templates/site/settings.html.eex+++ b/lib/plausible_web/templates/site/settings.html.eex@@ -95,14 +95,14 @@</div><div class="max-w-md mx-auto bg-white shadow-md rounded rounded-t-none border-t-2 border-indigo-lightest px-8 pt-6 pb-8 mt-10" id="google-auth">- <div class="flex items-center justify-between">+ <div><h2>Email reports</h2></div><div class="my-4 border-b border-grey-light"></div>- <div class="my-4 flex items-center">- <%= if @weekly_report_enabled do %>+ <div class="my-8 flex items-center">+ <%= if @report_changeset do %><%= button(to: "/sites/#{@site.domain}/email-report/disable", method: :post, class: "border rounded-full border-grey flex items-center cursor-pointer w-8 bg-green justify-end") do %><span class="rounded-full border w-4 h-4 border-grey shadow-inner bg-white shadow"></span><% end %>@@ -114,9 +114,19 @@<span class="ml-2">Receive a weekly email report every Monday</span><% end %></div>- <div class="text-sm text-grey-darker mt-6">- Configured email: <%= @conn.assigns[:current_user].email %>- </div>+ <%= if @report_changeset do %>+ <div class="my-4 border-b border-grey-light"></div>+ <div class="text-sm text-grey-darker mt-6">+ <%= form_for @report_changeset, "/sites/#{@site.domain}/email-report", [class: "max-w-xs"], fn f -> %>+ <div class="my-4">+ <%= label f, :email, "Email address", class: "block text-grey-darker text-sm font-bold mb-2" %>+ <%= email_input f, :email, class: "transition bg-grey-lighter appearance-none border border-transparent rounded w-full p-2 text-grey-darker leading-normal appearance-none focus:outline-none focus:border-grey-light" %>+ <%= error_tag f, :email %>+ </div>+ <%= submit "Set email address", class: "button mt-2" %>+ <% end %>+ </div>+ <% end %></div><%= form_for @conn, "/", [class: "bg-white max-w-md mx-auto shadow-md rounded rounded-t-none border-t-2 border-indigo-lightest px-8 pt-6 pb-8 mb-4 mt-16"], fn f -> %>diff --git a/priv/repo/migrations/20190910120900_add_email_address_to_settings.exs b/priv/repo/migrations/20190910120900_add_email_address_to_settings.exsnew file mode 100644index 0000000000..bff4f428e3--- /dev/null+++ b/priv/repo/migrations/20190910120900_add_email_address_to_settings.exs@@ -0,0 +1,23 @@+defmodule Plausible.Repo.Migrations.AddEmailAddressToSettings do+ use Ecto.Migration+ use Plausible.Repo+ alias Plausible.Site.EmailSettings++ def change do+ alter table(:email_settings) do+ add :email, :citext+ end++ flush()+ all = Repo.all(EmailSettings)++ for settings <- all do+ mem = Repo.get_by(Plausible.Site.Membership, site_id: settings.site_id) |> Repo.preload(:user)+ EmailSettings.changeset(settings, %{email: mem.user.email}) |> Repo.update!+ end++ alter table(:email_settings) do+ modify :email, :citext, null: false+ end+ end+end