Feature gates (#3401)

This pull request implements limits to funnels, revenue goals and custom props based on the site owner plan. It extends the current "premium feature" notice to account for the new plans, trials and the on-going private preview. Stats API is not in the context of this pull request, but will be implemented likewise.
pull/3408/head r2023.10.11-209
Vini Brasil 3 years ago committed by GitHub
parent 90cb7cdef8
commit 303b3509f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -44,10 +44,6 @@ body {
overflow-x: hidden;
}
button:disabled {
cursor: default;
}
blockquote {
@apply px-4 py-2 my-4 border-l-4 border-gray-500;
}

@ -0,0 +1,156 @@
defmodule Plausible.Billing.Feature do
@moduledoc """
This module provides an interface for managing features, e.g. Revenue Goals,
Funnels and Custom Properties.
Feature modules have functions for toggling the feature on/off and checking
whether the feature is available for a site/user.
When defining new features, the following options are expected by the
`__using__` macro:
* `:display_name` - human-readable display name of the feature
* `:toggle_field` - the field in the %Plausible.Site{} schema that toggles
the feature. If `nil` or not set, toggle/2 silently returns `:ok`
* `:extra_feature` - an atom representing the feature name in the plan JSON
file (see also Plausible.Billing.Plan). If `nil` or not set,
`check_availability/1` silently returns `:ok`
Functions defined by `__using__` can be overridden if needed.
"""
@doc """
Returns the human-readable display name of the feature.
"""
@callback display_name() :: String.t()
@doc """
Returns the %Plausible.Site{} field that toggles the feature on and off.
"""
@callback toggle_field() :: atom()
@doc """
Toggles the feature on and off for a site. Returns
`{:error, :upgrade_required}` when toggling a feature the site owner does not
have access to.
"""
@callback toggle(Plausible.Site.t(), Keyword.t()) :: :ok | {:error, :upgrade_required}
@doc """
Checks whether a feature is enabled or not. Returns false when the feature is
disabled or the user does not have access to it.
"""
@callback enabled?(Plausible.Site.t()) :: boolean()
@doc """
Checks whether the site owner or the user plan includes the given feature.
"""
@callback check_availability(Plausible.Site.t() | Plausible.Auth.User.t()) ::
:ok | {:error, :upgrade_required}
@features [
Plausible.Billing.Feature.Funnels,
Plausible.Billing.Feature.Goals,
Plausible.Billing.Feature.Props,
Plausible.Billing.Feature.RevenueGoals
]
@doc """
Lists all available feature modules.
"""
def list() do
@features
end
@doc false
defmacro __using__(opts \\ []) do
quote location: :keep do
@behaviour Plausible.Billing.Feature
alias Plausible.Billing.Quota
@impl true
def display_name, do: Keyword.get(unquote(opts), :display_name)
@impl true
def toggle_field, do: Keyword.get(unquote(opts), :toggle_field)
@impl true
def enabled?(site) do
cond do
check_availability(site) !== :ok -> false
is_nil(toggle_field()) -> true
true -> Map.get(site, toggle_field())
end
end
@impl true
def check_availability(site_or_user)
def check_availability(%Plausible.Site{} = site) do
site = Plausible.Repo.preload(site, :owner)
check_availability(site.owner)
end
def check_availability(%Plausible.Auth.User{} = user) do
extra_feature = Keyword.get(unquote(opts), :extra_feature)
cond do
is_nil(extra_feature) -> :ok
not FunWithFlags.enabled?(:business_tier, for: user) -> :ok
extra_feature in Quota.extra_features_limit(user) -> :ok
true -> {:error, :upgrade_required}
end
end
@impl true
def toggle(site, opts \\ []) do
with key when not is_nil(key) <- toggle_field(),
:ok <- check_availability(site) do
override = Keyword.get(opts, :override)
toggle = if is_boolean(override), do: override, else: !Map.fetch!(site, toggle_field())
site
|> Ecto.Changeset.change(%{toggle_field() => toggle})
|> Plausible.Repo.update()
else
nil = _feature_not_togglable -> :ok
{:error, :upgrade_required} -> {:error, :upgrade_required}
end
end
defoverridable check_availability: 1, toggle: 1, toggle: 2
end
end
end
defmodule Plausible.Billing.Feature.Funnels do
@moduledoc false
use Plausible.Billing.Feature,
display_name: "Funnels",
toggle_field: :funnels_enabled,
extra_feature: :funnels
end
defmodule Plausible.Billing.Feature.RevenueGoals do
@moduledoc false
use Plausible.Billing.Feature,
display_name: "Revenue Goals",
extra_feature: :revenue_goals
end
defmodule Plausible.Billing.Feature.Goals do
@moduledoc false
use Plausible.Billing.Feature,
display_name: "Goals",
toggle_field: :conversions_enabled
end
defmodule Plausible.Billing.Feature.Props do
@moduledoc false
use Plausible.Billing.Feature,
display_name: "Custom Properties",
toggle_field: :props_enabled,
extra_feature: :props
end

@ -2,8 +2,7 @@ defmodule Plausible.Billing.Plan do
@moduledoc false
@derive Jason.Encoder
@enforce_keys ~w(kind site_limit monthly_pageview_limit team_member_limit volume monthly_product_id yearly_product_id)a
@enforce_keys ~w(kind site_limit monthly_pageview_limit team_member_limit extra_features volume monthly_product_id yearly_product_id)a
defstruct @enforce_keys ++ [:monthly_cost, :yearly_cost]
@type t() ::
@ -16,7 +15,8 @@ defmodule Plausible.Billing.Plan do
monthly_cost: Money.t() | nil,
yearly_cost: Money.t() | nil,
monthly_product_id: String.t() | nil,
yearly_product_id: String.t() | nil
yearly_product_id: String.t() | nil,
extra_features: [atom()]
}
| :enterprise
@ -31,6 +31,8 @@ defmodule Plausible.Billing.Plans do
alias Plausible.Billing.{Subscription, Plan, EnterprisePlan}
alias Plausible.Auth.User
@available_features ["props", "revenue_goals", "funnels"]
for f <- [
:plans_v1,
:plans_v2,
@ -54,12 +56,20 @@ defmodule Plausible.Billing.Plans do
_any -> raise ArgumentError, "Failed to parse team member limit from plan JSON files"
end
extra_features =
Enum.map(raw.extra_features, fn feature ->
if feature in @available_features,
do: String.to_atom(feature),
else: raise(ArgumentError, "Failed to parse extra features from plan JSON files")
end)
volume = PlausibleWeb.StatsView.large_number_format(raw.monthly_pageview_limit)
raw
|> Map.put(:volume, volume)
|> Map.put(:kind, String.to_atom(raw.kind))
|> Map.put(:team_member_limit, team_member_limit)
|> Map.put(:extra_features, extra_features)
|> Plan.new()
end)

@ -106,13 +106,8 @@ defmodule Plausible.Billing.Quota do
with the user's sites.
"""
def team_member_usage(user) do
owned_sites_query =
from sm in Plausible.Site.Membership,
where: sm.role == :owner and sm.user_id == ^user.id,
select: %{site_id: sm.site_id}
team_members_query =
from os in subquery(owned_sites_query),
from os in subquery(owned_sites_query(user)),
inner_join: sm in Plausible.Site.Membership,
on: sm.site_id == os.site_id,
inner_join: u in assoc(sm, :user),
@ -120,7 +115,7 @@ defmodule Plausible.Billing.Quota do
invitations_and_team_members_query =
from i in Plausible.Auth.Invitation,
inner_join: os in subquery(owned_sites_query),
inner_join: os in subquery(owned_sites_query(user)),
on: i.site_id == os.site_id,
where: i.role != :owner,
select: %{email: i.email},
@ -134,6 +129,61 @@ defmodule Plausible.Billing.Quota do
Plausible.Repo.one(query)
end
@spec extra_features_usage(Plausible.Auth.User.t()) :: [atom()]
@doc """
Returns a list of extra features the given user's sites uses.
"""
def extra_features_usage(user) do
props_usage_query =
from s in Plausible.Site,
inner_join: os in subquery(owned_sites_query(user)),
on: s.id == os.site_id,
where: fragment("cardinality(?) > 0", s.allowed_event_props)
funnels_usage_query =
from f in Plausible.Funnel,
inner_join: os in subquery(owned_sites_query(user)),
on: f.site_id == os.site_id
revenue_goals_usage =
from g in Plausible.Goal,
inner_join: os in subquery(owned_sites_query(user)),
on: g.site_id == os.site_id,
where: not is_nil(g.currency)
queries = [
props: props_usage_query,
funnels: funnels_usage_query,
revenue_goals: revenue_goals_usage
]
Enum.reduce(queries, [], fn {feature, query}, acc ->
if Plausible.Repo.exists?(query), do: [feature | acc], else: acc
end)
end
@all_features [:props, :revenue_goals, :funnels]
@doc """
Returns a list of extra features the user can use. Trial users have the
ability to use all features during their trial.
"""
def extra_features_limit(user) do
user = Plausible.Users.with_subscription(user)
case Plans.get_subscription_plan(user.subscription) do
%Plausible.Billing.EnterprisePlan{} -> @all_features
%Plausible.Billing.Plan{extra_features: extra_features} -> extra_features
:free_10k -> []
nil -> @all_features
end
end
defp owned_sites_query(user) do
from sm in Plausible.Site.Membership,
where: sm.role == :owner and sm.user_id == ^user.id,
select: %{site_id: sm.site_id}
end
@spec within_limit?(non_neg_integer(), non_neg_integer() | :unlimited) :: boolean()
@doc """
Returns whether the limit has been exceeded or not.

