Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

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.ex
index 97c65de5c8..c637d65caa 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 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) 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 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 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 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}) 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 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.eex
index 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.eex
index 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.ex
index 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 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 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 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