diff --git a/lib/plausible_web/templates/email/weekly_report.html.eex b/lib/plausible_web/templates/email/weekly_report.html.eex index be6ae610b..0afc7a407 100644 --- a/lib/plausible_web/templates/email/weekly_report.html.eex +++ b/lib/plausible_web/templates/email/weekly_report.html.eex @@ -609,7 +609,7 @@ body {
<%= if @login_link do %> - + Login to view your dashboard diff --git a/lib/workers/send_email_report.ex b/lib/workers/send_email_report.ex index 72ee4465c..fda661120 100644 --- a/lib/workers/send_email_report.ex +++ b/lib/workers/send_email_report.ex @@ -46,6 +46,12 @@ defmodule Plausible.Workers.SendEmailReport do :ok end + defp gen_login_link("Weekly"), + do: PlausibleWeb.Endpoint.url() <> "/login?utm_medium=email&utm_source=weekly-report" + + defp gen_login_link(_), + do: PlausibleWeb.Endpoint.url() <> "/login?utm_medium=email&utm_source=monthly-report" + defp send_report(email, site, name, unsubscribe_link, query) do {pageviews, unique_visitors} = Stats.pageviews_and_visitors(site, query) @@ -58,7 +64,7 @@ defmodule Plausible.Workers.SendEmailReport do referrers = Stats.top_sources(site, query, 5, 1, []) pages = Stats.top_pages(site, query, 5, 1, []) user = Plausible.Auth.find_user_by(email: email) - login_link = user && Plausible.Sites.is_owner?(user.id, site) + login_link = user && Plausible.Sites.is_owner?(user.id, site) && gen_login_link(name) template = PlausibleWeb.Email.weekly_report(email, site, @@ -73,7 +79,8 @@ defmodule Plausible.Workers.SendEmailReport do login_link: login_link, pages: pages, query: query, - name: name + name: name, + login_link: login_link ) try do diff --git a/test/workers/send_email_report_test.exs b/test/workers/send_email_report_test.exs index 21c1b5c8e..2db827e32 100644 --- a/test/workers/send_email_report_test.exs +++ b/test/workers/send_email_report_test.exs @@ -25,6 +25,19 @@ defmodule Plausible.Workers.SendEmailReportTest do to: [nil: "user2@email.com"] ) end + + test "includes correct login link in email" do + user = insert(:user) + site = insert(:site, domain: "test-site.com", timezone: "US/Eastern", members: [user]) + insert(:weekly_report, site: site, recipients: [user.email]) + + perform(%{"site_id" => site.id, "interval" => "weekly"}) + + user_email = user.email + assert_delivered_email_matches(%{to: [{_, ^user_email}], html_body: html_body}) + + assert html_body =~ "/login?utm_medium=email&utm_source=weekly-report" + end end describe "monthly_reports" do @@ -50,5 +63,18 @@ defmodule Plausible.Workers.SendEmailReportTest do to: [nil: "user2@email.com"] ) end + + test "includes correct login link in email" do + user = insert(:user) + site = insert(:site, domain: "test-site.com", timezone: "US/Eastern", members: [user]) + insert(:monthly_report, site: site, recipients: [user.email]) + + perform(%{"site_id" => site.id, "interval" => "monthly"}) + + user_email = user.email + assert_delivered_email_matches(%{to: [{_, ^user_email}], html_body: html_body}) + + assert html_body =~ "/login?utm_medium=email&utm_source=monthly-report" + end end end