@ -5,6 +5,8 @@ defmodule Plausible.Goals do
use Plausible.Funnel
@spec create(Plausible.Site.t(), map(), Keyword.t()) ::
{:ok, Goal.t()} | {:error, Ecto.Changeset.t()} | {:error, :upgrade_required}
@doc """
Creates a Goal for a site.
@ -28,8 +30,8 @@ defmodule Plausible.Goals do
{:ok, :upsert, goal} ->
Repo.preload(goal, :site)
{:error, changeset} ->
Repo.rollback(changeset)
{:error, cause} ->
Repo.rollback(cause)
end
end)
end
@ -192,7 +194,8 @@ defmodule Plausible.Goals do
changeset = Goal.changeset(%Goal{site_id: site.id}, params)
with {:ok, goal} <- Repo.insert(changeset, insert_opts) do
with :ok <- maybe_check_feature_access(site, changeset),
{:ok, goal} <- Repo.insert(changeset, insert_opts) do
# Upsert with `on_conflict: :nothing` strategy
# will result in goal struct missing primary key field
# which is generated by the database.
@ -209,6 +212,14 @@ defmodule Plausible.Goals do
end
end
defp maybe_check_feature_access(site, changeset) do
if Ecto.Changeset.get_field(changeset, :currency) do
Plausible.Billing.Feature.RevenueGoals.check_availability(site)
else
:ok
end
end
defp maybe_trim(%Goal{} = goal) do
# we make sure that even if we saved goals erroneously with trailing
# space, it's removed during fetch

@ -17,16 +17,18 @@ defmodule Plausible.Props do
def max_prop_value_length, do: @max_prop_value_length
@spec allow(Plausible.Site.t(), [prop()] | prop()) ::
{:ok, Plausible.Site.t()} | {:error, Ecto.Changeset.t()}
{:ok, Plausible.Site.t()} | {:error, Ecto.Changeset.t()} | {:error, :upgrade_required}
@doc """
Allows a prop key or a list of props keys to be included in ClickHouse
queries. Allowing prop keys does not affect ingestion, as we don't want any
data to be dropped or lost.
"""
def allow(site, prop_or_props) do
site
|> allow_changeset(prop_or_props)
|> Plausible.Repo.update()
with :ok <- Plausible.Billing.Feature.Props.check_availability(site) do
site
|> allow_changeset(prop_or_props)
|> Plausible.Repo.update()
end
end
def allow_changeset(site, prop_or_props) do
@ -67,7 +69,8 @@ defmodule Plausible.Props do
end)
end
@spec allow_existing_props(Plausible.Site.t()) :: {:ok, Plausible.Site.t()}
@spec allow_existing_props(Plausible.Site.t()) ::
{:ok, Plausible.Site.t()} | {:error, :upgrade_required}
@doc """
Allows the #{@max_props} most frequent props keys for a specific site over
the past 6 months.

@ -41,6 +41,8 @@ defmodule Plausible.Site do
has_one :monthly_report, Plausible.Site.MonthlyReport
has_one :custom_domain, Plausible.Site.CustomDomain
has_one :spike_notification, Plausible.Site.SpikeNotification
has_one :ownership, Plausible.Site.Membership, where: [role: :owner]
has_one :owner, through: [:ownership, :user]
# If `from_cache?` is set, the struct might be incomplete - see `Plausible.Site.Cache`.
# Use `Plausible.Repo.reload!(cached_site)` to pre-fill missing fields if
@ -166,21 +168,6 @@ defmodule Plausible.Site do
)
end
@togglable_features ~w[conversions_enabled funnels_enabled props_enabled]a
def feature_toggle_change(site, property, opts \\ [])
when property in @togglable_features do
override = Keyword.get(opts, :override)
attrs =
if is_boolean(override) do
%{property => override}
else
%{property => !Map.fetch!(site, property)}
end
cast(site, attrs, @togglable_features)
end
def remove_imported_data(site) do
change(site, imported_data: nil)
end

@ -156,9 +156,9 @@ defmodule Plausible.Sites do
end
defp check_team_member_limit(site, _role) do
site_owner = owner_for(site)
limit = Quota.team_member_limit(site_owner)
usage = Quota.team_member_usage(site_owner)
site = Plausible.Repo.preload(site, :owner)
limit = Quota.team_member_limit(site.owner)
usage = Quota.team_member_usage(site.owner)
if Quota.within_limit?(usage, limit),
do: :ok,
@ -301,15 +301,4 @@ defmodule Plausible.Sites do
where: sm.user_id == ^user.id
)
end
def owner_for(site) do
Repo.one(
from(u in Plausible.Auth.User,
join: sm in Site.Membership,
on: sm.user_id == u.id,
where: sm.site_id == ^site.id,
where: sm.role == :owner
)
)
end
end

@ -7,6 +7,54 @@ defmodule PlausibleWeb.Components.Billing do
alias PlausibleWeb.Router.Helpers, as: Routes
alias Plausible.Billing.Subscription
attr(:site, Plausible.Site, required: true)
attr(:current_user, Plausible.Auth.User, required: true)
attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list())
attr(:grandfathered?, :boolean, default: false)
attr(:size, :atom, default: :sm)
attr(:rest, :global)
# credo:disable-for-next-line Credo.Check.Refactor.CyclomaticComplexity
def extra_feature_notice(assigns) do
private_preview? = not FunWithFlags.enabled?(:business_tier, for: assigns.current_user)
owner? = assigns.current_user.id == assigns.site.owner.id
has_access? = assigns.feature_mod.check_availability(assigns.site.owner) == :ok
message =
cond do
private_preview? && assigns.grandfathered? ->
"#{assigns.feature_mod.display_name()} is an upcoming premium functionality that's free-to-use during the private preview. Existing subscribers will be grandfathered and will keep having access to this feature without any change to their plan."
private_preview? ->
"#{assigns.feature_mod.display_name()} is an upcoming premium functionality that's free-to-use during the private preview. Pricing will be announced soon."
Plausible.Billing.on_trial?(assigns.site.owner) ->
"#{assigns.feature_mod.display_name()} is part of the Plausible Business plan. You can access it during your trial, but you'll need to subscribe to the Business plan to retain access after the trial ends."
not has_access? && owner? ->
~H"""
<%= @feature_mod.display_name() %> is part of the Plausible Business plan. To get access to it, please
<.link class="underline" href={Routes.billing_path(PlausibleWeb.Endpoint, :upgrade)}>
upgrade your subscription
</.link> to the Business plan.
"""
not has_access? && not owner? ->
"#{assigns.feature_mod.display_name()} is part of the Plausible Business plan. To get access to it, please reach out to the site owner to upgrade your subscription to the Business plan."
true ->
nil
end
assigns = assign(assigns, :message, message)
~H"""
<.notice :if={@message} class="rounded-t-md rounded-b-none" size={@size} {@rest}>
<%= @message %>
</.notice>
"""
end
slot(:inner_block, required: true)
attr(:rest, :global)

