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
Fix upgrade page for legacy trials + bug fixes (#3486)
RobertJoonasf977351ae2ba
diff --git a/lib/plausible/billing/plans.ex b/lib/plausible/billing/plans.exindex bd7860fdfe..6a175338eb 100644--- a/lib/plausible/billing/plans.ex+++ b/lib/plausible/billing/plans.ex@@ -27,7 +27,7 @@ defmodule Plausible.Billing.Plans doModule.put_attribute(__MODULE__, :external_resource, path)end- @business_tier_launch ~D[2023-11-07]+ @business_tier_launch ~N[2023-12-01 12:00:00]@spec growth_plans_for(User.t()) :: [Plan.t()]@doc """@@ -44,7 +44,9 @@ defmodule Plausible.Billing.Plans docond doApplication.get_env(:plausible, :environment) == "dev" -> @sandbox_plans- !owned_plan -> if v4_available, do: @plans_v4, else: @plans_v3+ is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3+ is_nil(owned_plan) && v4_available -> @plans_v4+ is_nil(owned_plan) -> @plans_v3owned_plan.kind == :business -> @plans_v4owned_plan.generation == 1 -> @plans_v1owned_plan.generation == 2 -> @plans_v2@@ -60,6 +62,7 @@ defmodule Plausible.Billing.Plans docond doApplication.get_env(:plausible, :environment) == "dev" -> @sandbox_plans+ is_nil(owned_plan) && Timex.before?(user.inserted_at, @business_tier_launch) -> @plans_v3owned_plan && owned_plan.generation < 4 -> @plans_v3true -> @plans_v4end@@ -242,6 +245,10 @@ defmodule Plausible.Billing.Plans doenddefp all() do- @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4+ @legacy_plans ++ @plans_v1 ++ @plans_v2 ++ @plans_v3 ++ @plans_v4 ++ sandbox_plans()+ end++ defp sandbox_plans() do+ if Application.get_env(:plausible, :environment) == "dev", do: @sandbox_plans, else: []endenddiff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.exindex d11f7333e4..348e66354b 100644--- a/lib/plausible/billing/quota.ex+++ b/lib/plausible/billing/quota.ex@@ -230,7 +230,7 @@ defmodule Plausible.Billing.Quota docase Plans.get_subscription_plan(user.subscription) do%EnterprisePlan{} -> Feature.list()%Plan{features: features} -> features- :free_10k -> [Goals]+ :free_10k -> [Goals, Props, StatsAPI]nil -> Feature.list()endend@@ -241,11 +241,21 @@ defmodule Plausible.Billing.Quota doselect: %{site_id: sm.site_id}end+ @spec below_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()+ @doc """+ Returns whether the usage is below the limit or not.+ Returns false if usage is equal to the limit.+ """+ def below_limit?(usage, limit) do+ if limit == :unlimited, do: true, else: usage < limit+ end+@spec within_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()@doc """- Returns whether the limit has been exceeded or not.+ Returns whether the usage is within the limit or not.+ Returns true if usage is equal to the limit."""def within_limit?(usage, limit) do- if limit == :unlimited, do: true, else: usage < limit+ if limit == :unlimited, do: true, else: usage <= limitendenddiff --git a/lib/plausible/site/memberships/create_invitation.ex b/lib/plausible/site/memberships/create_invitation.exindex 91244d8d36..8bbe99ee46 100644--- a/lib/plausible/site/memberships/create_invitation.ex+++ b/lib/plausible/site/memberships/create_invitation.ex@@ -117,14 +117,14 @@ defmodule Plausible.Site.Memberships.CreateInvitation dosite_usage = Plausible.Repo.aggregate(Quota.team_member_usage_query(site.owner, site), :count)usage_after_transfer = current_usage + site_usage- Quota.within_limit?(usage_after_transfer, limit)+ Quota.below_limit?(usage_after_transfer, limit)enddefp within_site_limit_after_transfer?(new_owner) dolimit = Quota.site_limit(new_owner)usage_after_transfer = Quota.site_usage(new_owner) + 1- Quota.within_limit?(usage_after_transfer, limit)+ Quota.below_limit?(usage_after_transfer, limit)enddefp has_access_to_site_features?(site, new_owner) do@@ -174,7 +174,7 @@ defmodule Plausible.Site.Memberships.CreateInvitation dolimit = Quota.team_member_limit(site.owner)usage = Quota.team_member_usage(site.owner)- if Quota.within_limit?(usage, limit),+ if Quota.below_limit?(usage, limit),do: :ok,else: {:error, {:over_limit, limit}}enddiff --git a/lib/plausible/sites.ex b/lib/plausible/sites.exindex 1df4b9b7e5..3532e3b9d7 100644--- a/lib/plausible/sites.ex+++ b/lib/plausible/sites.ex@@ -87,7 +87,7 @@ defmodule Plausible.Sites dolimit = Quota.site_limit(user)usage = Quota.site_usage(user)- if Quota.within_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit}+ if Quota.below_limit?(usage, limit), do: {:ok, usage}, else: {:error, limit}end)|> Ecto.Multi.insert(:site, site_changeset)|> Ecto.Multi.insert(:site_membership, fn %{site: site} ->diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.exindex e8a67de81c..1c46395db5 100644--- a/lib/plausible_web/controllers/billing_controller.ex+++ b/lib/plausible_web/controllers/billing_controller.ex@@ -124,14 +124,12 @@ defmodule PlausibleWeb.BillingController doenddef change_plan_preview(conn, %{"plan_id" => new_plan_id}) do- with {:ok, {subscription, preview_info}} <-- preview_subscription(conn.assigns.current_user, new_plan_id) do+ user = conn.assigns.current_user+ business_tier_enabled? = FunWithFlags.enabled?(:business_tier, for: user)++ with {:ok, {subscription, preview_info}} <- preview_subscription(user, new_plan_id) doback_action =- if FunWithFlags.enabled?(:business_tier, for: conn.assigns.current_user) do- :choose_plan- else- :change_plan_form- end+ if business_tier_enabled?, do: :choose_plan, else: :change_plan_formrender(conn, "change_plan_preview.html",back_link: Routes.billing_path(conn, back_action),@@ -142,7 +140,22 @@ defmodule PlausibleWeb.BillingController do)else_ ->- redirect(conn, to: Routes.billing_path(conn, :upgrade))+ redirect_to = if business_tier_enabled?, do: :choose_plan, else: :upgrade++ msg =+ "Something went wrong with loading your plan change information. Please try again, or contact us at [email protected] if the issue persists."++ Sentry.capture_message("Error loading change plan preview",+ extra: %{+ message: msg,+ new_plan_id: new_plan_id,+ user_id: user.id+ }+ )++ conn+ |> put_flash(:error, msg)+ |> redirect(to: Routes.billing_path(conn, redirect_to))endenddiff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.exindex a41179734e..c4a493c01a 100644--- a/lib/plausible_web/controllers/site_controller.ex+++ b/lib/plausible_web/controllers/site_controller.ex@@ -13,12 +13,12 @@ defmodule PlausibleWeb.SiteController dolimit = Plausible.Billing.Quota.site_limit(current_user)usage = Plausible.Billing.Quota.site_usage(current_user)- within_limit? = Plausible.Billing.Quota.within_limit?(usage, limit)+ below_limit? = Plausible.Billing.Quota.below_limit?(usage, limit)render(conn, "new.html",changeset: Plausible.Site.changeset(%Plausible.Site{}),is_first_site: usage == 0,- is_at_limit: !within_limit?,+ is_at_limit: !below_limit?,site_limit: limit,layout: {PlausibleWeb.LayoutView, "focus.html"})diff --git a/lib/plausible_web/live/choose_plan.ex b/lib/plausible_web/live/choose_plan.exindex 58a87028ef..2dfaaacb66 100644--- a/lib/plausible_web/live/choose_plan.ex+++ b/lib/plausible_web/live/choose_plan.ex@@ -285,7 +285,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do<.contact_button class="bg-indigo-600 hover:bg-indigo-500 text-white" /><% end %></div>- <%= if @kind == :growth && @plan_to_render.generation < 4 do %>+ <%= if @owned && @kind == :growth && @plan_to_render.generation < 4 do %><.growth_grandfathering_notice /><% else %><ul@@ -396,11 +396,15 @@ defmodule PlausibleWeb.Live.ChoosePlan doenddefp change_plan_link(assigns) do+ confirmed =+ if assigns.confirm_message, do: "confirm(\"#{assigns.confirm_message}\")", else: "true"++ assigns = assign(assigns, :confirmed, confirmed)+~H"""- <.link+ <buttonid={"#{@kind}-checkout"}- onclick={if @confirm_message, do: "if (!confirm(\"#{@confirm_message}\")) {e.preventDefault()}"}- href={Routes.billing_path(PlausibleWeb.Endpoint, :change_plan_preview, @paddle_product_id)}+ onclick={"if (#{@confirmed}) {window.location = '#{Routes.billing_path(PlausibleWeb.Endpoint, :change_plan_preview, @paddle_product_id)}'}"}class={["w-full mt-6 block rounded-md py-2 px-3 text-center text-sm font-semibold leading-6 text-white",!@checkout_disabled && "bg-indigo-600 hover:bg-indigo-500",@@ -408,7 +412,7 @@ defmodule PlausibleWeb.Live.ChoosePlan do]}><%= @change_plan_link_text %>- </.link>+ </button>"""enddiff --git a/lib/workers/check_usage.ex b/lib/workers/check_usage.exindex a276202b18..58d987210d 100644--- a/lib/workers/check_usage.ex+++ b/lib/workers/check_usage.ex@@ -69,7 +69,7 @@ defmodule Plausible.Workers.CheckUsage dosite_limit = check_site_limit_for_enterprise(subscriber)case {pageview_limit, site_limit} do- {{:within_limit, _}, {:within_limit, _}} ->+ {{:below_limit, _}, {:below_limit, _}} ->nil{{_, {last_cycle, last_cycle_usage}}, {_, {site_usage, site_allowance}}} ->@@ -126,15 +126,15 @@ defmodule Plausible.Workers.CheckUsage do{last_last_cycle_usage, last_cycle_usage} =billing_mod.last_two_billing_months_usage(subscriber)- exceeded_last_cycle? = not Plausible.Billing.Quota.within_limit?(last_cycle_usage, limit)+ exceeded_last_cycle? = not Plausible.Billing.Quota.below_limit?(last_cycle_usage, limit)exceeded_last_last_cycle? =- not Plausible.Billing.Quota.within_limit?(last_last_cycle_usage, limit)+ not Plausible.Billing.Quota.below_limit?(last_last_cycle_usage, limit)if exceeded_last_last_cycle? && exceeded_last_cycle? do{:over_limit, {last_cycle, last_cycle_usage}}else- {:within_limit, {last_cycle, last_cycle_usage}}+ {:below_limit, {last_cycle, last_cycle_usage}}endend@@ -142,8 +142,8 @@ defmodule Plausible.Workers.CheckUsage dolimit = subscriber.enterprise_plan.site_limitusage = Plausible.Billing.Quota.site_usage(subscriber)- if Plausible.Billing.Quota.within_limit?(usage, limit) do- {:within_limit, {usage, limit}}+ if Plausible.Billing.Quota.below_limit?(usage, limit) do+ {:below_limit, {usage, limit}}else{:over_limit, {usage, limit}}enddiff --git a/test/plausible/billing/plans_test.exs b/test/plausible/billing/plans_test.exsindex c34bf7d55f..f45e12c3b3 100644--- a/test/plausible/billing/plans_test.exs+++ b/test/plausible/billing/plans_test.exs@@ -5,6 +5,7 @@ defmodule Plausible.Billing.PlansTest do@legacy_plan_id "558746"@v1_plan_id "558018"@v2_plan_id "654177"+ @v3_plan_id "749342"@v4_plan_id "857097"@v3_business_plan_id "857481"@v4_business_plan_id "857105"@@ -25,8 +26,13 @@ defmodule Plausible.Billing.PlansTest doassert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v2_plan_idend- test "growth_plans_for/1 returns v4 plans for everyone else" do- user = insert(:user)+ test "growth_plans_for/1 shows v3 pricing for users who signed up before the business tier" do+ user = insert(:user, inserted_at: ~U[2023-10-01T00:00:00Z])+ assert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v3_plan_id+ end++ test "growth_plans_for/1 shows v4 plans for everyone else" do+ user = insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])assert List.first(Plans.growth_plans_for(user)).monthly_product_id == @v4_plan_idend@@ -60,8 +66,13 @@ defmodule Plausible.Billing.PlansTest doassert List.first(business_plans).monthly_product_id == @v3_business_plan_idend+ test "business_plans_for/1 returns v3 business plans for users who signed up before the business tier release" do+ user = insert(:user, inserted_at: ~U[2023-10-01T00:00:00Z])+ assert List.first(Plans.business_plans_for(user)).monthly_product_id == @v3_business_plan_id+ end+test "business_plans_for/1 returns v4 business plans for everyone else" do- user = insert(:user)+ user = insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])business_plans = Plans.business_plans_for(user)assert Enum.all?(business_plans, &(&1.kind == :business))diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exsindex 157be54b34..9af82292fb 100644--- a/test/plausible/billing/quota_test.exs+++ b/test/plausible/billing/quota_test.exs@@ -97,21 +97,21 @@ defmodule Plausible.Billing.QuotaTest doassert Quota.site_usage(user) == 3end- describe "within_limit?/2" do+ describe "below_limit?/2" dotest "returns true when quota is not exceeded" do- assert Quota.within_limit?(3, 5)+ assert Quota.below_limit?(3, 5)endtest "returns true when limit is :unlimited" do- assert Quota.within_limit?(10_000, :unlimited)+ assert Quota.below_limit?(10_000, :unlimited)endtest "returns false when usage is at limit" do- refute Quota.within_limit?(3, 3)+ refute Quota.below_limit?(3, 3)endtest "returns false when usage exceeds the limit" do- refute Quota.within_limit?(10, 3)+ refute Quota.below_limit?(10, 3)endend@@ -127,6 +127,18 @@ defmodule Plausible.Billing.QuotaTest doassert Quota.exceeded_limits(usage, plan) == [:monthly_pageview_limit, :site_limit]end++ test "if limits are reached, they're not exceeded" do+ usage = %{+ monthly_pageviews: 10_000,+ team_members: 2,+ sites: 50+ }++ plan = Plans.find(@v3_plan_id)++ assert Quota.exceeded_limits(usage, plan) == []+ endenddescribe "monthly_pageview_limit/1" do@@ -474,9 +486,9 @@ defmodule Plausible.Billing.QuotaTest doassert [Goals, StatsAPI, Props] == Quota.allowed_features_for(user_on_v3)end- test "returns [Goals] when user is on free_10k plan" do+ test "returns [Goals, Props, StatsAPI] when user is on free_10k plan" douser = insert(:user, subscription: build(:subscription, paddle_plan_id: "free_10k"))- assert [Goals] == Quota.allowed_features_for(user)+ assert [Goals, Props, StatsAPI] == Quota.allowed_features_for(user)endtest "returns all features when user is on an enterprise plan" dodiff --git a/test/plausible_web/controllers/billing_controller_test.exs b/test/plausible_web/controllers/billing_controller_test.exsindex 60b3ec58f2..88ca8110bb 100644--- a/test/plausible_web/controllers/billing_controller_test.exs+++ b/test/plausible_web/controllers/billing_controller_test.exs@@ -5,6 +5,7 @@ defmodule PlausibleWeb.BillingControllerTest doalias Plausible.Billing.Subscription@v4_growth_plan "857097"+ @v4_business_plan "857105"describe "GET /upgrade" dosetup [:create_user, :log_in]@@ -218,6 +219,35 @@ defmodule PlausibleWeb.BillingControllerTest doendend+ describe "GET /billing/change-plan/preview/:plan_id" do+ setup [:create_user, :log_in]++ test "renders preview information about the plan change", %{conn: conn, user: user} do+ insert(:subscription, user: user, paddle_plan_id: @v4_growth_plan)++ html_response =+ conn+ |> get(Routes.billing_path(conn, :change_plan_preview, @v4_business_plan))+ |> html_response(200)++ assert html_response =~+ "Your card will be charged a pro-rated amount for the current billing period"++ assert html_response =~ "€-72.6"+ assert html_response =~ "Back"+ assert html_response =~ ~s[<a href="/billing/choose-plan"]+ end++ test "flashes error and redirects to choose-plan page", %{conn: conn} do+ conn = get(conn, Routes.billing_path(conn, :change_plan_preview, @v4_business_plan))++ assert Phoenix.Flash.get(conn.assigns.flash, :error) =~+ "Something went wrong with loading your plan change information. Please try again, or contact us at [email protected] if the issue persists."++ assert redirected_to(conn) == Routes.billing_path(conn, :choose_plan)+ end+ end+@enterprise_contact_link "[email protected]"describe "GET /upgrade-to-enterprise-plan (already subscribed to latest enterprise plan)" dodiff --git a/test/plausible_web/live/choose_plan_test.exs b/test/plausible_web/live/choose_plan_test.exsindex a2cc24dbc9..a95315ab4d 100644--- a/test/plausible_web/live/choose_plan_test.exs+++ b/test/plausible_web/live/choose_plan_test.exs@@ -30,6 +30,37 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do@enterprise_plan_box "#enterprise-plan-box"+ describe "for a legacy trial (user registered before business tiers release)" do+ setup %{conn: conn} do+ user = insert(:user, inserted_at: ~N[2023-10-25 12:00:00])+ {:ok, conn: conn} = log_in(%{conn: conn, user: user})+ {:ok, conn: conn, user: user}+ end++ test "renders v3 plan benefits, but no grandfathering notice", %{conn: conn} do+ {:ok, _lv, doc} = get_liveview(conn)++ growth_box = text_of_element(doc, @growth_plan_box)+ business_box = text_of_element(doc, @business_plan_box)++ assert growth_box =~ "Unlimited team members"+ assert growth_box =~ "Up to 50 sites"+ assert growth_box =~ "Intuitive, fast and privacy-friendly dashboard"+ assert growth_box =~ "Email/Slack reports"+ assert growth_box =~ "Google Analytics import"+ assert growth_box =~ "Goals and custom events"+ assert growth_box =~ "Stats API"+ assert growth_box =~ "Custom Properties"++ assert business_box =~ "Everything in Growth"+ assert business_box =~ "Funnels"+ assert business_box =~ "Ecommerce revenue attribution"+ assert business_box =~ "Priority support"++ refute growth_box =~ "Your subscription has been grandfathered"+ end+ end+describe "for a user with no subscription" dosetup [:create_user, :log_in]@@ -354,16 +385,16 @@ defmodule PlausibleWeb.Live.ChoosePlanTest dogrowth_checkout_button = find(doc, @growth_checkout_button)- assert text_of_attr(growth_checkout_button, "href") =~- Routes.billing_path(conn, :change_plan_preview, @v4_growth_200k_yearly_plan_id)+ assert text_of_attr(growth_checkout_button, "onclick") =~+ "if (true) {window.location = '#{Routes.billing_path(conn, :change_plan_preview, @v4_growth_200k_yearly_plan_id)}'}"element(lv, @slider_input) |> render_change(%{slider: 6})doc = element(lv, @monthly_interval_button) |> render_click()business_checkout_button = find(doc, @business_checkout_button)- assert text_of_attr(business_checkout_button, "href") =~- Routes.billing_path(conn, :change_plan_preview, @v4_business_5m_monthly_plan_id)+ assert text_of_attr(business_checkout_button, "onclick") =~+ "if (true) {window.location = '#{Routes.billing_path(conn, :change_plan_preview, @v4_business_5m_monthly_plan_id)}'}"endend@@ -417,6 +448,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest dobuild(:site_membership, user: user, role: :owner),build(:site_membership, user: build(:user)),build(:site_membership, user: build(:user)),+ build(:site_membership, user: build(:user)),build(:site_membership, user: build(:user))])@@ -449,7 +481,7 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do{:ok, _lv, doc} = get_liveview(conn)assert text_of_attr(find(doc, @growth_checkout_button), "onclick") =~- "if (!confirm(\"This plan does not support Custom Properties, Revenue Goals and Stats API, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {e.preventDefault()}"+ "if (confirm(\"This plan does not support Custom Properties, Revenue Goals and Stats API, which you are currently using. Please note that by subscribing to this plan you will lose access to these features.\")) {window.location = "endenddiff --git a/test/support/paddle_api_mock.ex b/test/support/paddle_api_mock.exindex 64505f9605..a2aee7f078 100644--- a/test/support/paddle_api_mock.ex+++ b/test/support/paddle_api_mock.ex@@ -26,6 +26,25 @@ defmodule Plausible.PaddleApi.Mock do}}end+ def update_subscription_preview(_user, _new_plan_id) do+ {:ok,+ %{+ "immediate_payment" => %{+ "amount" => -72.6,+ "currency" => "EUR",+ "date" => "2023-11-05"+ },+ "next_payment" => %{+ "amount" => 47.19,+ "currency" => "EUR",+ "date" => "2023-12-05"+ },+ "plan_id" => 63_852,+ "subscription_id" => 600_279,+ "user_id" => 666_317+ }}+ end+def get_invoices(nil), do: {:error, :no_invoices}def get_invoices(%{paddle_subscription_id: nil}), do: {:error, :no_invoices}diff --git a/test/support/test_utils.ex b/test/support/test_utils.exindex b0987a860f..3bdf74cfab 100644--- a/test/support/test_utils.ex+++ b/test/support/test_utils.ex@@ -35,7 +35,7 @@ defmodule Plausible.TestUtils doenddef create_user(_) do- {:ok, user: Factory.insert(:user)}+ {:ok, user: Factory.insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z])}enddef create_site(%{user: user}) do