From e7a7deef8446e5d6c0ac99dd112fa84a1a8792f7 Mon Sep 17 00:00:00 2001 From: Uku Taht Date: Fri, 15 Jan 2021 16:28:57 +0200 Subject: [PATCH] Clarify that custom events are billed separately from events (#607) * Clarify that custom events are billed separately from events * Fix for dark mode --- lib/plausible/billing/billing.ex | 17 +++++--- lib/plausible/stats/clickhouse.ex | 18 ++++++++ .../controllers/auth_controller.ex | 7 ++- lib/plausible_web/email.ex | 9 +++- .../templates/auth/user_settings.html.eex | 43 +++++++++++++++++-- .../templates/billing/upgrade.html.eex | 2 +- .../email/trial_upgrade_email.html.eex | 6 +-- lib/workers/send_trial_notifications.ex | 4 +- .../workers/send_trial_notifications_test.exs | 38 +++++++++++----- 9 files changed, 114 insertions(+), 30 deletions(-) diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 97c65de5c..c637d65ca 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -129,16 +129,19 @@ defmodule Plausible.Billing do end def usage(user) do + {pageviews, custom_events} = usage_breakdown(user) + pageviews + custom_events + end + + def usage_breakdown(user) do user = Repo.preload(user, :sites) - Enum.reduce(user.sites, 0, fn site, total -> - total + site_usage(site) - end) - end + Enum.reduce(user.sites, {0, 0}, fn site, {pageviews, custom_events} -> + usage = Plausible.Stats.Clickhouse.usage(site) - defp site_usage(site) do - q = Plausible.Stats.Query.from(site.timezone, %{"period" => "30d"}) - Plausible.Stats.Clickhouse.total_events(site, q) + {pageviews + Map.get(usage, :pageviews, 0), + custom_events + Map.get(usage, :custom_events, 0)} + end) end defp format_subscription(params) do diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index d1acdcf7a..fcb76d475 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -158,6 +158,24 @@ defmodule Plausible.Stats.Clickhouse do ) end + def usage(site) do + q = Plausible.Stats.Query.from(site.timezone, %{"period" => "30d"}) + {first_datetime, last_datetime} = utc_boundaries(q, site.timezone) + + ClickhouseRepo.all( + from e in "events", + where: e.domain == ^site.domain, + where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime, + group_by: e.name, + select: {e.name, fragment("count(*)")} + ) + |> Enum.map(fn {ev_name, count} -> + ev_name = if ev_name == "pageview", do: :pageviews, else: :custom_events + {ev_name, count} + end) + |> Enum.into(%{}) + end + def pageviews_and_visitors(site, query) do ClickhouseRepo.one( from e in base_query_w_sessions(site, query), diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index 2194049e5..44dbedacf 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -289,10 +289,15 @@ defmodule PlausibleWeb.AuthController do def user_settings(conn, _params) do changeset = Auth.User.changeset(conn.assigns[:current_user]) + {usage_pageviews, usage_custom_events} = + Plausible.Billing.usage_breakdown(conn.assigns[:current_user]) + render(conn, "user_settings.html", changeset: changeset, subscription: conn.assigns[:current_user].subscription, - theme: conn.assigns[:current_user].theme || "system" + theme: conn.assigns[:current_user].theme || "system", + usage_pageviews: usage_pageviews, + usage_custom_events: usage_custom_events ) end diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex index 4557d3ae7..1f002f2a7 100644 --- a/lib/plausible_web/email.ex +++ b/lib/plausible_web/email.ex @@ -70,12 +70,17 @@ defmodule PlausibleWeb.Email do |> render("trial_one_week_reminder.html", user: user) end - def trial_upgrade_email(user, day, pageviews) do + def trial_upgrade_email(user, day, {pageviews, custom_events}) do base_email() |> to(user) |> tag("trial-upgrade-email") |> subject("Your Plausible trial ends #{day}") - |> render("trial_upgrade_email.html", user: user, day: day, pageviews: pageviews) + |> render("trial_upgrade_email.html", + user: user, + day: day, + custom_events: custom_events, + usage: pageviews + custom_events + ) end def trial_over_email(user) do diff --git a/lib/plausible_web/templates/auth/user_settings.html.eex b/lib/plausible_web/templates/auth/user_settings.html.eex index 5d04a2fdf..335b72bbe 100644 --- a/lib/plausible_web/templates/auth/user_settings.html.eex +++ b/lib/plausible_web/templates/auth/user_settings.html.eex @@ -71,10 +71,45 @@

Your usage

- -
- <%= delimit_integer(Plausible.Billing.usage(@conn.assigns[:current_user])) %> - pageviews in the last 30 days +

Last 30 days total usage across all of your sites

+
+
+
+
+
+ + + + + + + + + + + + + + + + +
+ Pageviews + + <%= delimit_integer(@usage_pageviews) %> +
+ Custom events + + <%= delimit_integer(@usage_custom_events) %> +
+ Total billable pageviews + + <%= delimit_integer(@usage_pageviews + @usage_custom_events) %> +
+
+
+
+
<%= cond do %> diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eex index 33bad364b..b62777261 100644 --- a/lib/plausible_web/templates/billing/upgrade.html.eex +++ b/lib/plausible_web/templates/billing/upgrade.html.eex @@ -10,7 +10,7 @@
- You've used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> pageviews in the last 30 days + You've used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> billable pageviews in the last 30 days
diff --git a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex index 296040559..7c0ec0947 100644 --- a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex +++ b/lib/plausible_web/templates/email/trial_upgrade_email.html.eex @@ -2,9 +2,9 @@ Hey <%= user_salutation(@user) %>,

Thanks for exploring Plausible, a simple and privacy-friendly alternative to Google Analytics. Your free 30-day trial is ending <%= @day %>, but you can keep using Plausible by upgrading to a paid plan.

-In the last month, your account has used <%= PlausibleWeb.AuthView.delimit_integer(@pageviews) %> pageviews. -<%= if @pageviews <= 4_500_000 do %> -Based on that we recommend you select the <%= suggested_plan_name(@pageviews) %> plan which runs at <%= suggested_plan_cost(@pageviews) %>. +In the last month, your account has used <%= PlausibleWeb.AuthView.delimit_integer(@usage) %> billable pageviews<%= if @custom_events > 0, do: " and custom events in total", else: "" %>. +<%= if @usage <= 4_500_000 do %> +Based on that we recommend you select the <%= suggested_plan_name(@usage) %> plan which runs at <%= suggested_plan_cost(@usage) %>. You can also go with yearly billing to get 33% off on your plan.

diff --git a/lib/workers/send_trial_notifications.ex b/lib/workers/send_trial_notifications.ex index 8bd989b35..d93bab404 100644 --- a/lib/workers/send_trial_notifications.ex +++ b/lib/workers/send_trial_notifications.ex @@ -54,14 +54,14 @@ defmodule Plausible.Workers.SendTrialNotifications do end defp send_tomorrow_reminder(user) do - usage = Plausible.Billing.usage(user) + usage = Plausible.Billing.usage_breakdown(user) PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", usage) |> send_email() end defp send_today_reminder(user) do - usage = Plausible.Billing.usage(user) + usage = Plausible.Billing.usage_breakdown(user) PlausibleWeb.Email.trial_upgrade_email(user, "today", usage) |> send_email() diff --git a/test/workers/send_trial_notifications_test.exs b/test/workers/send_trial_notifications_test.exs index 3612d7795..ccf6d47a6 100644 --- a/test/workers/send_trial_notifications_test.exs +++ b/test/workers/send_trial_notifications_test.exs @@ -34,7 +34,7 @@ defmodule Plausible.Workers.SendTrialNotificationsTest do perform() - assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", 3)) + assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", {3, 0})) end test "sends an upgrade email the day the trial ends" do @@ -43,7 +43,25 @@ defmodule Plausible.Workers.SendTrialNotificationsTest do perform() - assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "today", 3)) + assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "today", {3, 0})) + end + + test "does not include custom event note if user has not used custom events" do + user = insert(:user, trial_expiry_date: Timex.today()) + + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {9_000, 0}) + + assert email.html_body =~ + "In the last month, your account has used 9,000 billable pageviews." + end + + test "includes custom event note if user has used custom events" do + user = insert(:user, trial_expiry_date: Timex.today()) + + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {9_000, 100}) + + assert email.html_body =~ + "In the last month, your account has used 9,100 billable pageviews and custom events in total." end test "sends a trial over email the day after the trial ends" do @@ -70,56 +88,56 @@ defmodule Plausible.Workers.SendTrialNotificationsTest do test "suggests 10k/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 9_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {9_000, 0}) assert email.html_body =~ "we recommend you select the 10k/mo plan which runs at $6/mo." end test "suggests 100k/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 90_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {90_000, 0}) assert email.html_body =~ "we recommend you select the 100k/mo plan which runs at $12/mo." end test "suggests 200k/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 180_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {180_000, 0}) assert email.html_body =~ "we recommend you select the 200k/mo plan which runs at $18/mo." end test "suggests 500k/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 450_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {450_000, 0}) assert email.html_body =~ "we recommend you select the 500k/mo plan which runs at $27/mo." end test "suggests 1m/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 900_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {900_000, 0}) assert email.html_body =~ "we recommend you select the 1m/mo plan which runs at $48/mo." end test "suggests 2m/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 1_800_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {1_800_000, 0}) assert email.html_body =~ "we recommend you select the 2m/mo plan which runs at $69/mo." end test "suggests 5m/mo plan" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 4_500_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {4_500_000, 0}) assert email.html_body =~ "we recommend you select the 5m/mo plan which runs at $99/mo." end test "does not suggest a plan above that" do user = insert(:user) - email = PlausibleWeb.Email.trial_upgrade_email(user, "today", 10_000_000) + email = PlausibleWeb.Email.trial_upgrade_email(user, "today", {10_000_000, 0}) assert email.html_body =~ "please reply back to this email to get a quote for your volume" end end