@ -5,17 +5,15 @@ defmodule PlausibleWeb.Components.Generic do
use Phoenix.Component
attr :title, :string, default: "Notice"
attr :class, :string, default: ""
attr :size, :atom, default: :sm
attr :rest, :global
slot :inner_block
def notice(assigns) do
~H"""
<div class={[
"rounded-md bg-yellow-50 dark:bg-yellow-100 p-4",
@class
]}>
<div class="rounded-md bg-yellow-50 dark:bg-yellow-100 p-4" {@rest}>
<div class="flex">
<div class="flex-shrink-0">
<div :if={@size !== :xs} class="flex-shrink-0">
<svg
class="h-5 w-5 text-yellow-400"
viewBox="0 0 20 20"
@ -30,8 +28,13 @@ defmodule PlausibleWeb.Components.Generic do
</svg>
</div>
<div class="ml-3">
<h3 class="text-sm font-medium text-yellow-800 dark:text-yellow-900"><%= @title %></h3>
<div class="mt-2 text-sm text-yellow-700 dark:text-yellow-800">
<h3
:if={@size !== :xs}
class={"text-#{@size} font-medium text-yellow-800 dark:text-yellow-900 mb-2"}
>
<%= @title %>
</h3>
<div class={"text-#{@size} text-yellow-700 dark:text-yellow-800"}>
<p>
<%= render_slot(@inner_block) %>
</p>

@ -5,26 +5,39 @@ defmodule PlausibleWeb.Components.Site.Feature do
"""
use PlausibleWeb, :view
attr :site, Plausible.Site, required: true
attr :setting, :atom, required: true
attr :label, :string, required: true
attr :conn, Plug.Conn, required: true
slot :inner_block
attr(:site, Plausible.Site, required: true)
attr(:feature_mod, :atom, required: true, values: Plausible.Billing.Feature.list())
attr(:conn, Plug.Conn, required: true)
slot(:inner_block)
def toggle(assigns) do
assigns =
assigns
|> assign(:current_setting, assigns.feature_mod.enabled?(assigns.site))
|> assign(:disabled?, assigns.feature_mod.check_availability(assigns.site) !== :ok)
~H"""
<div>
<div class="mt-4 mb-8 flex items-center">
<%= if Map.fetch!(@site, @setting) do %>
<.button_active to={target(@site, @setting, @conn, false)} />
<% else %>
<.button_inactive to={target(@site, @setting, @conn, true)} />
<% end %>
<span class="ml-2 text-sm font-medium text-gray-900 leading-5 dark:text-gray-100">
<%= @label %>
<.button
set_to={!@current_setting}
disabled?={@disabled?}
conn={@conn}
site={@site}
feature_mod={@feature_mod}
/>
<span class={[
"ml-2 text-sm font-medium leading-5 mb-1",
if(assigns.disabled?,
do: "text-gray-500 dark:text-gray-300",
else: "text-gray-900 dark:text-gray-100"
)
]}>
Show <%= @feature_mod.display_name() %> in the Dashboard
</span>
</div>
<div :if={Map.fetch!(@site, @setting)}>
<div :if={@current_setting}>
<%= render_slot(@inner_block) %>
</div>
</div>
@ -36,21 +49,27 @@ defmodule PlausibleWeb.Components.Site.Feature do
Routes.site_path(conn, :update_feature_visibility, site.domain, setting, r: r, set: set_to)
end
def button_active(assigns) do
~H"""
<%= button(to: @to, method: :put, class: "bg-indigo-600 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring") do %>
<span class="translate-x-5 inline-block h-5 w-5 rounded-full bg-white dark:bg-gray-800 shadow transform transition ease-in-out duration-200">
</span>
<% end %>
"""
end
def button_inactive(assigns) do
defp button(assigns) do
~H"""
<%= button(to: @to, method: :put, class: "bg-gray-200 dark:bg-gray-700 relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none focus:ring") do %>
<span class="translate-x-0 inline-block h-5 w-5 rounded-full bg-white dark:bg-gray-800 shadow transform transition ease-in-out duration-200">
</span>
<% end %>
<.form action={target(@site, @feature_mod.toggle_field(), @conn, @set_to)} method="put" for={nil}>
<button
type="submit"
class={[
"relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full transition-colors ease-in-out duration-200 focus:outline-none focus:ring",
if(assigns.set_to, do: "bg-gray-200 dark:bg-gray-700", else: "bg-indigo-600"),
if(assigns.disabled?, do: "cursor-not-allowed")
]}
disabled={@disabled?}
>
<span
aria-hidden="true"
class={[
"inline-block h-5 w-5 rounded-full bg-white dark:bg-gray-800 shadow transform transition ease-in-out duration-200",
if(assigns.set_to, do: "translate-x-0", else: "translate-x-5")
]}
/>
</button>
</.form>
"""
end
end

@ -34,22 +34,31 @@ defmodule PlausibleWeb.Api.InternalController do
end
end
@features %{
"funnels" => Plausible.Billing.Feature.Funnels,
"props" => Plausible.Billing.Feature.Props,
"conversions" => Plausible.Billing.Feature.Goals
}
def disable_feature(conn, %{"domain" => domain, "feature" => feature}) do
with %User{id: user_id} <- conn.assigns[:current_user],
site <- Sites.get_by_domain(domain),
true <- Sites.has_admin_access?(user_id, site) || Auth.is_super_admin?(user_id) do
property =
case feature do
"funnels" -> :funnels_enabled
"props" -> :props_enabled
"conversions" -> :conversions_enabled
end
change = Plausible.Site.feature_toggle_change(site, property, override: false)
Repo.update!(change)
true <- Sites.has_admin_access?(user_id, site) || Auth.is_super_admin?(user_id),
{:ok, mod} <- Map.fetch(@features, feature),
{:ok, _site} <- mod.toggle(site, override: false) do
json(conn, "ok")
else
{:error, :upgrade_required} ->
PlausibleWeb.Api.Helpers.payment_required(
conn,
"This feature is part of the Plausible Business plan. To get access to this feature, please upgrade your account"
)
:error ->
PlausibleWeb.Api.Helpers.bad_request(
conn,
"The feature you tried to disable is not valid. Valid features are: #{@features |> Map.keys() |> Enum.join(", ")}"
)
_ ->
PlausibleWeb.Api.Helpers.unauthorized(
conn,

@ -121,11 +121,6 @@ defmodule PlausibleWeb.SiteController do
)
end
@feature_titles %{
funnels_enabled: "Funnels",
conversions_enabled: "Goals",
props_enabled: "Properties"
}
def update_feature_visibility(conn, %{
"setting" => setting,
"r" => "/" <> _ = redirect_path,
@ -134,18 +129,18 @@ defmodule PlausibleWeb.SiteController do
when setting in ~w[conversions_enabled funnels_enabled props_enabled] and
value in ["true", "false"] do
site = conn.assigns[:site]
toggle_field = String.to_existing_atom(setting)
setting = String.to_existing_atom(setting)
change = Plausible.Site.feature_toggle_change(site, setting, override: value == "true")
result = Repo.update(change)
feature_mod =
Enum.find(Plausible.Billing.Feature.list(), &(&1.toggle_field() == toggle_field))
case result do
case feature_mod.toggle(site, override: value == "true") do
{:ok, updated_site} ->
message =
if Map.fetch!(updated_site, setting) do
"#{@feature_titles[setting]} are now visible again on your dashboard"
if Map.fetch!(updated_site, toggle_field) do
"#{feature_mod.display_name()} are now visible again on your dashboard"
else
"#{@feature_titles[setting]} are now hidden from your dashboard"
"#{feature_mod.display_name()} are now hidden from your dashboard"
end
conn
@ -156,7 +151,7 @@ defmodule PlausibleWeb.SiteController do
conn
|> put_flash(
:error,
"Something went wrong. Failed to toggle #{@feature_titles[setting]} on your dashboard."
"Something went wrong. Failed to toggle #{feature_mod.display_name()} on your dashboard."
)
|> redirect(to: redirect_path)
end
@ -227,7 +222,9 @@ defmodule PlausibleWeb.SiteController do
end
def settings_funnels(conn, _params) do
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
site = Repo.preload(conn.assigns[:site], [:custom_domain, :owner])
owner = Plausible.Users.with_subscription(site.owner)
site = Map.put(site, :owner, owner)
conn
|> render("settings_funnels.html",
@ -239,7 +236,9 @@ defmodule PlausibleWeb.SiteController do
end
def settings_props(conn, _params) do
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
site = Repo.preload(conn.assigns[:site], [:custom_domain, :owner])
owner = Plausible.Users.with_subscription(site.owner)
site = Map.put(site, :owner, owner)
conn
|> render("settings_props.html",

@ -83,13 +83,8 @@ defmodule PlausibleWeb.StatsController do
)
Sites.locked?(site) ->
owner = Sites.owner_for(site)
render(conn, "site_locked.html",
owner: owner,
site: site,
dogfood_page_path: dogfood_page_path
)
site = Plausible.Repo.preload(site, :owner)
render(conn, "site_locked.html", site: site, dogfood_page_path: dogfood_page_path)
end
end
@ -320,7 +315,7 @@ defmodule PlausibleWeb.StatsController do
)
Sites.locked?(shared_link.site) ->
owner = Sites.owner_for(shared_link.site)
owner = Plausible.Repo.preload(shared_link.site, :owner)
render(conn, "site_locked.html",
owner: owner,

@ -20,7 +20,16 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
) do
form = to_form(Plausible.Goal.changeset(%Plausible.Goal{}))
site = Plausible.Sites.get_for_user!(user_id, domain, [:owner, :admin, :super_admin])
site =
user_id
|> Plausible.Sites.get_for_user!(domain, [:owner, :admin, :super_admin])
|> Repo.preload([:custom_domain, :owner])
owner = Plausible.Users.with_subscription(site.owner)
site = Map.put(site, :owner, owner)
has_access_to_revenue_goals? =
Plausible.Billing.Feature.RevenueGoals.check_availability(site) == :ok
{:ok,
assign(socket,
@ -29,7 +38,8 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
domain: domain,
rendered_by: pid,
tabs: %{custom_events: true, pageviews: false},
site: site
site: site,
has_access_to_revenue_goals?: has_access_to_revenue_goals?
)}
end
@ -54,7 +64,13 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
<.tabs tabs={@tabs} />
<.custom_event_fields :if={@tabs.custom_events} f={f} />
<.custom_event_fields
:if={@tabs.custom_events}
f={f}
current_user={@current_user}
site={@site}
has_access_to_revenue_goals?={@has_access_to_revenue_goals?}
/>
<.pageview_fields :if={@tabs.pageviews} f={f} site={@site} />
<div class="py-4">
@ -99,6 +115,9 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
end
attr(:f, Phoenix.HTML.Form)
attr(:site, Plausible.Site)
attr(:current_user, Plausible.Auth.User)
attr(:has_access_to_revenue_goals?, :boolean)
def custom_event_fields(assigns) do
~H"""
@ -133,44 +152,54 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
})
}
>
<div
class="flex items-center w-max cursor-pointer"
<button
class={[
"flex items-center w-max mb-3",
if(assigns.has_access_to_revenue_goals?,
do: "cursor-pointer",
else: "cursor-not-allowed"
)
]}
aria-labelledby="enable-revenue-tracking"
role="switch"
type="button"
x-on:click="active = !active; currency = ''"
x-bind:aria-checked="active"
disabled={not @has_access_to_revenue_goals?}
>
<button
<span
class="relative inline-flex h-6 w-11 flex-shrink-0 rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-indigo-600 focus:ring-offset-2"
x-bind:class="active ? 'bg-indigo-600' : 'dark:bg-gray-700 bg-gray-200'"
x-bind:aria-checked="active"
aria-labelledby="enable-revenue-tracking"
role="switch"
type="button"
>
<span
aria-hidden="true"
class="pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"
x-bind:class="active ? 'dark:bg-gray-800 translate-x-5' : 'dark:bg-gray-800 translate-x-0'"
/>
</button>
</span>
<span
class="ml-3 font-medium text-gray-900 dark:text-gray-200"
class={[
"ml-3 font-medium",
if(assigns.has_access_to_revenue_goals?,
do: "text-gray-900 dark:text-gray-100",
else: "text-gray-500 dark:text-gray-300"
)
]}
id="enable-revenue-tracking"
>
Enable Revenue Tracking
</span>
</div>
</button>
<div class="rounded-md bg-yellow-50 dark:bg-yellow-900 p-4" x-show="active">
<p class="text-xs text-yellow-700 dark:text-yellow-50 text-justify">
Revenue Tracking is an upcoming premium feature that's free-to-use
during the private preview. Pricing will be announced soon. See
examples and learn more in <a
class="font-medium text-yellow underline hover:text-yellow-600"
target="_blank"
rel="noreferrer"
href="https://plausible.io/docs/ecommerce-revenue-tracking"
>our docs</a>.
</p>
</div>
<span x-show="active">
<PlausibleWeb.Components.Billing.extra_feature_notice
site={@site}
current_user={@current_user}
feature_mod={Plausible.Billing.Feature.RevenueGoals}
size={:xs}
class="rounded-b-md"
/>
</span>
<div x-show="active">
<.live_component
@ -253,7 +282,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do
send(socket.assigns.rendered_by, {:goal_added, Map.put(goal, :funnels, [])})
{:noreply, socket}
{:error, changeset} ->
{:error, %Ecto.Changeset{} = changeset} ->
{:noreply, assign(socket, form: to_form(changeset))}
end
end

