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
Clarify that custom events are billed separately from events (#607)
Uku Tahte7a7deef8446
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.exindex 97c65de5c8..c637d65caa 100644--- a/lib/plausible/billing/billing.ex+++ b/lib/plausible/billing/billing.ex@@ -129,16 +129,19 @@ defmodule Plausible.Billing doenddef usage(user) do+ {pageviews, custom_events} = usage_breakdown(user)+ pageviews + custom_events+ end++ def usage_breakdown(user) douser = 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)enddefp format_subscription(params) dodiff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.exindex d1acdcf7a2..fcb76d4752 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) doClickhouseRepo.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.exindex 2194049e5c..44dbedacf9 100644--- a/lib/plausible_web/controllers/auth_controller.ex+++ b/lib/plausible_web/controllers/auth_controller.ex@@ -289,10 +289,15 @@ defmodule PlausibleWeb.AuthController dodef user_settings(conn, _params) dochangeset = 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)enddiff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.exindex 4557d3ae74..1f002f2a77 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}) dobase_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+ )enddef trial_over_email(user) dodiff --git a/lib/plausible_web/templates/auth/user_settings.html.eex b/lib/plausible_web/templates/auth/user_settings.html.eexindex 5d04a2fdfb..335b72bbe3 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 @@</div><h3 class="text-xl font-bold mt-8 dark:text-gray-100">Your usage</h3>-- <div class="py-2 dark:text-gray-100">- <b><%= delimit_integer(Plausible.Billing.usage(@conn.assigns[:current_user])) %></b>- pageviews in the last 30 days+ <p class="mt-1 text-sm leading-5 text-gray-500 dark:text-gray-200">Last 30 days total usage across all of your sites</p>+ <div class="py-2">+ <div class="flex flex-col">+ <div class="-my-2 sm:-mx-6 lg:-mx-8">+ <div class="py-2 align-middle inline-block min-w-full sm:px-6 lg:px-8">+ <div>+ <table class="min-w-full divide-y divide-gray-200 text-gray-900 dark:text-gray-100">+ <tbody class="bg-white dark:bg-gray-800 divide-y divide-gray-200">+ <tr>+ <td class="py-4 whitespace-nowrap text-sm">+ Pageviews+ </td>+ <td class="py-4 whitespace-nowrap text-sm">+ <%= delimit_integer(@usage_pageviews) %>+ </td>+ </tr>+ <tr>+ <td class="py-4 whitespace-nowrap text-sm">+ Custom events+ </td>+ <td class="py-4 whitespace-nowrap text-sm">+ <%= delimit_integer(@usage_custom_events) %>+ </td>+ </tr>+ <tr>+ <td class="py-4 whitespace-nowrap text-sm font-medium">+ Total billable pageviews+ </td>+ <td class="py-4 whitespace-nowrap text-sm font-medium">+ <%= delimit_integer(@usage_pageviews + @usage_custom_events) %>+ </td>+ </tr>++ </tbody>+ </table>+ </div>+ </div>+ </div>+ </div></div><%= cond do %>diff --git a/lib/plausible_web/templates/billing/upgrade.html.eex b/lib/plausible_web/templates/billing/upgrade.html.eexindex 33bad364b9..b627772612 100644--- a/lib/plausible_web/templates/billing/upgrade.html.eex+++ b/lib/plausible_web/templates/billing/upgrade.html.eex@@ -10,7 +10,7 @@<div class="w-full max-w-4xl px-4 mt-4 mx-auto flex flex-col md:flex-row"><div x-data="{planSize: '10k', billingCycle: 'monthly'}" class="flex-1 bg-white dark:bg-gray-800 shadow-md rounded px-8 py-4 mb-4 mt-8"><div class="w-full py-4 dark:text-gray-100">- <span>You've used <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> pageviews in the last 30 days</span>+ <span>You've used <b><%= PlausibleWeb.AuthView.delimit_integer(@usage) %></b> billable pageviews in the last 30 days</span></div><div class="pt-2"></div>diff --git a/lib/plausible_web/templates/email/trial_upgrade_email.html.eex b/lib/plausible_web/templates/email/trial_upgrade_email.html.eexindex 296040559d..7c0ec0947d 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) %>,<br /><br />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.<br /><br />-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.<br /><br />diff --git a/lib/workers/send_trial_notifications.ex b/lib/workers/send_trial_notifications.exindex 8bd989b356..d93bab4041 100644--- a/lib/workers/send_trial_notifications.ex+++ b/lib/workers/send_trial_notifications.ex@@ -54,14 +54,14 @@ defmodule Plausible.Workers.SendTrialNotifications doenddefp 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()enddefp 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.exsindex 3612d7795e..ccf6d47a60 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 doperform()- assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", 3))+ assert_delivered_email(PlausibleWeb.Email.trial_upgrade_email(user, "tomorrow", {3, 0}))endtest "sends an upgrade email the day the trial ends" do@@ -43,7 +43,25 @@ defmodule Plausible.Workers.SendTrialNotificationsTest doperform()- 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."endtest "sends a trial over email the day after the trial ends" do@@ -70,56 +88,56 @@ defmodule Plausible.Workers.SendTrialNotificationsTest dotest "suggests 10k/mo plan" douser = 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."endtest "suggests 100k/mo plan" douser = 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."endtest "suggests 200k/mo plan" douser = 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."endtest "suggests 500k/mo plan" douser = 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."endtest "suggests 1m/mo plan" douser = 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."endtest "suggests 2m/mo plan" douser = 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."endtest "suggests 5m/mo plan" douser = 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."endtest "does not suggest a plan above that" douser = 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"endend