@ -1,14 +1,9 @@
<section class="shadow bg-white dark:bg-gray-800 sm:rounded-md sm:overflow-hidden">
<PlausibleWeb.Components.Generic.notice class="rounded-t-md rounded-b-none">
Funnels is an upcoming premium feature that's free-to-use
during the private preview. Pricing will be announced soon. See
examples and learn more in <a
class="font-medium text-yellow underline hover:text-yellow-600"
target="_blank"
rel="noreferrer"
href="https://plausible.io/docs/funnel-analysis"
>our docs</a>.
</PlausibleWeb.Components.Generic.notice>
<PlausibleWeb.Components.Billing.extra_feature_notice
site={@site}
current_user={@current_user}
feature_mod={Plausible.Billing.Feature.Funnels}
/>
<div class="py-6 px-4 sm:p-6">
<header class="relative">
@ -35,9 +30,8 @@
</header>
<PlausibleWeb.Components.Site.Feature.toggle
feature_mod={Plausible.Billing.Feature.Funnels}
site={@site}
setting={:funnels_enabled}
label="Show Funnels in the Dashboard"
conn={@conn}
>
<%= live_render(@conn, PlausibleWeb.Live.FunnelSettings,

@ -29,9 +29,8 @@
</header>
<PlausibleWeb.Components.Site.Feature.toggle
feature_mod={Plausible.Billing.Feature.Goals}
site={@site}
setting={:conversions_enabled}
label="Show Goals in the Dashboard"
conn={@conn}
>
<%= live_render(@conn, PlausibleWeb.Live.GoalSettings,

@ -1,15 +1,10 @@
<section class="shadow bg-white dark:bg-gray-800 sm:rounded-md sm:overflow-hidden">
<PlausibleWeb.Components.Generic.notice class="rounded-t-md rounded-b-none">
Properties is an upcoming premium feature that's free-to-use during the
private preview. Pricing will be announced soon. Existing subscribers will
be grandfathered and will keep having access to custom properties without
any change to their plan. See examples and learn more in <.link
class="font-medium text-yellow underline hover:text-yellow-600"
href="https://plausible.io/docs/custom-props/introduction"
target="_blank"
rel="noreferrer"
>our docs</.link>.
</PlausibleWeb.Components.Generic.notice>
<PlausibleWeb.Components.Billing.extra_feature_notice
site={@site}
current_user={@current_user}
feature_mod={Plausible.Billing.Feature.Props}
grandfathered?
/>
<div class="py-6 px-4 sm:p-6">
<header class="w-full flex">
@ -50,9 +45,8 @@
</header>
<PlausibleWeb.Components.Site.Feature.toggle
feature_mod={Plausible.Billing.Feature.Props}
site={@site}
setting={:props_enabled}
label="Show Properties in the Dashboard"
conn={@conn}
>
<%= live_render(@conn, PlausibleWeb.Live.PropsSettings,

@ -21,12 +21,12 @@
<% role when role in [:admin, :viewer] -> %>
<div class="mt-3 text-gray-600 dark:text-gray-300 text-center">
<p>
This dashboard is currently locked and cannot be accessed. The site owner <b><%= @owner.email %></b> must upgrade their subscription plan in order to
This dashboard is currently locked and cannot be accessed. The site owner <b><%= @site.owner.email %></b> must upgrade their subscription plan in order to
unlock the stats.
</p>
<div class="mt-6 text-sm text-gray-500">
<p>Want to pay for this site with the account you're logged in with?</p>
<p class="mt-1">Contact <%= @owner.email %> and ask them to <%= link("transfer the ownership", class: "text-indigo-500", to: "https://plausible.io/docs/transfer-ownership", rel: "noreferrer") %> of the site over to you</p>
<p class="mt-1">Contact <%= @site.owner.email %> and ask them to <%= link("transfer the ownership", class: "text-indigo-500", to: "https://plausible.io/docs/transfer-ownership", rel: "noreferrer") %> of the site over to you</p>
</div>
</div>
<div class="mt-6 w-full text-center">

@ -37,14 +37,12 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
left_join: se in "setup_help_emails",
on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval")
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: [:owner]
)
for site <- Repo.all(q) do
owner =
Plausible.Sites.owner_for(site)
|> Plausible.Users.with_subscription()
owner = Plausible.Users.with_subscription(site.owner)
setup_completed = Plausible.Sites.has_stats?(site)
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
@ -60,13 +58,12 @@ defmodule Plausible.Workers.SendSiteSetupEmails do
left_join: se in "setup_success_emails",
on: se.site_id == s.id,
where: is_nil(se.id),
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval")
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
preload: :owner
)
for site <- Repo.all(q) do
owner =
Plausible.Sites.owner_for(site)
|> Plausible.Users.with_subscription()
owner = Plausible.Users.with_subscription(site.owner)
if Plausible.Sites.has_stats?(site) do
send_setup_success_email(owner, site)

@ -5,7 +5,8 @@
"monthly_product_id":"558018",
"yearly_product_id":"572810",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -13,7 +14,8 @@
"monthly_product_id":"558745",
"yearly_product_id":"590752",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -21,7 +23,8 @@
"monthly_product_id":"597485",
"yearly_product_id":"597486",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -29,7 +32,8 @@
"monthly_product_id":"597487",
"yearly_product_id":"597488",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -37,7 +41,8 @@
"monthly_product_id":"597642",
"yearly_product_id":"597643",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -45,7 +50,8 @@
"monthly_product_id":"597309",
"yearly_product_id":"597310",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -53,7 +59,8 @@
"monthly_product_id":"597311",
"yearly_product_id":"597312",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -61,7 +68,8 @@
"monthly_product_id":"642352",
"yearly_product_id":"642354",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -69,7 +77,8 @@
"monthly_product_id":"642355",
"yearly_product_id":"642356",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -77,6 +86,7 @@
"monthly_product_id":"650652",
"yearly_product_id":"650653",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
}
]

@ -5,7 +5,8 @@
"monthly_product_id":"654177",
"yearly_product_id":"653232",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -13,7 +14,8 @@
"monthly_product_id":"654178",
"yearly_product_id":"653234",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -21,7 +23,8 @@
"monthly_product_id":"653237",
"yearly_product_id":"653236",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -29,7 +32,8 @@
"monthly_product_id":"653238",
"yearly_product_id":"653239",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -37,7 +41,8 @@
"monthly_product_id":"653240",
"yearly_product_id":"653242",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -45,7 +50,8 @@
"monthly_product_id":"653253",
"yearly_product_id":"653254",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -53,7 +59,8 @@
"monthly_product_id":"653255",
"yearly_product_id":"653256",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -61,7 +68,8 @@
"monthly_product_id":"654181",
"yearly_product_id":"653257",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -69,7 +77,8 @@
"monthly_product_id":"654182",
"yearly_product_id":"653258",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -77,6 +86,7 @@
"monthly_product_id":"654183",
"yearly_product_id":"653259",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
}
]

@ -5,7 +5,8 @@
"monthly_product_id":"749342",
"yearly_product_id":"749343",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -13,7 +14,8 @@
"monthly_product_id":"749344",
"yearly_product_id":"749345",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -21,7 +23,8 @@
"monthly_product_id":"749346",
"yearly_product_id":"749347",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -29,7 +32,8 @@
"monthly_product_id":"749348",
"yearly_product_id":"749349",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -37,7 +41,8 @@
"monthly_product_id":"749350",
"yearly_product_id":"749352",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -45,7 +50,8 @@
"monthly_product_id":"749353",
"yearly_product_id":"749355",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -53,7 +59,8 @@
"monthly_product_id":"749356",
"yearly_product_id":"749357",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
},
{
"kind":"growth",
@ -61,6 +68,7 @@
"monthly_product_id":"749358",
"yearly_product_id":"749359",
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
}
]

@ -5,7 +5,8 @@
"monthly_product_id":"change-me-749342",
"yearly_product_id":"change-me-749343",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -13,7 +14,8 @@
"monthly_product_id":"change-me-749344",
"yearly_product_id":"change-me-749345",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -21,7 +23,8 @@
"monthly_product_id":"change-me-749346",
"yearly_product_id":"change-me-749347",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -29,7 +32,8 @@
"monthly_product_id":"change-me-749348",
"yearly_product_id":"change-me-749349",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -37,7 +41,8 @@
"monthly_product_id":"change-me-749350",
"yearly_product_id":"change-me-749352",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -45,7 +50,8 @@
"monthly_product_id":"change-me-749353",
"yearly_product_id":"change-me-749355",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -53,7 +59,8 @@
"monthly_product_id":"change-me-749356",
"yearly_product_id":"change-me-749357",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"growth",
@ -61,7 +68,8 @@
"monthly_product_id":"change-me-749358",
"yearly_product_id":"change-me-749359",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":[]
},
{
"kind":"business",
@ -69,7 +77,8 @@
"monthly_product_id":"change-me-b749342",
"yearly_product_id":"change-me-b749343",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -77,7 +86,8 @@
"monthly_product_id":"change-me-b749344",
"yearly_product_id":"change-me-b749345",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -85,7 +95,8 @@
"monthly_product_id":"change-me-b749346",
"yearly_product_id":"change-me-b749347",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -93,7 +104,8 @@
"monthly_product_id":"change-me-b749348",
"yearly_product_id":"change-me-b749349",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -101,7 +113,8 @@
"monthly_product_id":"change-me-b749350",
"yearly_product_id":"change-me-b749352",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -109,7 +122,8 @@
"monthly_product_id":"change-me-b749353",
"yearly_product_id":"change-me-b749355",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -117,7 +131,8 @@
"monthly_product_id":"change-me-b749356",
"yearly_product_id":"change-me-b749357",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
},
{
"kind":"business",
@ -125,6 +140,7 @@
"monthly_product_id":"change-me-b749358",
"yearly_product_id":"change-me-b749359",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props","revenue_goals","funnels"]
}
]

@ -5,7 +5,8 @@
"monthly_product_id":"63842",
"yearly_product_id":"63859",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -13,7 +14,8 @@
"monthly_product_id":"63843",
"yearly_product_id":"63860",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -21,7 +23,8 @@
"monthly_product_id":"63844",
"yearly_product_id":"63861",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -29,7 +32,8 @@
"monthly_product_id":"63845",
"yearly_product_id":"63862",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -37,7 +41,8 @@
"monthly_product_id":"63846",
"yearly_product_id":"63863",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -45,7 +50,8 @@
"monthly_product_id":"63847",
"yearly_product_id":"63864",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -53,7 +59,8 @@
"monthly_product_id":"63848",
"yearly_product_id":"63865",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"growth",
@ -61,7 +68,8 @@
"monthly_product_id":"63849",
"yearly_product_id":"63866",
"site_limit":10,
"team_member_limit":5
"team_member_limit":5,
"extra_features":["props"]
},
{
"kind":"business",
@ -69,7 +77,8 @@
"monthly_product_id":"63850",
"yearly_product_id":"63867",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -77,7 +86,8 @@
"monthly_product_id":"63851",
"yearly_product_id":"63868",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -85,7 +95,8 @@
"monthly_product_id":"63852",
"yearly_product_id":"63869",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -93,7 +104,8 @@
"monthly_product_id":"63853",
"yearly_product_id":"63870",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -101,7 +113,8 @@
"monthly_product_id":"63854",
"yearly_product_id":"63871",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -109,7 +122,8 @@
"monthly_product_id":"63855",
"yearly_product_id":"63872",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -117,7 +131,8 @@
"monthly_product_id":"63856",
"yearly_product_id":"63873",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
},
{
"kind":"business",
@ -125,6 +140,7 @@
"monthly_product_id":"63857",
"yearly_product_id":"63874",
"site_limit":50,
"team_member_limit":50
"team_member_limit":50,
"extra_features":["props", "revenue_goals", "funnels"]
}
]

@ -5,6 +5,7 @@
"yearly_product_id":"648089",
"monthly_product_id":null,
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
}
]

@ -5,6 +5,7 @@
"monthly_product_id":"655350",
"yearly_product_id":null,
"site_limit":50,
"team_member_limit":"unlimited"
"team_member_limit":"unlimited",
"extra_features":["props"]
}
]

@ -0,0 +1,125 @@
defmodule Plausible.Billing.FeatureTest do
use Plausible.DataCase
@v1_plan_id "558018"
@v4_growth_plan_id "change-me-749342"
@v4_business_plan_id "change-me-b749342"
for mod <- [Plausible.Billing.Feature.Funnels, Plausible.Billing.Feature.RevenueGoals] do
test "#{mod}.check_availability/1 returns :ok when site owner is on a enterprise plan" do
user =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"),
subscription: build(:subscription, paddle_plan_id: "123321")
)
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
assert :ok == unquote(mod).check_availability(user)
assert :ok == unquote(mod).check_availability(site)
end
test "#{mod}.check_availability/1 returns :ok when site owner is on a business plan" do
user =
insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_business_plan_id))
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
assert :ok == unquote(mod).check_availability(user)
assert :ok == unquote(mod).check_availability(site)
end
test "#{mod}.check_availability/1 returns error when site owner is on a growth plan" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
assert {:error, :upgrade_required} == unquote(mod).check_availability(user)
assert {:error, :upgrade_required} == unquote(mod).check_availability(site)
end
test "#{mod}.check_availability/1 returns error when site owner is on an old plan" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
assert {:error, :upgrade_required} == unquote(mod).check_availability(user)
assert {:error, :upgrade_required} == unquote(mod).check_availability(site)
end
end
test "Plausible.Billing.Feature.Props.check_availability/1 applies grandfathering to old plans" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
assert :ok == Plausible.Billing.Feature.Props.check_availability(user)
assert :ok == Plausible.Billing.Feature.Props.check_availability(site)
end
test "Plausible.Billing.Feature.Goals.check_availability/2 always returns :ok" do
u1 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
u2 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
u3 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_business_plan_id))
u4 =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"),
subscription: build(:subscription, paddle_plan_id: "123321")
)
assert :ok == Plausible.Billing.Feature.Goals.check_availability(u1)
assert :ok == Plausible.Billing.Feature.Goals.check_availability(u2)
assert :ok == Plausible.Billing.Feature.Goals.check_availability(u3)
assert :ok == Plausible.Billing.Feature.Goals.check_availability(u4)
end
for {mod, property} <- [
{Plausible.Billing.Feature.Funnels, :funnels_enabled},
{Plausible.Billing.Feature.Props, :props_enabled}
] do
test "#{mod}.toggle/2 toggles #{property} on and off" do
site = insert(:site, [{:members, [build(:user)]}, {unquote(property), false}])
{:ok, site} = unquote(mod).toggle(site)
assert Map.get(site, unquote(property))
assert unquote(mod).enabled?(site)
{:ok, site} = unquote(mod).toggle(site)
refute Map.get(site, unquote(property))
refute unquote(mod).enabled?(site)
end
test "#{mod}.toggle/2 accepts an override option" do
site = insert(:site, [{:members, [build(:user)]}, {unquote(property), false}])
{:ok, site} = unquote(mod).toggle(site, override: false)
refute Map.get(site, unquote(property))
refute unquote(mod).enabled?(site)
end
test "#{mod}.toggle/2 errors when site owner does not have access to the feature" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, [{:members, [user]}, {unquote(property), false}])
{:error, :upgrade_required} = unquote(mod).toggle(site)
refute unquote(mod).enabled?(site)
end
end
test "Plausible.Billing.Feature.Goals.toggle/2 toggles conversions_enabled on and off" do
site = insert(:site, [{:members, [build(:user)]}, {:conversions_enabled, false}])
{:ok, site} = Plausible.Billing.Feature.Goals.toggle(site)
assert Map.get(site, :conversions_enabled)
assert Plausible.Billing.Feature.Goals.enabled?(site)
{:ok, site} = Plausible.Billing.Feature.Goals.toggle(site)
refute Map.get(site, :conversions_enabled)
refute Plausible.Billing.Feature.Goals.enabled?(site)
end
for mod <- [Plausible.Billing.Feature.Funnels, Plausible.Billing.Feature.Props] do
test "#{mod}.enabled?/1 returns false when user does not have access to the feature even when enabled" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, [{:members, [user]}, {unquote(mod).toggle_field(), true}])
refute unquote(mod).enabled?(site)
end
end
end

@ -363,4 +363,149 @@ defmodule Plausible.Billing.QuotaTest do
assert 50 == Quota.team_member_limit(user_on_business)
end
end
describe "extra_features_usage/1" do
test "returns an empty list" do
user = insert(:user)
assert [] == Quota.extra_features_usage(user)
end
test "returns :props when user uses custom props" do
user = insert(:user)
insert(:site,
allowed_event_props: ["dummy"],
memberships: [build(:site_membership, user: user, role: :owner)]
)
assert [:props] == Quota.extra_features_usage(user)
end
test "returns :funnels when user uses funnels" do
user = insert(:user)
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
goals = insert_list(3, :goal, site: site, event_name: fn -> Ecto.UUID.generate() end)
steps = Enum.map(goals, &%{"goal_id" => &1.id})
Plausible.Funnels.create(site, "dummy", steps)
assert [:funnels] == Quota.extra_features_usage(user)
end
test "returns :revenue_goals when user uses revenue goals" do
user = insert(:user)
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
insert(:goal, currency: :USD, site: site, event_name: "Purchase")
assert [:revenue_goals] == Quota.extra_features_usage(user)
end
test "returns multiple extra features" do
user = insert(:user)
site =
insert(:site,
allowed_event_props: ["dummy"],
memberships: [build(:site_membership, user: user, role: :owner)]
)
insert(:goal, currency: :USD, site: site, event_name: "Purchase")
goals = insert_list(3, :goal, site: site, event_name: fn -> Ecto.UUID.generate() end)
steps = Enum.map(goals, &%{"goal_id" => &1.id})
Plausible.Funnels.create(site, "dummy", steps)
assert [:revenue_goals, :funnels, :props] == Quota.extra_features_usage(user)
end
test "accounts only for sites the user owns" do
user = insert(:user)
insert(:site,
allowed_event_props: ["dummy"],
memberships: [build(:site_membership, user: user, role: :admin)]
)
assert [] == Quota.extra_features_usage(user)
end
end
describe "extra_features_limit/1" do
test "returns props when user is on an old plan" do
user_on_v1 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v1_plan_id))
user_on_v2 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v2_plan_id))
user_on_v3 = insert(:user, subscription: build(:subscription, paddle_plan_id: @v3_plan_id))
assert [:props] == Quota.extra_features_limit(user_on_v1)
assert [:props] == Quota.extra_features_limit(user_on_v2)
assert [:props] == Quota.extra_features_limit(user_on_v3)
end
test "returns an empty list when user is on free_10k plan" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: "free_10k"))
assert [] == Quota.extra_features_limit(user)
end
test "returns all extra features when user is on an enterprise plan" do
user = insert(:user)
enterprise_plan =
insert(:enterprise_plan,
user_id: user.id,
monthly_pageview_limit: 100_000,
site_limit: 500
)
_subscription =
insert(:subscription, user_id: user.id, paddle_plan_id: enterprise_plan.paddle_plan_id)
assert [:props, :revenue_goals, :funnels] == Quota.extra_features_limit(user)
end
test "returns all extra features when user in on trial" do
user = insert(:user, trial_expiry_date: Timex.shift(Timex.now(), days: 7))
assert [:props, :revenue_goals, :funnels] == Quota.extra_features_limit(user)
end
test "returns previous plan limits for enterprise users who have not paid yet" do
user =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"),
subscription: build(:subscription, paddle_plan_id: @v1_plan_id)
)
assert [:props] == Quota.extra_features_limit(user)
end
test "returns trial limits for enterprise users who have not upgraded yet and are on trial" do
user =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"),
subscription: nil
)
assert [:props, :revenue_goals, :funnels] == Quota.extra_features_limit(user)
end
test "returns all extra features for enterprise customers" do
user =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"),
subscription: build(:subscription, paddle_plan_id: "123321")
)
assert [:props, :revenue_goals, :funnels] == Quota.extra_features_limit(user)
end
test "returns all extra features for enterprise customers who are due to change a plan" do
user =
insert(:user,
enterprise_plan: build(:enterprise_plan, paddle_plan_id: "old-paddle-plan-id"),
subscription: build(:subscription, paddle_plan_id: "old-paddle-plan-id")
)
insert(:enterprise_plan, user_id: user.id, paddle_plan_id: "new-paddle-plan-id")
assert [:props, :revenue_goals, :funnels] == Quota.extra_features_limit(user)
end
end
end

@ -1,8 +1,9 @@
defmodule Plausible.GoalsTest do
use Plausible.DataCase
alias Plausible.Goals
@v4_growth_plan_id "change-me-749342"
test "create/2 creates goals and trims input" do
site = insert(:site)
{:ok, goal} = Goals.create(site, %{"page_path" => "/foo bar "})
@ -50,6 +51,7 @@ defmodule Plausible.GoalsTest do
test "create/2 sets site.updated_at for revenue goal" do
site_1 = insert(:site, updated_at: DateTime.add(DateTime.utc_now(), -3600))
{:ok, _goal_1} = Goals.create(site_1, %{"event_name" => "Checkout", "currency" => "BRL"})
assert NaiveDateTime.compare(site_1.updated_at, Plausible.Repo.reload!(site_1).updated_at) ==
@ -70,6 +72,14 @@ defmodule Plausible.GoalsTest do
assert goal.currency == :EUR
end
test "create/2 returns error when site does not have access to revenue goals" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, members: [user])
{:error, :upgrade_required} =
Goals.create(site, %{"event_name" => "Purchase", "currency" => "EUR"})
end
test "create/2 fails for unknown currency code" do
site = insert(:site)

@ -1,6 +1,16 @@
defmodule Plausible.PropsTest do
use Plausible.DataCase
@v4_growth_plan_id "change-me-749342"
test "allow/2 returns error when user plan does not include props" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, members: [user])
assert {:error, :upgrade_required} = Plausible.Props.allow(site, "my-prop-1")
assert %Plausible.Site{allowed_event_props: nil} = Plausible.Repo.reload!(site)
end
test "allow/2 adds props to the array" do
site = insert(:site)
@ -83,6 +93,32 @@ defmodule Plausible.PropsTest do
assert %Plausible.Site{allowed_event_props: ["my-prop-1"]} = Plausible.Repo.reload!(site)
end
test "allow_existing_props/2 returns error when user plan does not include props" do
user = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = insert(:site, members: [user])
populate_stats(site, [
build(:event,
name: "Payment",
"meta.key": ["amount"],
"meta.value": ["500"]
),
build(:event,
name: "Payment",
"meta.key": ["amount", "logged_in"],
"meta.value": ["100", "false"]
),
build(:event,
name: "Payment",
"meta.key": ["amount", "is_customer"],
"meta.value": ["100", "false"]
)
])
assert {:error, :upgrade_required} = Plausible.Props.allow_existing_props(site)
assert %Plausible.Site{allowed_event_props: nil} = Plausible.Repo.reload!(site)
end
test "allow_existing_props/1 saves the most frequent prop keys" do
site = insert(:site)

@ -96,6 +96,7 @@ defmodule Plausible.Site.CacheTest do
# the site was added yesterday so full refresh will pick it up
%{id: site_id} = site = insert(:site, domain: "site1.example.com", updated_at: yesterday)
# the goal was added yesterday so full refresh will pick it up
Plausible.Goals.create(site, %{"event_name" => "Purchase", "currency" => :BRL},
now: yesterday

@ -4,9 +4,7 @@ defmodule Plausible.Site.MembershipTest do
test "raises on trying to insert two owner memberships for the same site" do
user1 = insert(:user)
user2 = insert(:user)
site = insert(:site)
insert(:site_membership, site: site, user: user1, role: "owner")
site = insert(:site, memberships: [build(:site_membership, user: user1, role: "owner")])
assert_raise Ecto.ConstraintError, ~r/site_memberships_site_id_index/, fn ->
insert(:site_membership, site: site, user: user2, role: "owner")

@ -34,9 +34,10 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
end
test "does not degrade role when trying to invite self as an owner" do
site = insert(:site)
user = insert(:user)
membership = insert(:site_membership, user: user, site: site, role: :owner)
%{memberships: [membership]} =
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
invitation =
insert(:invitation,
@ -93,7 +94,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
describe "ownership transfers" do
for {label, opts} <- [{"cloud", []}, {"selfhosted", [selfhost?: true]}] do
test "converts an ownership transfer into a membership on #{label} instance" do
site = insert(:site)
site = insert(:site, memberships: [])
existing_owner = insert(:user)
existing_membership =
@ -136,37 +137,41 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
for role <- [:viewer, :admin] do
test "upgrades existing #{role} membership into an owner" do
site = insert(:site)
owner = insert(:user)
owner_membership = insert(:site_membership, user: owner, site: site, role: :owner)
existing_owner = insert(:user)
new_owner = insert(:user)
new_owner_membership =
insert(:site_membership, user: new_owner, site: site, role: unquote(role))
site =
insert(:site,
memberships: [
build(:site_membership, user: existing_owner, role: :owner),
build(:site_membership, user: new_owner, role: unquote(role))
]
)
invitation =
insert(:invitation,
site_id: site.id,
inviter: owner,
inviter: existing_owner,
email: new_owner.email,
role: :owner
)
assert {:ok, membership} =
assert {:ok, %{id: membership_id}} =
AcceptInvitation.accept_invitation(invitation.invitation_id, new_owner)
assert membership.id == new_owner_membership.id
assert membership.role == :owner
assert %{role: :admin} =
Plausible.Repo.get_by(Plausible.Site.Membership, user_id: existing_owner.id)
assert %{id: ^membership_id, role: :owner} =
Plausible.Repo.get_by(Plausible.Site.Membership, user_id: new_owner.id)
assert Repo.reload!(owner_membership).role == :admin
refute Repo.reload(invitation)
end
end
test "does note degrade or alter trial when accepting ownership transfer by self" do
site = insert(:site)
owner = insert(:user, trial_expiry_date: nil)
owner_membership = insert(:site_membership, user: owner, site: site, role: :owner)
site = insert(:site, memberships: [build(:site_membership, user: owner, role: :owner)])
invitation =
insert(:invitation,
@ -176,21 +181,24 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
role: :owner
)
assert {:ok, membership} =
assert {:ok, %{id: membership_id}} =
AcceptInvitation.accept_invitation(invitation.invitation_id, owner)
assert membership.id == owner_membership.id
assert membership.role == :owner
assert Repo.reload!(owner).trial_expiry_date == nil
assert %{id: ^membership_id, role: :owner} =
Plausible.Repo.get_by(Plausible.Site.Membership, user_id: owner.id)
assert Repo.reload!(owner).trial_expiry_date == nil
refute Repo.reload(invitation)
end
test "locks the site if the new owner has no active subscription or trial" do
site = insert(:site, locked: false)
existing_owner = insert(:user)
insert(:site_membership, user: existing_owner, site: site, role: :owner)
site =
insert(:site,
locked: false,
memberships: [build(:site_membership, user: existing_owner, role: :owner)]
)
new_owner = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
@ -209,10 +217,13 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
end
test "does not lock the site or set trial expiry date if the instance is selfhosted" do
site = insert(:site, locked: false)
existing_owner = insert(:user)
insert(:site_membership, user: existing_owner, site: site, role: :owner)
site =
insert(:site,
locked: false,
memberships: [build(:site_membership, user: existing_owner, role: :owner)]
)
new_owner = insert(:user, trial_expiry_date: nil)
@ -234,10 +245,13 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
end
test "ends trial of the new owner immediately" do
site = insert(:site, locked: false)
existing_owner = insert(:user)
insert(:site_membership, user: existing_owner, site: site, role: :owner)
site =
insert(:site,
locked: false,
memberships: [build(:site_membership, user: existing_owner, role: :owner)]
)
new_owner = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), 7))
@ -257,10 +271,13 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
end
test "sets user's trial expiry date to yesterday if they don't have one" do
site = insert(:site, locked: false)
existing_owner = insert(:user)
insert(:site_membership, user: existing_owner, site: site, role: :owner)
site =
insert(:site,
locked: false,
memberships: [build(:site_membership, user: existing_owner, role: :owner)]
)
new_owner = insert(:user, trial_expiry_date: nil)
@ -280,10 +297,14 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do
end
test "ends grace period and sends an email about it if new owner is past grace period" do
site = insert(:site, locked: false)
existing_owner = insert(:user)
insert(:site_membership, user: existing_owner, site: site, role: :owner)
site =
insert(:site,
locked: false,
memberships: [build(:site_membership, user: existing_owner, role: :owner)]
)
new_owner = insert(:user, trial_expiry_date: Date.add(Date.utc_today(), -1))
insert(:subscription, user: new_owner, next_bill_date: Timex.today())

@ -1,7 +1,6 @@
defmodule Plausible.SitesTest do
use Plausible.DataCase
use Bamboo.Test
alias Plausible.Sites
describe "is_member?" do

@ -0,0 +1,77 @@
defmodule PlausibleWeb.Components.BillingTest do
use Plausible.DataCase
import Phoenix.LiveViewTest
alias PlausibleWeb.Components.Billing
@v4_growth_plan_id "change-me-749342"
@v4_business_plan_id "change-me-b749342"
test "extra_feature_notice/1 renders a message when user is on trial" do
me = insert(:user)
site = :site |> insert(members: [me]) |> Plausible.Repo.preload(:owner)
assert render_component(&Billing.extra_feature_notice/1,
site: site,
current_user: me,
feature_mod: Plausible.Billing.Feature.Props
) =~
"Custom Properties is part of the Plausible Business plan. You can access it during your trial"
end
test "extra_feature_notice/1 renders an upgrade link when user is the site owner and does not have access to the feature" do
me = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site = :site |> insert(members: [me]) |> Plausible.Repo.preload(:owner)
rendered =
render_component(&Billing.extra_feature_notice/1,
site: site,
current_user: me,
feature_mod: Plausible.Billing.Feature.Props
)
assert rendered =~ "Custom Properties is part of the Plausible Business plan."
assert rendered =~ "upgrade your subscription"
assert rendered =~ "/billing/upgrade"
end
test "extra_feature_notice/1 does not render an upgrade link when user is not the site owner" do
me = insert(:user)
owner = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_growth_plan_id))
site =
:site
|> insert(
memberships: [
build(:site_membership, user: owner, role: :owner),
build(:site_membership, user: me, role: :admin)
]
)
|> Plausible.Repo.preload(:owner)
rendered =
render_component(&Billing.extra_feature_notice/1,
site: site,
current_user: me,
feature_mod: Plausible.Billing.Feature.Funnels
)
assert rendered =~
"Funnels is part of the Plausible Business plan. To get access to it, please reach out to the site owner to upgrade your subscription to the Business plan."
refute rendered =~ "/billing/upgrade"
end
test "extra_feature_notice/1 does not render a notice when the user has access to the feature" do
me = insert(:user, subscription: build(:subscription, paddle_plan_id: @v4_business_plan_id))
site = :site |> insert(members: [me]) |> Plausible.Repo.preload(:owner)
rendered =
render_component(&Billing.extra_feature_notice/1,
site: site,
current_user: me,
feature_mod: Plausible.Billing.Feature.Funnels
)
assert rendered == ""
end
end

@ -91,9 +91,7 @@ defmodule PlausibleWeb.Api.InternalControllerTest do
end
test "when the logged-in user is an owner of the site", %{conn: conn, user: user} do
site = insert(:site)
insert(:site_membership, user: user, site: site, role: :owner)
site = insert(:site, memberships: [build(:site_membership, user: user, role: :owner)])
conn = put(conn, "/api/#{site.domain}/disable-feature", %{"feature" => "conversions"})
assert json_response(conn, 200) == "ok"

@ -902,8 +902,7 @@ defmodule PlausibleWeb.AuthControllerTest do
import Ecto.Query
test "can create an API key", %{conn: conn, user: user} do
site = insert(:site)
insert(:site_membership, site: site, user: user, role: "owner")
insert(:site, memberships: [build(:site_membership, user: user, role: "owner")])
conn =
post(conn, "/settings/api-keys", %{
@ -920,8 +919,7 @@ defmodule PlausibleWeb.AuthControllerTest do
end
test "cannot create a duplicate API key", %{conn: conn, user: user} do
site = insert(:site)
insert(:site_membership, site: site, user: user, role: "owner")
insert(:site, memberships: [build(:site_membership, user: user, role: "owner")])
conn =
post(conn, "/settings/api-keys", %{
@ -945,12 +943,12 @@ defmodule PlausibleWeb.AuthControllerTest do
end
test "can't create api key into another site", %{conn: conn, user: me} do
my_site = insert(:site)
insert(:site_membership, site: my_site, user: me, role: "owner")
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: "owner")])
other_user = insert(:user)
other_site = insert(:site)
insert(:site_membership, site: other_site, user: other_user, role: "owner")
_other_site =
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
conn =
post(conn, "/settings/api-keys", %{
@ -973,7 +971,7 @@ defmodule PlausibleWeb.AuthControllerTest do
test "can't delete api key that doesn't belong to me", %{conn: conn} do
other_user = insert(:user)
insert(:site_membership, site: insert(:site), user: other_user, role: "owner")
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
assert {:ok, %ApiKey{} = api_key} =
%ApiKey{user_id: other_user.id}

@ -151,12 +151,12 @@ defmodule PlausibleWeb.Site.InvitationControllerTest do
end
test "fails to remove an invitation from the outside", %{conn: my_conn, user: me} do
my_site = insert(:site)
insert(:site_membership, site: my_site, user: me, role: "owner")
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: "owner")])
other_user = insert(:user)
other_site = insert(:site)
insert(:site_membership, site: other_site, user: other_user, role: "owner")
other_site =
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
invitation =
insert(:invitation,

@ -62,12 +62,12 @@ defmodule PlausibleWeb.Site.MembershipControllerTest do
end
test "fails to create invitation for a foreign site", %{conn: my_conn, user: me} do
my_site = insert(:site)
insert(:site_membership, site: my_site, user: me, role: "owner")
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: "owner")])
other_user = insert(:user)
other_site = insert(:site)
insert(:site_membership, site: other_site, user: other_user, role: "owner")
other_site =
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
my_conn =
post(my_conn, "/sites/#{other_site.domain}/memberships/invite", %{

@ -397,8 +397,9 @@ defmodule PlausibleWeb.SiteControllerTest do
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: :owner)])
other_user = insert(:user)
other_site = insert(:site)
insert(:site_membership, site: other_site, user: other_user, role: "owner")
other_site =
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
my_conn = post(my_conn, "/sites/#{other_site.domain}/make-public")
assert my_conn.status == 404
@ -448,8 +449,10 @@ defmodule PlausibleWeb.SiteControllerTest do
_my_site = insert(:site, memberships: [build(:site_membership, user: me, role: :owner)])
other_user = insert(:user)
other_site = insert(:site)
insert(:site_membership, site: other_site, user: other_user, role: "owner")
other_site =
insert(:site, memberships: [build(:site_membership, user: other_user, role: "owner")])
insert(:google_auth, user: other_user, site: other_site)
insert(:custom_domain, site: other_site)
insert(:spike_notification, site: other_site)
@ -634,14 +637,19 @@ defmodule PlausibleWeb.SiteControllerTest do
for {title, setting} <- %{
"Goals" => :conversions_enabled,
"Funnels" => :funnels_enabled,
"Properties" => :props_enabled
"Custom Properties" => :props_enabled
} do
test "can toggle #{title} with admin access", %{
user: user,
conn: conn0
} do
site = insert(:site)
insert(:site_membership, user: user, site: site, role: :admin)
site =
insert(:site,
memberships: [
build(:site_membership, user: build(:user), role: :owner),
build(:site_membership, user: user, role: :admin)
]
)
conn =
put(

@ -23,14 +23,23 @@ defmodule Plausible.Factory do
}
end
def site_factory do
def site_factory(attrs) do
domain = sequence(:domain, &"example-#{&1}.com")
%Plausible.Site{
defined_memberships? =
Map.has_key?(attrs, :memberships) ||
Map.has_key?(attrs, :members) ||
Map.has_key?(attrs, :owner)
attrs = if defined_memberships?, do: attrs, else: Map.put_new(attrs, :members, [build(:user)])
site = %Plausible.Site{
native_stats_start_at: ~N[2000-01-01 00:00:00],
domain: domain,
timezone: "UTC"
}
merge_attributes(site, attrs)
end
def site_membership_factory do

Loading…
Cancel
Save