diff --git a/config/runtime.exs b/config/runtime.exs index ef71eb492..2148fee56 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -6,7 +6,7 @@ if config_env() in [:dev, :test] do Envy.load(["config/.env.#{config_env()}"]) end -if config_env() == :small do +if config_env() == :small_dev do Envy.load(["config/.env.dev"]) end diff --git a/config/small.exs b/config/small.exs index eaa8a34e6..c98cd2d4a 100644 --- a/config/small.exs +++ b/config/small.exs @@ -1,6 +1,6 @@ import Config -import_config "dev.exs" +import_config "prod.exs" config :esbuild, default: [ diff --git a/config/small_dev.exs b/config/small_dev.exs new file mode 100644 index 000000000..eaa8a34e6 --- /dev/null +++ b/config/small_dev.exs @@ -0,0 +1,11 @@ +import Config + +import_config "dev.exs" + +config :esbuild, + default: [ + args: + ~w(js/app.js js/dashboard.js js/embed.host.js js/embed.content.js --bundle --target=es2017 --loader:.js=jsx --outdir=../priv/static/js --define:BUILD_EXTRA=false), + cd: Path.expand("../assets", __DIR__), + env: %{"NODE_PATH" => Path.expand("../deps", __DIR__)} + ] diff --git a/extra/lib/plausible/stats/sampling.ex b/extra/lib/plausible/stats/sampling.ex new file mode 100644 index 000000000..836ef7ce5 --- /dev/null +++ b/extra/lib/plausible/stats/sampling.ex @@ -0,0 +1,41 @@ +defmodule Plausible.Stats.Sampling do + @moduledoc """ + Sampling related functions + """ + @default_sample_threshold 20_000_000 + + import Ecto.Query + + @spec add_query_hint(Ecto.Query.t(), Plausible.Stats.Query.t()) :: Ecto.Query.t() + def add_query_hint(%Ecto.Query{} = db_query, %Plausible.Stats.Query{} = query) do + case query.sample_threshold do + :infinite -> + db_query + + threshold -> + add_query_hint(db_query, threshold) + end + end + + @spec add_query_hint(Ecto.Query.t(), pos_integer()) :: Ecto.Query.t() + def add_query_hint(%Ecto.Query{} = query, threshold) when is_integer(threshold) do + from(x in query, hints: [sample: threshold]) + end + + @spec add_query_hint(Ecto.Query.t()) :: Ecto.Query.t() + def add_query_hint(%Ecto.Query{} = query) do + add_query_hint(query, @default_sample_threshold) + end + + @spec put_threshold(Plausible.Stats.Query.t(), map()) :: Plausible.Stats.Query.t() + def put_threshold(query, params) do + sample_threshold = + case params["sample_threshold"] do + nil -> @default_sample_threshold + "infinite" -> :infinite + value -> String.to_integer(value) + end + + Map.put(query, :sample_threshold, sample_threshold) + end +end diff --git a/lib/plausible.ex b/lib/plausible.ex index c0a2f01c4..a8fe326fa 100644 --- a/lib/plausible.ex +++ b/lib/plausible.ex @@ -3,7 +3,7 @@ defmodule Plausible do Build-related macros """ - @small_builds [:small, :small_test] + @small_builds [:small, :small_test, :small_dev] defmacro __using__(_) do quote do diff --git a/lib/plausible/auth/auth.ex b/lib/plausible/auth/auth.ex index 4248826c5..41cc124f8 100644 --- a/lib/plausible/auth/auth.ex +++ b/lib/plausible/auth/auth.ex @@ -1,4 +1,5 @@ defmodule Plausible.Auth do + use Plausible use Plausible.Repo alias Plausible.Auth @@ -56,11 +57,15 @@ defmodule Plausible.Auth do ) end - def is_super_admin?(nil), do: false - def is_super_admin?(%Plausible.Auth.User{id: id}), do: is_super_admin?(id) + on_full_build do + def is_super_admin?(nil), do: false + def is_super_admin?(%Plausible.Auth.User{id: id}), do: is_super_admin?(id) - def is_super_admin?(user_id) when is_integer(user_id) do - user_id in Application.get_env(:plausible, :super_admin_user_ids) + def is_super_admin?(user_id) when is_integer(user_id) do + user_id in Application.get_env(:plausible, :super_admin_user_ids) + end + else + def is_super_admin?(_), do: false end def enterprise_configured?(nil), do: false diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex index 6980da2a0..b62c771e4 100644 --- a/lib/plausible/auth/user.ex +++ b/lib/plausible/auth/user.ex @@ -11,6 +11,7 @@ defimpl FunWithFlags.Actor, for: Plausible.Auth.User do end defmodule Plausible.Auth.User do + use Plausible use Ecto.Schema import Ecto.Changeset @@ -209,10 +210,10 @@ defmodule Plausible.Auth.User do end defp trial_expiry() do - if Application.get_env(:plausible, :is_selfhost) do - Timex.today() |> Timex.shift(years: 100) - else + on_full_build do Timex.today() |> Timex.shift(days: 30) + else + Timex.today() |> Timex.shift(years: 100) end end diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex index 0ae80dcf8..ccab13835 100644 --- a/lib/plausible/billing/billing.ex +++ b/lib/plausible/billing/billing.ex @@ -1,4 +1,5 @@ defmodule Plausible.Billing do + use Plausible use Plausible.Repo require Plausible.Billing.Subscription.Status alias Plausible.Billing.{Subscription, Plans, Quota} @@ -116,11 +117,15 @@ defmodule Plausible.Billing do defp subscription_is_active?(%Subscription{}), do: false defp subscription_is_active?(nil), do: false - def on_trial?(%Plausible.Auth.User{trial_expiry_date: nil}), do: false + on_full_build do + def on_trial?(%Plausible.Auth.User{trial_expiry_date: nil}), do: false - def on_trial?(user) do - user = Plausible.Users.with_subscription(user) - !subscription_is_active?(user.subscription) && trial_days_left(user) >= 0 + def on_trial?(user) do + user = Plausible.Users.with_subscription(user) + !subscription_is_active?(user.subscription) && trial_days_left(user) >= 0 + end + else + def on_trial?(_), do: false end def trial_days_left(user) do diff --git a/lib/plausible/billing/quota.ex b/lib/plausible/billing/quota.ex index e747e7284..24b7406a2 100644 --- a/lib/plausible/billing/quota.ex +++ b/lib/plausible/billing/quota.ex @@ -26,44 +26,51 @@ defmodule Plausible.Billing.Quota do end end - @limit_sites_since ~D[2021-05-05] - @spec site_limit(User.t()) :: non_neg_integer() | :unlimited @doc """ Returns the limit of sites a user can have. For enterprise customers, returns :unlimited. The site limit is checked in a background job so as to avoid service disruption. """ - def site_limit(user) do - cond do - Application.get_env(:plausible, :is_selfhost) -> :unlimited - Timex.before?(user.inserted_at, @limit_sites_since) -> :unlimited - true -> get_site_limit_from_plan(user) + on_full_build do + @limit_sites_since ~D[2021-05-05] + @spec site_limit(User.t()) :: non_neg_integer() | :unlimited + def site_limit(user) do + if Timex.before?(user.inserted_at, @limit_sites_since) do + :unlimited + else + get_site_limit_from_plan(user) + end end - end - @site_limit_for_trials 10 - @site_limit_for_legacy_trials 50 - @site_limit_for_free_10k 50 - defp get_site_limit_from_plan(user) do - user = Plausible.Users.with_subscription(user) + @site_limit_for_trials 10 + @site_limit_for_legacy_trials 50 + @site_limit_for_free_10k 50 + defp get_site_limit_from_plan(user) do + user = Plausible.Users.with_subscription(user) - case Plans.get_subscription_plan(user.subscription) do - %EnterprisePlan{} -> - :unlimited + case Plans.get_subscription_plan(user.subscription) do + %EnterprisePlan{} -> + :unlimited - %Plan{site_limit: site_limit} -> - site_limit + %Plan{site_limit: site_limit} -> + site_limit - :free_10k -> - @site_limit_for_free_10k + :free_10k -> + @site_limit_for_free_10k - nil -> - if Timex.before?(user.inserted_at, Plans.business_tier_launch()) do - @site_limit_for_legacy_trials - else - @site_limit_for_trials - end + nil -> + if Timex.before?(user.inserted_at, Plans.business_tier_launch()) do + @site_limit_for_legacy_trials + else + @site_limit_for_trials + end + end + end + else + @spec site_limit(any()) :: non_neg_integer() | :unlimited + def site_limit(_) do + :unlimited end end @@ -203,13 +210,7 @@ defmodule Plausible.Billing.Quota do stats_api_usage = from a in Plausible.Auth.ApiKey, where: a.user_id == ^user.id queries = - if Plausible.Release.selfhost?() do - [ - {Props, props_usage_query}, - {RevenueGoals, revenue_goals_usage}, - {StatsAPI, stats_api_usage} - ] - else + on_full_build do funnels_usage_query = from f in "funnels", inner_join: os in subquery(owned_sites_query(user)), @@ -221,6 +222,12 @@ defmodule Plausible.Billing.Quota do {RevenueGoals, revenue_goals_usage}, {StatsAPI, stats_api_usage} ] + else + [ + {Props, props_usage_query}, + {RevenueGoals, revenue_goals_usage}, + {StatsAPI, stats_api_usage} + ] end Enum.reduce(queries, [], fn {feature, query}, acc -> diff --git a/lib/plausible/plugins/api/token.ex b/lib/plausible/plugins/api/token.ex index 4330da0d9..4f64ccbe8 100644 --- a/lib/plausible/plugins/api/token.ex +++ b/lib/plausible/plugins/api/token.ex @@ -10,6 +10,7 @@ defmodule Plausible.Plugins.API.Token do It is prefixed with a plain text identifier allowing source scanning for leaked secrets. """ + use Plausible use Ecto.Schema import Ecto.Changeset @@ -63,10 +64,15 @@ defmodule Plausible.Plugins.API.Token do can scan repositories for accidental secret commits. """ def prefix() do - case {Plausible.Release.selfhost?(), Application.get_env(:plausible, :environment)} do - {true, _} -> "plausible-plugin-selfhost" - {false, "prod"} -> "plausible-plugin" - {false, env} -> "plausible-plugin-#{env}" + on_full_build do + env = Application.get_env(:plausible, :environment) + + case env do + "prod" -> "plausible-plugin" + env -> "plausible-plugin-#{env}" + end + else + "plausible-plugin-selfhost" end end diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex index f18fc5b03..35b9dc2e5 100644 --- a/lib/plausible/site.ex +++ b/lib/plausible/site.ex @@ -39,7 +39,6 @@ defmodule Plausible.Site do has_one :google_auth, GoogleAuth has_one :weekly_report, Plausible.Site.WeeklyReport 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] @@ -49,7 +48,7 @@ defmodule Plausible.Site do # strictly necessary. field :from_cache?, :boolean, virtual: true, default: false - # Used in the context of paginated sites list to order in relation to + # Used in the context of paginated sites list to order in relation to # user's membership state. Currently it can be either "invitation", # "pinned_site" or "site", where invitations are first. field :entry_type, :string, virtual: true diff --git a/lib/plausible/site/custom_domain.ex b/lib/plausible/site/custom_domain.ex deleted file mode 100644 index 325b72bb0..000000000 --- a/lib/plausible/site/custom_domain.ex +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Plausible.Site.CustomDomain do - use Ecto.Schema - - schema "custom_domains" do - field :domain, :string - field :has_ssl_certificate, :boolean - belongs_to :site, Plausible.Site - - timestamps() - end -end diff --git a/lib/plausible/site/memberships/accept_invitation.ex b/lib/plausible/site/memberships/accept_invitation.ex index 4ba1720a1..a96b16c97 100644 --- a/lib/plausible/site/memberships/accept_invitation.ex +++ b/lib/plausible/site/memberships/accept_invitation.ex @@ -50,7 +50,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitation do @spec accept_invitation(String.t(), Auth.User.t(), Keyword.t()) :: {:ok, Site.Membership.t()} | {:error, :invitation_not_found | Ecto.Changeset.t()} def accept_invitation(invitation_id, user, opts \\ []) do - selfhost? = Keyword.get(opts, :selfhost?, Plausible.Release.selfhost?()) + selfhost? = Keyword.get(opts, :selfhost?, small_build?()) with {:ok, invitation} <- Invitations.find_for_user(invitation_id, user) do membership = get_or_create_membership(invitation, user) diff --git a/lib/plausible/stats/base.ex b/lib/plausible/stats/base.ex index ecdb922ff..684d2676f 100644 --- a/lib/plausible/stats/base.ex +++ b/lib/plausible/stats/base.ex @@ -4,8 +4,6 @@ defmodule Plausible.Stats.Base do alias Plausible.Stats.{Query, Filters} import Ecto.Query - # Ecto typespec has not been updated for this PR: https://github.com/elixir-ecto/ecto/pull/3592 - @dialyzer {:nowarn_function, add_sample_hint: 2} @no_ref "Direct / None" @not_set "(not set)" @@ -40,7 +38,10 @@ defmodule Plausible.Stats.Base do where: e.site_id == ^site.id, where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime ) - |> add_sample_hint(query) + + on_full_build do + q = Plausible.Stats.Sampling.add_query_hint(q, query) + end q = from(e in q, where: ^dynamic_filter_condition(query, "event:page", :pathname)) @@ -211,8 +212,12 @@ defmodule Plausible.Stats.Base do where: s.site_id == ^site.id, where: s.start >= ^first_datetime and s.start < ^last_datetime ) - |> add_sample_hint(query) - |> filter_by_entry_props(query) + + on_full_build do + sessions_q = Plausible.Stats.Sampling.add_query_hint(sessions_q, query) + end + + sessions_q = filter_by_entry_props(sessions_q, query) Enum.reduce(Filters.visit_props(), sessions_q, fn prop_name, sessions_q -> filter_key = "visit:" <> prop_name @@ -549,16 +554,6 @@ defmodule Plausible.Stats.Base do end) end - defp add_sample_hint(db_q, query) do - case query.sample_threshold do - :infinite -> - db_q - - threshold -> - from(e in db_q, hints: [sample: threshold]) - end - end - defp split_goals(clauses, map_fn \\ &Function.identity/1) do groups = Enum.group_by(clauses, fn {goal_type, _v} -> goal_type end, fn {_k, val} -> map_fn.(val) end) diff --git a/lib/plausible/stats/clickhouse.ex b/lib/plausible/stats/clickhouse.ex index adefd1822..909f71f56 100644 --- a/lib/plausible/stats/clickhouse.ex +++ b/lib/plausible/stats/clickhouse.ex @@ -1,4 +1,5 @@ defmodule Plausible.Stats.Clickhouse do + use Plausible use Plausible.Repo use Plausible.ClickhouseRepo use Plausible.Stats.Fragments @@ -236,7 +237,6 @@ defmodule Plausible.Stats.Clickhouse do current_q = from( e in "events_v2", - hints: [sample: 20_000_000], join: total_q in subquery(total_q), on: e.site_id == total_q.site_id, where: e.site_id in ^Map.keys(site_id_to_domain_mapping), @@ -252,6 +252,10 @@ defmodule Plausible.Stats.Clickhouse do order_by: [e.site_id, fragment("toStartOfHour(timestamp)")] ) + on_full_build do + current_q = Plausible.Stats.Sampling.add_query_hint(current_q) + end + result = current_q |> ClickhouseRepo.all() @@ -275,17 +279,22 @@ defmodule Plausible.Stats.Clickhouse do end defp visitors_24h_total(now, offset1, offset2, site_id_to_domain_mapping) do - from(e in "events_v2", - hints: [sample: 20_000_000], - where: e.site_id in ^Map.keys(site_id_to_domain_mapping), - where: e.timestamp >= ^NaiveDateTime.add(now, offset1, :hour), - where: e.timestamp <= ^NaiveDateTime.add(now, offset2, :hour), - select: %{ - site_id: e.site_id, - total_visitors: fragment("toUInt64(round(uniq(user_id) * any(_sample_factor)))") - }, - group_by: [e.site_id] - ) + query = + from e in "events_v2", + where: e.site_id in ^Map.keys(site_id_to_domain_mapping), + where: e.timestamp >= ^NaiveDateTime.add(now, offset1, :hour), + where: e.timestamp <= ^NaiveDateTime.add(now, offset2, :hour), + select: %{ + site_id: e.site_id, + total_visitors: fragment("toUInt64(round(uniq(user_id) * any(_sample_factor)))") + }, + group_by: [e.site_id] + + on_full_build do + query = Plausible.Stats.Sampling.add_query_hint(query) + end + + query end defp empty_24h_intervals(now) do @@ -306,11 +315,14 @@ defmodule Plausible.Stats.Clickhouse do q = from(s in "sessions_v2", - hints: ["SAMPLE 10000000"], where: s.site_id == ^site.id, where: s.timestamp >= ^first_datetime and s.start < ^last_datetime ) + on_full_build do + q = Plausible.Stats.Sampling.add_query_hint(q, 10_000_000) + end + q = if query.filters["source"] do source = query.filters["source"] @@ -425,11 +437,14 @@ defmodule Plausible.Stats.Clickhouse do q = from(e in "events_v2", - hints: ["SAMPLE 10000000"], where: e.site_id == ^site.id, where: e.timestamp >= ^first_datetime and e.timestamp < ^last_datetime ) + on_full_build do + q = Plausible.Stats.Sampling.add_query_hint(q, 10_000_000) + end + q = if query.filters["screen"] do size = query.filters["screen"] diff --git a/lib/plausible/stats/query.ex b/lib/plausible/stats/query.ex index 6c52f79e9..d00b27eaa 100644 --- a/lib/plausible/stats/query.ex +++ b/lib/plausible/stats/query.ex @@ -1,4 +1,6 @@ defmodule Plausible.Stats.Query do + use Plausible + defstruct date_range: nil, interval: nil, period: nil, @@ -7,19 +9,24 @@ defmodule Plausible.Stats.Query do imported_data_requested: false, include_imported: false - @default_sample_threshold 20_000_000 require OpenTelemetry.Tracer, as: Tracer alias Plausible.Stats.{FilterParser, Interval} @type t :: %__MODULE__{} def from(site, params) do - query_by_period(site, params) - |> put_interval(params) - |> put_parsed_filters(params) - |> put_imported_opts(site, params) - |> put_sample_threshold(params) - |> maybe_drop_prop_filter(site) + query = + query_by_period(site, params) + |> put_interval(params) + |> put_parsed_filters(params) + |> put_imported_opts(site, params) + |> maybe_drop_prop_filter(site) + + on_full_build do + query = Plausible.Stats.Sampling.put_threshold(query, params) + end + + query end defp query_by_period(site, %{"period" => "realtime"}) do @@ -163,17 +170,6 @@ defmodule Plausible.Stats.Query do Map.put(query, :filters, FilterParser.parse_filters(params["filters"])) end - defp put_sample_threshold(query, params) do - sample_threshold = - case params["sample_threshold"] do - nil -> @default_sample_threshold - "infinite" -> :infinite - value -> String.to_integer(value) - end - - Map.put(query, :sample_threshold, sample_threshold) - end - def put_filter(query, key, val) do %__MODULE__{ query diff --git a/lib/plausible_release.ex b/lib/plausible_release.ex index 766873ff2..057e3bc53 100644 --- a/lib/plausible_release.ex +++ b/lib/plausible_release.ex @@ -1,4 +1,5 @@ defmodule Plausible.Release do + use Plausible use Plausible.Repo require Logger @@ -10,13 +11,12 @@ defmodule Plausible.Release do :ecto ] - @spec selfhost? :: boolean - def selfhost? do - Application.fetch_env!(@app, :is_selfhost) - end - def should_be_first_launch? do - selfhost?() and not (_has_users? = Repo.exists?(Plausible.Auth.User)) + on_full_build do + false + else + not (_has_users? = Repo.exists?(Plausible.Auth.User)) + end end def migrate do diff --git a/lib/plausible_web/controllers/auth_controller.ex b/lib/plausible_web/controllers/auth_controller.ex index a54b34851..d30bff50d 100644 --- a/lib/plausible_web/controllers/auth_controller.ex +++ b/lib/plausible_web/controllers/auth_controller.ex @@ -31,12 +31,6 @@ defmodule PlausibleWeb.AuthController do ] ) - plug(:assign_is_selfhost) - - defp assign_is_selfhost(conn, _opts) do - assign(conn, :is_selfhost, Plausible.Release.selfhost?()) - end - def register(conn, %{"user" => %{"email" => email, "password" => password}}) do with {:ok, user} <- login_user(conn, email, password) do conn = set_user_session(conn, user) diff --git a/lib/plausible_web/controllers/site_controller.ex b/lib/plausible_web/controllers/site_controller.ex index c4a493c01..0b9949939 100644 --- a/lib/plausible_web/controllers/site_controller.ex +++ b/lib/plausible_web/controllers/site_controller.ex @@ -61,7 +61,7 @@ defmodule PlausibleWeb.SiteController do def add_snippet(conn, _params) do user = conn.assigns[:current_user] - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] is_first_site = !Repo.exists?( @@ -121,9 +121,7 @@ defmodule PlausibleWeb.SiteController do end def settings_general(conn, _params) do - site = - conn.assigns[:site] - |> Repo.preload([:custom_domain]) + site = conn.assigns[:site] conn |> render("settings_general.html", @@ -137,7 +135,7 @@ defmodule PlausibleWeb.SiteController do def settings_people(conn, _params) do site = conn.assigns[:site] - |> Repo.preload(memberships: :user, invitations: [], custom_domain: []) + |> Repo.preload(memberships: :user, invitations: []) conn |> render("settings_people.html", @@ -148,7 +146,7 @@ defmodule PlausibleWeb.SiteController do end def settings_visibility(conn, _params) do - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id) conn @@ -161,7 +159,7 @@ defmodule PlausibleWeb.SiteController do end def settings_goals(conn, _params) do - site = Repo.preload(conn.assigns[:site], [:custom_domain, :owner]) + site = Repo.preload(conn.assigns[:site], [:owner]) owner = Plausible.Users.with_subscription(site.owner) site = Map.put(site, :owner, owner) @@ -175,7 +173,7 @@ defmodule PlausibleWeb.SiteController do end def settings_funnels(conn, _params) do - site = Repo.preload(conn.assigns[:site], [:custom_domain, :owner]) + site = Repo.preload(conn.assigns[:site], [:owner]) owner = Plausible.Users.with_subscription(site.owner) site = Map.put(site, :owner, owner) @@ -189,7 +187,7 @@ defmodule PlausibleWeb.SiteController do end def settings_props(conn, _params) do - site = Repo.preload(conn.assigns[:site], [:custom_domain, :owner]) + site = Repo.preload(conn.assigns[:site], [:owner]) owner = Plausible.Users.with_subscription(site.owner) site = Map.put(site, :owner, owner) @@ -203,7 +201,7 @@ defmodule PlausibleWeb.SiteController do end def settings_email_reports(conn, _params) do - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] conn |> render("settings_email_reports.html", @@ -216,21 +214,8 @@ defmodule PlausibleWeb.SiteController do ) end - def settings_custom_domain(conn, _params) do - site = - conn.assigns[:site] - |> Repo.preload(:custom_domain) - - conn - |> assign(:skip_plausible_tracking, true) - |> render("settings_custom_domain.html", - site: site, - layout: {PlausibleWeb.LayoutView, "site_settings.html"} - ) - end - def settings_danger_zone(conn, _params) do - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] conn |> render("settings_danger_zone.html", @@ -243,7 +228,7 @@ defmodule PlausibleWeb.SiteController do def settings_integrations(conn, _params) do site = conn.assigns.site - |> Repo.preload([:google_auth, :custom_domain]) + |> Repo.preload([:google_auth]) search_console_domains = if site.google_auth do @@ -295,7 +280,7 @@ defmodule PlausibleWeb.SiteController do end def update_settings(conn, %{"site" => site_params}) do - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] changeset = Plausible.Site.update_changeset(site, site_params) case Repo.update(changeset) do @@ -624,27 +609,6 @@ defmodule PlausibleWeb.SiteController do end end - def delete_custom_domain(conn, %{"id" => domain_id}) do - site = conn.assigns[:site] - site_id = site.id - - case Repo.delete_all( - from d in Plausible.Site.CustomDomain, - where: d.site_id == ^site_id, - where: d.id == ^domain_id - ) do - {1, _} -> - conn - |> put_flash(:success, "Custom domain deleted successfully") - |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/general") - - {0, _} -> - conn - |> put_flash(:error, "Failed to delete custom domain") - |> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/general") - end - end - def import_from_google_user_metric_notice(conn, %{ "view_id" => view_id, "access_token" => access_token, @@ -874,7 +838,7 @@ defmodule PlausibleWeb.SiteController do end def add_snippet_after_domain_change(conn, _params) do - site = conn.assigns[:site] |> Repo.preload(:custom_domain) + site = conn.assigns[:site] conn |> assign(:skip_plausible_tracking, true) diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex index 149ca599d..ff149aa77 100644 --- a/lib/plausible_web/controllers/stats_controller.ex +++ b/lib/plausible_web/controllers/stats_controller.ex @@ -363,14 +363,13 @@ defmodule PlausibleWeb.StatsController do end defp is_dbip() do - is_or_nil = - if Application.get_env(:plausible, :is_selfhost) do - if type = Plausible.Geo.database_type() do - String.starts_with?(type, "DBIP") - end - end - - !!is_or_nil + on_full_build do + false + else + Plausible.Geo.database_type() + |> to_string() + |> String.starts_with?("DBIP") + end end defp title(%{path_info: ["plausible.io"]}, _) do diff --git a/lib/plausible_web/live/goal_settings/form.ex b/lib/plausible_web/live/goal_settings/form.ex index 1a5b28233..4da4681cf 100644 --- a/lib/plausible_web/live/goal_settings/form.ex +++ b/lib/plausible_web/live/goal_settings/form.ex @@ -24,7 +24,7 @@ defmodule PlausibleWeb.Live.GoalSettings.Form do site = user_id |> Plausible.Sites.get_for_user!(domain, [:owner, :admin, :super_admin]) - |> Repo.preload([:custom_domain, :owner]) + |> Repo.preload([:owner]) owner = Plausible.Users.with_subscription(site.owner) site = Map.put(site, :owner, owner) diff --git a/lib/plausible_web/live/register_form.ex b/lib/plausible_web/live/register_form.ex index 143ca30de..258c6b52a 100644 --- a/lib/plausible_web/live/register_form.ex +++ b/lib/plausible_web/live/register_form.ex @@ -5,6 +5,7 @@ defmodule PlausibleWeb.Live.RegisterForm do use Phoenix.LiveView use Phoenix.HTML + use Plausible import PlausibleWeb.Live.Components.Form @@ -37,7 +38,6 @@ defmodule PlausibleWeb.Live.RegisterForm do form: to_form(changeset), captcha_error: nil, password_strength: Auth.User.password_strength(changeset), - is_selfhost: Plausible.Release.selfhost?(), trigger_submit: false )} end @@ -68,7 +68,7 @@ defmodule PlausibleWeb.Live.RegisterForm do ~H"""

- <%= if @is_selfhost or @live_action == :register_from_invitation_form do %> + <%= if small_build?() or @live_action == :register_from_invitation_form do %> Register your Plausible Analytics account <% else %> Register your 30-day free trial @@ -162,7 +162,7 @@ defmodule PlausibleWeb.Live.RegisterForm do <% end %> <% submit_text = - if @is_selfhost or @invitation do + if small_build?() or @invitation do "Create my account →" else "Start my free trial →" diff --git a/lib/plausible_web/plugs/first_launch_plug.ex b/lib/plausible_web/plugs/first_launch_plug.ex index 6588e338b..8bf81056a 100644 --- a/lib/plausible_web/plugs/first_launch_plug.ex +++ b/lib/plausible_web/plugs/first_launch_plug.ex @@ -3,6 +3,17 @@ defmodule PlausibleWeb.FirstLaunchPlug do Redirects first-launch users to registration page. """ + defmodule Test do + @moduledoc """ + Test helper for setup blocks allowing to skip the plug processing + """ + @spec skip(map()) :: {:ok, map()} + def skip(context) do + conn = Plug.Conn.put_private(context.conn, PlausibleWeb.FirstLaunchPlug, :skip) + {:ok, Map.put(context, :conn, conn)} + end + end + @behaviour Plug alias Plausible.Release @@ -12,6 +23,7 @@ defmodule PlausibleWeb.FirstLaunchPlug do end @impl true + def call(%Plug.Conn{private: %{__MODULE__ => :skip}} = conn, _), do: conn def call(%Plug.Conn{request_path: path} = conn, path), do: conn def call(conn, redirect_to) do diff --git a/lib/plausible_web/plugs/maybe_disable_registration.ex b/lib/plausible_web/plugs/maybe_disable_registration.ex index 5cfdc7afc..7c05a7b91 100644 --- a/lib/plausible_web/plugs/maybe_disable_registration.ex +++ b/lib/plausible_web/plugs/maybe_disable_registration.ex @@ -20,18 +20,13 @@ defmodule PlausibleWeb.Plugs.MaybeDisableRegistration do disable_registration = Keyword.fetch!(selfhost_config, :disable_registration) first_launch? = Release.should_be_first_launch?() - cond do - first_launch? -> - conn - - disable_registration in disabled_for -> - conn - |> put_flash(:error, "Registration is disabled on this instance") - |> redirect(to: Routes.auth_path(conn, :login_form)) - |> halt() - - true -> - conn + if not first_launch? and disable_registration in disabled_for do + conn + |> put_flash(:error, "Registration is disabled on this instance") + |> redirect(to: Routes.auth_path(conn, :login_form)) + |> halt() + else + conn end end end diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index d8bfc57e1..b6d1e00c9 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -10,7 +10,7 @@ defmodule PlausibleWeb.Router do plug :fetch_live_flash plug :put_secure_browser_headers plug PlausibleWeb.Plugs.NoRobots - plug PlausibleWeb.FirstLaunchPlug, redirect_to: "/register" + on_full_build(do: nil, else: plug(PlausibleWeb.FirstLaunchPlug, redirect_to: "/register")) plug PlausibleWeb.SessionTimeoutPlug, timeout_after_seconds: @two_weeks_in_seconds plug PlausibleWeb.AuthPlug plug PlausibleWeb.LastSeenPlug @@ -278,8 +278,6 @@ defmodule PlausibleWeb.Router do put "/sites/:website/shared-links/:slug", SiteController, :update_shared_link delete "/sites/:website/shared-links/:slug", SiteController, :delete_shared_link - delete "/sites/:website/custom-domains/:id", SiteController, :delete_custom_domain - get "/sites/:website/memberships/invite", Site.MembershipController, :invite_member_form post "/sites/:website/memberships/invite", Site.MembershipController, :invite_member @@ -311,7 +309,6 @@ defmodule PlausibleWeb.Router do end get "/:website/settings/email-reports", SiteController, :settings_email_reports - get "/:website/settings/custom-domain", SiteController, :settings_custom_domain get "/:website/settings/danger-zone", SiteController, :settings_danger_zone get "/:website/settings/integrations", SiteController, :settings_integrations diff --git a/lib/plausible_web/templates/auth/activate.html.eex b/lib/plausible_web/templates/auth/activate.html.eex deleted file mode 100644 index 789c97d94..000000000 --- a/lib/plausible_web/templates/auth/activate.html.eex +++ /dev/null @@ -1,71 +0,0 @@ -
- <%= if @has_email_code? do %> - <%= form_for @conn, "/activate", [class: "w-full max-w-lg mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8"], fn f -> %> -

- <%= if @has_any_memberships? do %> - Verify your email address - <% else %> - Activate your account - <% end %> -

- -
- Please enter the 4-digit code we sent to <%= @conn.assigns[:current_user].email %> -
- -
-
- <%= text_input f, :code, class: "tracking-widest font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-36 px-8 border-gray-300 dark:border-gray-500 rounded-l-md dark:text-gray-200 dark:bg-gray-900", oninput: "this.value=this.value.replace(/[^0-9]/g, ''); if (this.value.length >= 4) document.getElementById('submit').focus()", onclick: "this.select();", maxlength: "4", placeholder: "••••", style: "letter-spacing: 10px;", required: "required" %> -
- -
- <%= error_tag(assigns, :error) %> - -
- Didn't receive an email? -
-
    -
  1. Check your spam folder
  2. -
  3. <%= link("Send a new code", class: "underline text-indigo-600", to: "/activate/request-code", method: :post) %> to <%= @conn.assigns[:current_user].email %>
  4. - <%= if Application.get_env(:plausible, :is_selfhost) do %> -
  5. Ask on our <%= link("community-supported forum", to: "https://github.com/plausible/analytics/discussions", class: "text-indigo-600 underline" ) %>
  6. - <% else %> -
  7. Contact us if the problem persists
  8. - <% end %> -
-
- Entered the wrong email address? -
-
    - <%= if @has_any_memberships? do %> -
  • - <%= link("Change email back to", class: "underline text-indigo-600", to: "/settings/email/cancel", method: "post") %> to <%= @conn.assigns[:current_user].previous_email %> -
  • - <% else %> -
  • - <%= link("Delete this account", class: "underline text-indigo-600", to: "/me?redirect=/register", method: "delete", data: [confirm: "Deleting your account cannot be reversed. Are you sure?"]) %> and start over -
  • - <% end %> -
- <% end %> - <% else %> -
-

Activate your account

- -
- A 4-digit activation code will be sent to <%= @conn.assigns[:current_user].email %> -
- - <%= error_tag(assigns, :error) %> - - <%= button("Request activation code", to: "/activate/request-code", method: :post, class: "button mt-12") %> - -
- <% end %> - - <%= if !@has_any_invitations? and !@has_any_memberships? do %> - - <% end %> -
diff --git a/lib/plausible_web/templates/auth/activate.html.heex b/lib/plausible_web/templates/auth/activate.html.heex new file mode 100644 index 000000000..93e940525 --- /dev/null +++ b/lib/plausible_web/templates/auth/activate.html.heex @@ -0,0 +1,109 @@ +
+ <%= if @has_email_code? do %> + <%= form_for @conn, "/activate", [class: "w-full max-w-lg mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 py-6 mb-4 mt-8"], fn f -> %> +

+ <%= if @has_any_memberships? do %> + Verify your email address + <% else %> + Activate your account + <% end %> +

+ +
+ Please enter the 4-digit code we sent to <%= @conn.assigns[:current_user].email %> +
+ +
+
+ <%= text_input(f, :code, + class: + "tracking-widest font-medium shadow-sm focus:ring-indigo-500 focus:border-indigo-500 block w-36 px-8 border-gray-300 dark:border-gray-500 rounded-l-md dark:text-gray-200 dark:bg-gray-900", + oninput: + "this.value=this.value.replace(/[^0-9]/g, ''); if (this.value.length >= 4) document.getElementById('submit').focus()", + onclick: "this.select();", + maxlength: "4", + placeholder: "••••", + style: "letter-spacing: 10px;", + required: "required" + ) %> +
+ +
+ <%= error_tag(assigns, :error) %> + +
+ Didn't receive an email? +
+
    +
  1. Check your spam folder
  2. +
  3. + <%= link("Send a new code", + class: "underline text-indigo-600", + to: "/activate/request-code", + method: :post + ) %> to <%= @conn.assigns[:current_user].email %> +
  4. + <%= if full_build?() do %> +
  5. + + Contact us + + if the problem persists +
  6. + <% else %> +
  7. + Ask on our <%= link("community-supported forum", + to: "https://github.com/plausible/analytics/discussions", + class: "text-indigo-600 underline" + ) %> +
  8. + <% end %> +
+
+ Entered the wrong email address? +
+
    + <%= if @has_any_memberships? do %> +
  • + <%= link("Change email back to", + class: "underline text-indigo-600", + to: "/settings/email/cancel", + method: "post" + ) %> to <%= @conn.assigns[:current_user].previous_email %> +
  • + <% else %> +
  • + <%= link("Delete this account", + class: "underline text-indigo-600", + to: "/me?redirect=/register", + method: "delete", + data: [confirm: "Deleting your account cannot be reversed. Are you sure?"] + ) %> and start over +
  • + <% end %> +
+ <% end %> + <% else %> +
+

Activate your account

+ +
+ A 4-digit activation code will be sent to <%= @conn.assigns[:current_user].email %> +
+ + <%= error_tag(assigns, :error) %> + + <%= button("Request activation code", + to: "/activate/request-code", + method: :post, + class: "button mt-12" + ) %> +
+ <% end %> + + <%= if !@has_any_invitations? and !@has_any_memberships? do %> + + <% end %> +
diff --git a/lib/plausible_web/templates/auth/password_reset_request_success.html.eex b/lib/plausible_web/templates/auth/password_reset_request_success.html.heex similarity index 64% rename from lib/plausible_web/templates/auth/password_reset_request_success.html.eex rename to lib/plausible_web/templates/auth/password_reset_request_success.html.heex index a3bde0d4f..846a7c03e 100644 --- a/lib/plausible_web/templates/auth/password_reset_request_success.html.eex +++ b/lib/plausible_web/templates/auth/password_reset_request_success.html.heex @@ -1,20 +1,27 @@

Success!

- We've sent an email containing password reset instructions to <%= @email %> if it's registered in our system. + We've sent an email containing password reset instructions to <%= @email %> + if it's registered in our system.
Didn't receive the email within a few minutes?
You might have used an email address that's not registered in our database. Please verify the email address associated with your Plausible account and attempt the password reset once more. -
-
- <%= if Application.get_env(:plausible, :is_selfhost) do %> - Certain that you're using the correct email address but still aren't receiving the password reset email? Please check your spam folder or ask on our community-supported forum. - <% else %> - Certain that you're using the correct email address but still aren't receiving the password reset email? Please check your spam folder or contact us. - <% end %> -
+
+
+ + Certain that you're using the correct email address but still aren't receiving the password reset email? Please check your spam folder or ask on our community-supported forum. + + + Certain that you're using the correct email address but still aren't receiving the password reset email? Please check your spam folder or contact us. +

diff --git a/lib/plausible_web/templates/auth/user_settings.html.heex b/lib/plausible_web/templates/auth/user_settings.html.heex index ac373d707..6ede880e4 100644 --- a/lib/plausible_web/templates/auth/user_settings.html.heex +++ b/lib/plausible_web/templates/auth/user_settings.html.heex @@ -1,4 +1,4 @@ -<%= if !Application.get_env(:plausible, :is_selfhost) do %> +<%= if full_build?() do %>

Subscription Plan

diff --git a/lib/plausible_web/templates/email/google_analytics_import.html.eex b/lib/plausible_web/templates/email/google_analytics_import.html.heex similarity index 82% rename from lib/plausible_web/templates/email/google_analytics_import.html.eex rename to lib/plausible_web/templates/email/google_analytics_import.html.heex index a99c2f91a..d7d1ce539 100644 --- a/lib/plausible_web/templates/email/google_analytics_import.html.eex +++ b/lib/plausible_web/templates/email/google_analytics_import.html.heex @@ -1,19 +1,16 @@ <%= if @success do %> - Your Google Analytics import has completed successfully. The Plausible dashboard for <%= @site.domain %> now contains historical imported data from <%= date_format(@site.imported_data.start_date) %> to <%= date_format(@site.imported_data.end_date) %> -

-<%= link("Click here", to: @link) %> to view your dashboard. + Your Google Analytics import has completed successfully. The Plausible dashboard for <%= @site.domain %> now contains historical imported data from <%= date_format( + @site.imported_data.start_date + ) %> to <%= date_format(@site.imported_data.end_date) %> +

+ <%= link("Click here", to: @link) %> to view your dashboard. <% else %> Unfortunately, your Google Analytics import for <%= @site.domain %> did not complete successfully. Sorry about that!

- This may be due to the user metric setting in your Google Analytics account. We found that changing the user metric setting is necessary to get the correct data out of the Google API.

- Please log into your Google Analytics account and go to Admin -> Property Settings -> User Analysis. Then make sure that "Enable User Metric in Reporting" is set to OFF. Then try importing again. You can switch the setting back after the import. -

- - If you've already changed that setting, then it is something else: -
+

If you've already changed that setting, then it is something else:

- If all of the above is fine, please try to do the import once again. Sometimes the Google Analytics API just randomly returns empty data. It's intermittent and random. Trying to do the import again may return what you need. - - <%= if !Application.get_env(:plausible, :is_selfhost) do %> + <%= if full_build?() do %>

Please reply to this email to let us know if you're still experiencing issues with the import. <% end %> diff --git a/lib/plausible_web/templates/error/server_error.html.eex b/lib/plausible_web/templates/error/server_error.html.eex deleted file mode 100644 index d6d26a198..000000000 --- a/lib/plausible_web/templates/error/server_error.html.eex +++ /dev/null @@ -1,54 +0,0 @@ -
-
-

Oops, sorry about that...

- -
-
-
- -
-
-

- There has been a server error. - <%= if !assigns[:selfhosted?] do %> - But don't worry, we're on it! - <% end %> -

- -
-

- <%= if assigns[:trace_id] && !assigns[:selfhosted?] do %> - If you would like to help, tell us what you were trying to do. Our development team will receive your report. - <% else %> - We have been notified. - <% end %> -

-
-
-
-
- - <%= if !assigns[:selfhosted?] do %> - <%= if assigns[:trace_id] do %> -
- <%= form_for :error, Routes.error_report_path(PlausibleWeb.Endpoint, :submit_error_report), fn f -> %> - <%= hidden_input f, :trace_id, value: @trace_id %> - <%= label f, "What happened?", class: "block text-sm font-medium text-gray-700 dark:text-gray-300" %> - <%= textarea f, :user_feedback, rows: 5, class: "block w-full max-w-xl border-gray-300 dark:border-gray-700 resize-none shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300", placeholder: "I clicked on 'X' and then hit 'Confirm'..." %> - <%= submit "Send feedback →", class: "button mt-4 w-full" %> - <% end %> -
- <% end %> - -
-

- <%= link("Home page", to: "/", class: "text-indigo-500 mr-8") %> - <%= link("Status page", to: "https://status.plausible.io", class: "mr-8 text-indigo-500") %> - <%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %> -

-
- <% end %> -
-
diff --git a/lib/plausible_web/templates/error/server_error.html.heex b/lib/plausible_web/templates/error/server_error.html.heex new file mode 100644 index 000000000..24077e525 --- /dev/null +++ b/lib/plausible_web/templates/error/server_error.html.heex @@ -0,0 +1,74 @@ +
+
+

Oops, sorry about that...

+ +
+
+
+ +
+
+

+ There has been a server error. + <%= if full_build?() do %> + But don't worry, we're on it! + <% end %> +

+ +
+

+ <%= if assigns[:trace_id] && full_build?() do %> + If you would like to help, tell us what you were trying to do. Our development team will receive your report. + <% else %> + We have been notified. + <% end %> +

+
+
+
+
+ + <%= if full_build?() do %> + <%= if assigns[:trace_id] do %> +
+ <%= form_for :error, Routes.error_report_path(PlausibleWeb.Endpoint, :submit_error_report), fn f -> %> + <%= hidden_input(f, :trace_id, value: @trace_id) %> + <%= label(f, "What happened?", + class: "block text-sm font-medium text-gray-700 dark:text-gray-300" + ) %> + <%= textarea(f, :user_feedback, + rows: 5, + class: + "block w-full max-w-xl border-gray-300 dark:border-gray-700 resize-none shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md dark:bg-gray-900 dark:text-gray-300", + placeholder: "I clicked on 'X' and then hit 'Confirm'..." + ) %> + <%= submit("Send feedback →", class: "button mt-4 w-full") %> + <% end %> +
+ <% end %> + +
+

+ <%= link("Home page", to: "/", class: "text-indigo-500 mr-8") %> + <%= link("Status page", + to: "https://status.plausible.io", + class: "mr-8 text-indigo-500" + ) %> + <%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-500") %> +

+
+ <% end %> +
+
diff --git a/lib/plausible_web/templates/layout/_footer.html.eex b/lib/plausible_web/templates/layout/_footer.html.eex deleted file mode 100644 index 6e0d5e5cf..000000000 --- a/lib/plausible_web/templates/layout/_footer.html.eex +++ /dev/null @@ -1,190 +0,0 @@ -
-
-
-
-

- - Plausible Analytics -

-

- <%= if !Application.get_env(:plausible, :is_selfhost) do %> - Made and hosted in the EU 🇪🇺
- <% end %> - Solely funded by our subscribers.
-

- <%= if Application.get_env(:plausible, :is_selfhost) do %> - - <% end %> -
- -
-
-
diff --git a/lib/plausible_web/templates/layout/_footer.html.heex b/lib/plausible_web/templates/layout/_footer.html.heex new file mode 100644 index 000000000..a6454d23b --- /dev/null +++ b/lib/plausible_web/templates/layout/_footer.html.heex @@ -0,0 +1,317 @@ +
+
+
+
+

+ + Plausible Analytics +

+

+ <%= if full_build?() do %> + Made and hosted in the EU 🇪🇺
+ <% end %> + Solely funded by our subscribers.
+

+ <%= if small_build?() do %> + + <% end %> +
+ +
+
+
diff --git a/lib/plausible_web/templates/layout/_header.html.heex b/lib/plausible_web/templates/layout/_header.html.heex index 753f746f8..450a5daa7 100644 --- a/lib/plausible_web/templates/layout/_header.html.heex +++ b/lib/plausible_web/templates/layout/_header.html.heex @@ -24,15 +24,16 @@ <%= cond do %> <% @conn.assigns[:current_user] -> %>
@@ -76,7 +77,7 @@ - <%= if @conn.assigns[:current_user] && !Application.get_env(:plausible, :is_selfhost) do %> + <%= if @conn.assigns[:current_user] && full_build?() do %>
  • <% end %> diff --git a/lib/plausible_web/templates/layout/_tracking.html.heex b/lib/plausible_web/templates/layout/_tracking.html.heex index 9e3cc9b1c..2d1ea894a 100644 --- a/lib/plausible_web/templates/layout/_tracking.html.heex +++ b/lib/plausible_web/templates/layout/_tracking.html.heex @@ -1,4 +1,4 @@ -<%= if !Application.get_env(:plausible, :is_selfhost) && !@conn.assigns[:skip_plausible_tracking] do %> +<%= if full_build?() && !@conn.assigns[:skip_plausible_tracking] do %> - -
    - <%= if @site.locked do %> - - <% end %> -
    -

    Waiting for first pageview

    -

    on <%= @site.domain %>

    -
    -
    -

    - Need to see the snippet again? <%= link("Click here", to: "/#{URI.encode_www_form(@site.domain)}/snippet", class: "text-indigo-600 dark:text-indigo-500 text-underline")%>
    - Not working? <%= link("Troubleshoot the integration", to: "https://plausible.io/docs/troubleshoot-integration#check-for-the-plausible-snippet-in-your-source-code", class: "text-indigo-600 dark:text-indigo-500 text-underline", rel: "noreferrer") %> with our guide - <%= if Application.get_env(:plausible, :is_selfhost) do %> - first
    Still not working? Ask on our <%= link("community-supported forum", to: "https://github.com/plausible/analytics/discussions", class: "text-indigo-600 dark:text-indigo-500 text-underline" ) %> - <% else %> - first
    Still not working? <%= link("Contact us", to: "https://plausible.io/contact", class: "text-indigo-600 dark:text-indigo-500 text-underline" ) %> and we will help you with your setup - <% end %> -

    -
    -
    -
    diff --git a/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex b/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex new file mode 100644 index 000000000..1c8af5e7e --- /dev/null +++ b/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex @@ -0,0 +1,59 @@ + + +
    + <%= if @site.locked do %> + + <% end %> +
    +

    Waiting for first pageview

    +

    on <%= @site.domain %>

    +
    +
    +

    + Need to see the snippet again? + <.styled_link href={"/#{URI.encode_www_form(@site.domain)}/snippet"}> + Click here + + +
    Not working? + <.styled_link + new_tab + href="https://plausible.io/docs/troubleshoot-integration#check-for-the-plausible-snippet-in-your-source-code" + > + Troubleshoot the integration + + +
    + + Still not working? + <.styled_link new_tab href="https://plausible.io/contact">Contact us + and we will help you with your setup + + + Still not working? Ask on our + <.styled_link new_tab href="https://github.com/plausible/analytics/discussions"> + community-supported forum + + +

    +
    +
    +
    diff --git a/lib/plausible_web/views/auth_view.ex b/lib/plausible_web/views/auth_view.ex index ec2ab0dfb..b7f26d29f 100644 --- a/lib/plausible_web/views/auth_view.ex +++ b/lib/plausible_web/views/auth_view.ex @@ -1,4 +1,5 @@ defmodule PlausibleWeb.AuthView do + use Plausible use PlausibleWeb, :view require Plausible.Billing.Subscription.Status alias Plausible.Billing.{Plans, Subscription} diff --git a/lib/plausible_web/views/email_view.ex b/lib/plausible_web/views/email_view.ex index 786efb537..e2c6db164 100644 --- a/lib/plausible_web/views/email_view.ex +++ b/lib/plausible_web/views/email_view.ex @@ -1,4 +1,5 @@ defmodule PlausibleWeb.EmailView do + use Plausible use PlausibleWeb, :view def plausible_url do diff --git a/lib/plausible_web/views/error_view.ex b/lib/plausible_web/views/error_view.ex index 1e133e6f5..3f770e73e 100644 --- a/lib/plausible_web/views/error_view.ex +++ b/lib/plausible_web/views/error_view.ex @@ -1,9 +1,10 @@ defmodule PlausibleWeb.ErrorView do + use Plausible use PlausibleWeb, :view def render("500.json", %{conn: %{private: %{PlausibleWeb.Plugins.API.Router => _}}}) do contact_support_note = - if not Plausible.Release.selfhost?() do + on_full_build do "If the problem persists please contact support@plausible.io" end @@ -32,23 +33,21 @@ defmodule PlausibleWeb.ErrorView do def render(<<"5", _error_5xx::binary-size(2), ".html">>, assigns) do current_user = assigns[:current_user] - selfhosted? = Plausible.Release.selfhost?() last_event = Sentry.get_last_event_id_and_source() - case {selfhosted?, current_user, last_event} do - {false, current_user, {event_id, :plug}} + case {current_user, last_event} do + {current_user, {event_id, :plug}} when is_binary(event_id) and not is_nil(current_user) -> opts = %{ trace_id: event_id, user_name: current_user.name, - user_email: current_user.email, - selfhosted?: selfhosted? + user_email: current_user.email } render("server_error.html", Map.merge(opts, assigns)) _ -> - render("server_error.html", Map.put(assigns, :selfhosted?, selfhosted?)) + render("server_error.html", assigns) end end diff --git a/lib/plausible_web/views/layout_view.ex b/lib/plausible_web/views/layout_view.ex index 092b6871d..707b24b38 100644 --- a/lib/plausible_web/views/layout_view.ex +++ b/lib/plausible_web/views/layout_view.ex @@ -83,9 +83,6 @@ defmodule PlausibleWeb.LayoutView do [key: "Custom Properties", value: "properties"], [key: "Integrations", value: "integrations"], [key: "Email Reports", value: "email-reports"], - if !is_selfhost() && conn.assigns[:site].custom_domain do - [key: "Custom domain", value: "custom-domain"] - end, if conn.assigns[:current_user_role] == :owner do [key: "Danger zone", value: "danger-zone"] end @@ -123,8 +120,4 @@ defmodule PlausibleWeb.LayoutView do def is_current_tab(conn, tab) do List.last(conn.path_info) == tab end - - defp is_selfhost() do - Application.get_env(:plausible, :is_selfhost) - end end diff --git a/lib/plausible_web/views/site_view.ex b/lib/plausible_web/views/site_view.ex index 6c95abeb3..b7f97920f 100644 --- a/lib/plausible_web/views/site_view.ex +++ b/lib/plausible_web/views/site_view.ex @@ -23,12 +23,7 @@ defmodule PlausibleWeb.SiteView do end def render_snippet(site) do - tracker = - if site.custom_domain do - "https://" <> site.custom_domain.domain <> "/js/index.js" - else - "#{plausible_url()}/js/script.js" - end + tracker = "#{plausible_url()}/js/script.js" """ diff --git a/lib/plausible_web/views/stats_view.ex b/lib/plausible_web/views/stats_view.ex index a8babcd2e..70e5e10bd 100644 --- a/lib/plausible_web/views/stats_view.ex +++ b/lib/plausible_web/views/stats_view.ex @@ -1,5 +1,6 @@ defmodule PlausibleWeb.StatsView do use PlausibleWeb, :view + use Plausible def base_domain do PlausibleWeb.Endpoint.host() diff --git a/mix.exs b/mix.exs index c4b558a0b..96cfb8426 100644 --- a/mix.exs +++ b/mix.exs @@ -49,7 +49,7 @@ defmodule Plausible.MixProject do defp elixirc_paths(env) when env in [:test, :dev], do: ["lib", "test/support", "extra/lib"] - defp elixirc_paths(env) when env in [:small_test], + defp elixirc_paths(env) when env in [:small_test, :small_dev], do: ["lib", "test/support"] defp elixirc_paths(:small), do: ["lib"] @@ -80,10 +80,10 @@ defmodule Plausible.MixProject do {:ecto, "~> 3.10.0"}, {:ecto_sql, "~> 3.10.0"}, {:envy, "~> 1.1.1"}, - {:ex_machina, "~> 2.3", only: [:dev, :test, :small, :small_test]}, + {:ex_machina, "~> 2.3", only: [:dev, :test, :small_dev, :small_test]}, {:excoveralls, "~> 0.10", only: :test}, {:finch, "~> 0.16.0"}, - {:floki, "~> 0.34.3", only: [:dev, :test, :small, :small_test]}, + {:floki, "~> 0.34.3", only: [:dev, :test, :small_dev, :small_test]}, {:fun_with_flags, "~> 1.9.0"}, {:fun_with_flags_ui, "~> 0.8"}, {:locus, "~> 2.3"}, @@ -110,7 +110,7 @@ defmodule Plausible.MixProject do {:phoenix_view, "~> 2.0"}, {:phoenix_ecto, "~> 4.0"}, {:phoenix_html, "~> 3.3", override: true}, - {:phoenix_live_reload, "~> 1.2", only: [:dev, :small]}, + {:phoenix_live_reload, "~> 1.2", only: [:dev, :small_dev]}, {:phoenix_pubsub, "~> 2.0"}, {:phoenix_live_view, "~> 0.18"}, {:php_serializer, "~> 2.0"}, @@ -134,8 +134,8 @@ defmodule Plausible.MixProject do {:joken, "~> 2.5"}, {:paginator, git: "https://github.com/duffelhq/paginator.git"}, {:scrivener_ecto, "~> 2.0"}, - {:esbuild, "~> 0.7", runtime: Mix.env() in [:dev, :small]}, - {:tailwind, "~> 0.2.0", runtime: Mix.env() in [:dev, :small]}, + {:esbuild, "~> 0.7", runtime: Mix.env() in [:dev, :small_dev]}, + {:tailwind, "~> 0.2.0", runtime: Mix.env() in [:dev, :small_dev]}, {:ex_json_logger, "~> 1.3.0"} ] end diff --git a/priv/repo/migrations/20231127132321_remove_custom_domains.exs b/priv/repo/migrations/20231127132321_remove_custom_domains.exs new file mode 100644 index 000000000..d53307a9b --- /dev/null +++ b/priv/repo/migrations/20231127132321_remove_custom_domains.exs @@ -0,0 +1,7 @@ +defmodule Plausible.Repo.Migrations.RemoveCustomDomains do + use Ecto.Migration + + def change do + drop table(:custom_domains) + end +end diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs index 51da2403e..1e4d30c00 100644 --- a/test/plausible/billing/billing_test.exs +++ b/test/plausible/billing/billing_test.exs @@ -108,6 +108,7 @@ defmodule Plausible.BillingTest do end describe "on_trial?" do + @describetag :full_build_only test "is true with >= 0 trial days left" do user = insert(:user) diff --git a/test/plausible/billing/feature_test.exs b/test/plausible/billing/feature_test.exs index 4ce91fca8..845a0b7f4 100644 --- a/test/plausible/billing/feature_test.exs +++ b/test/plausible/billing/feature_test.exs @@ -74,7 +74,7 @@ defmodule Plausible.Billing.FeatureTest do end test "Plausible.Billing.Feature.StatsAPI.check_availability/2 returns error when user trial hasn't started and was created after the business tier launch" do - user = insert(:user, inserted_at: ~N[2024-01-01T00:00:00], trial_expiry_date: nil) + user = insert(:user, trial_expiry_date: nil) assert {:error, :upgrade_required} == Plausible.Billing.Feature.StatsAPI.check_availability(user) diff --git a/test/plausible/billing/plans_test.exs b/test/plausible/billing/plans_test.exs index 453604567..7c50052a0 100644 --- a/test/plausible/billing/plans_test.exs +++ b/test/plausible/billing/plans_test.exs @@ -68,7 +68,7 @@ defmodule Plausible.Billing.PlansTest do end test "growth_plans_for/1 shows v4 plans for everyone else" do - insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z]) + insert(:user) |> Plans.growth_plans_for() |> assert_generation(4) end @@ -144,7 +144,7 @@ defmodule Plausible.Billing.PlansTest do end test "business_plans_for/1 returns v4 business plans for everyone else" do - user = insert(:user, inserted_at: ~U[2024-01-01T00:00:00Z]) + user = insert(:user) business_plans = Plans.business_plans_for(user) assert Enum.all?(business_plans, &(&1.kind == :business)) @@ -318,14 +318,14 @@ defmodule Plausible.Billing.PlansTest do describe "suggest_tier/1" do test "suggests Business when user has used a premium feature" do - user = insert(:user, inserted_at: ~N[2024-01-01 10:00:00]) + user = insert(:user) insert(:api_key, user: user) assert Plans.suggest_tier(user) == :business end test "suggests Growth when no premium features used" do - user = insert(:user, inserted_at: ~N[2024-01-01 10:00:00]) + user = insert(:user) site = insert(:site, members: [user]) insert(:goal, site: site, event_name: "goals_is_not_premium") diff --git a/test/plausible/billing/quota_test.exs b/test/plausible/billing/quota_test.exs index 9795eacaf..e70be213e 100644 --- a/test/plausible/billing/quota_test.exs +++ b/test/plausible/billing/quota_test.exs @@ -16,6 +16,7 @@ defmodule Plausible.Billing.QuotaTest do @v3_business_plan_id "857481" describe "site_limit/1" do + @describetag :full_build_only test "returns 50 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)) @@ -50,8 +51,7 @@ defmodule Plausible.Billing.QuotaTest do test "returns 10 when user in on trial" do user = insert(:user, - trial_expiry_date: Timex.shift(Timex.now(), days: 7), - inserted_at: ~U[2024-01-01T00:00:00Z] + trial_expiry_date: Timex.shift(Timex.now(), days: 7) ) assert 10 == Quota.site_limit(user) @@ -81,8 +81,7 @@ defmodule Plausible.Billing.QuotaTest do user = insert(:user, enterprise_plan: build(:enterprise_plan, paddle_plan_id: "123321"), - subscription: nil, - inserted_at: ~U[2024-01-01T00:00:00Z] + subscription: nil ) assert 10 == Quota.site_limit(user) @@ -385,8 +384,7 @@ defmodule Plausible.Billing.QuotaTest do test "returns 5 when user in on trial" do user = insert(:user, - trial_expiry_date: Timex.shift(Timex.now(), days: 7), - inserted_at: ~U[2024-01-01T00:00:00Z] + trial_expiry_date: Timex.shift(Timex.now(), days: 7) ) assert 3 == Quota.team_member_limit(user) diff --git a/test/plausible/plugins/api/token_test.exs b/test/plausible/plugins/api/token_test.exs index 57c848f5d..47ce9fdab 100644 --- a/test/plausible/plugins/api/token_test.exs +++ b/test/plausible/plugins/api/token_test.exs @@ -3,6 +3,7 @@ defmodule Plausible.Plugins.API.TokenTest do alias Plausible.Plugins.API.Token + @tag :full_build_only test "basic token properties" do t1 = Token.generate() t2 = Token.generate() @@ -24,20 +25,23 @@ defmodule Plausible.Plugins.API.TokenTest do end describe "prefix/0" do - test "default prefix" do + @tag :full_build_only + test "default prefix - full build" do assert Token.prefix() == "plausible-plugin-test" end + @tag :small_build_only test "selfhosted prefix" do - patch_env(:is_selfhost, true) assert Token.prefix() == "plausible-plugin-selfhost" end + @tag :full_build_only test "prod prefix" do patch_env(:environment, "prod") assert Token.prefix() == "plausible-plugin" end + @tag :full_build_only test "staging prefix" do patch_env(:environment, "staging") assert Token.prefix() == "plausible-plugin-staging" diff --git a/test/plausible/plugins/api/tokens_test.exs b/test/plausible/plugins/api/tokens_test.exs index 43538f7f6..223e92ddb 100644 --- a/test/plausible/plugins/api/tokens_test.exs +++ b/test/plausible/plugins/api/tokens_test.exs @@ -9,7 +9,6 @@ defmodule Plausible.Plugins.API.TokensTest do test "generates and stores the token" do site = insert(:site) assert {:ok, %Token{} = token, raw} = Tokens.create(site, "My test token") - assert <<"plausible-plugin-test-", _::binary-size(40)>> = raw from_db = Repo.get(Token, token.id) diff --git a/test/plausible/release_test.exs b/test/plausible/release_test.exs index c1410594e..07ba568fd 100644 --- a/test/plausible/release_test.exs +++ b/test/plausible/release_test.exs @@ -1,35 +1,43 @@ defmodule Plausible.ReleaseTest do - use Plausible.DataCase + use Plausible.DataCase, async: true alias Plausible.{Release, Auth} + import ExUnit.CaptureIO describe "should_be_first_launch?/0" do + @tag :small_build_only test "returns true when self-hosted and no users" do - patch_env(:is_selfhost, true) refute Repo.exists?(Auth.User) assert Release.should_be_first_launch?() end + @tag :full_build_only test "returns false when not self-hosted and has no users" do - patch_env(:is_selfhost, false) refute Repo.exists?(Auth.User) refute Release.should_be_first_launch?() end + @tag :full_build_only test "returns false when not self-hosted and has users" do insert(:user) - patch_env(:is_selfhost, false) refute Release.should_be_first_launch?() end + @tag :small_build_only test "returns false when self-hosted and has users" do insert(:user) - patch_env(:is_selfhost, true) refute Release.should_be_first_launch?() end end test "dump_plans/0 inserts plans" do - Release.dump_plans() - assert Plausible.Repo.aggregate("plans", :count) == 54 + stdout = + capture_io(fn -> + Release.dump_plans() + end) + + assert stdout =~ "Loading plausible.." + assert stdout =~ "Starting dependencies.." + assert stdout =~ "Starting repos.." + assert stdout =~ "Inserted 54 plans" end end diff --git a/test/plausible/site/memberships/accept_invitation_test.exs b/test/plausible/site/memberships/accept_invitation_test.exs index c7fdaa25c..3098dc257 100644 --- a/test/plausible/site/memberships/accept_invitation_test.exs +++ b/test/plausible/site/memberships/accept_invitation_test.exs @@ -367,6 +367,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do refute Repo.reload(invitation) end + @tag :full_build_only test "locks the site if the new owner has no active subscription or trial" do existing_owner = insert(:user) @@ -420,6 +421,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do refute Repo.reload!(site).locked end + @tag :full_build_only test "ends trial of the new owner immediately" do existing_owner = insert(:user) @@ -446,6 +448,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do assert Repo.reload!(site).locked end + @tag :full_build_only test "sets user's trial expiry date to yesterday if they don't have one" do existing_owner = insert(:user) @@ -472,6 +475,7 @@ defmodule Plausible.Site.Memberships.AcceptInvitationTest do assert Repo.reload!(site).locked end + @tag :full_build_only test "ends grace period and sends an email about it if new owner is past grace period" do existing_owner = insert(:user) diff --git a/test/plausible/site/memberships/create_invitation_test.exs b/test/plausible/site/memberships/create_invitation_test.exs index 672fdde1d..5b8e6f720 100644 --- a/test/plausible/site/memberships/create_invitation_test.exs +++ b/test/plausible/site/memberships/create_invitation_test.exs @@ -67,7 +67,7 @@ defmodule Plausible.Site.Memberships.CreateInvitationTest do end test "returns error when owner is over their team member limit" do - [owner, inviter, invitee] = insert_list(3, :user, inserted_at: ~N[2024-01-01T00:00:00Z]) + [owner, inviter, invitee] = insert_list(3, :user) memberships = [ diff --git a/test/plausible/site/sites_test.exs b/test/plausible/site/sites_test.exs index 7e5d489b4..7406d2e85 100644 --- a/test/plausible/site/sites_test.exs +++ b/test/plausible/site/sites_test.exs @@ -69,6 +69,7 @@ defmodule Plausible.SitesTest do end describe "get_for_user/2" do + @tag :full_build_only test "get site for super_admin" do user1 = insert(:user) user2 = insert(:user) diff --git a/test/plausible/stats/query_test.exs b/test/plausible/stats/query_test.exs index 415f66e67..49b4033c2 100644 --- a/test/plausible/stats/query_test.exs +++ b/test/plausible/stats/query_test.exs @@ -158,11 +158,13 @@ defmodule Plausible.Stats.QueryTest do assert q.interval == "date" end + @tag :full_build_only test "adds sample_threshold :infinite to query struct", %{site: site} do q = Query.from(site, %{"period" => "30d", "sample_threshold" => "infinite"}) assert q.sample_threshold == :infinite end + @tag :full_build_only test "casts sample_threshold to integer in query struct", %{site: site} do q = Query.from(site, %{"period" => "30d", "sample_threshold" => "30000000"}) assert q.sample_threshold == 30_000_000 diff --git a/test/plausible_web/components/billing_test.exs b/test/plausible_web/components/billing_test.exs index 23eaf5cd9..f71a60220 100644 --- a/test/plausible_web/components/billing_test.exs +++ b/test/plausible_web/components/billing_test.exs @@ -87,6 +87,7 @@ defmodule PlausibleWeb.Components.BillingTest do assert rendered =~ "please reach out to the site owner to upgrade their subscription" end + @tag :full_build_only test "limit_exceeded_notice/1 when billable user is on trial displays upgrade link" do me = insert(:user) diff --git a/test/plausible_web/controllers/admin_auth_controller_test.exs b/test/plausible_web/controllers/admin_auth_controller_test.exs index e07fb17e5..dc216e126 100644 --- a/test/plausible_web/controllers/admin_auth_controller_test.exs +++ b/test/plausible_web/controllers/admin_auth_controller_test.exs @@ -2,11 +2,10 @@ defmodule PlausibleWeb.AdminAuthControllerTest do use PlausibleWeb.ConnCase alias Plausible.Release - setup_patch_env(:is_selfhost, true) - describe "GET /" do + @describetag :small_build_only test "disable registration", %{conn: conn} do - prevent_first_launch() + insert(:user) patch_config(disable_registration: true) conn = get(conn, "/register") assert redirected_to(conn) == "/login" @@ -26,8 +25,4 @@ defmodule PlausibleWeb.AdminAuthControllerTest do updated_config = Keyword.merge([disable_registration: false], config) patch_env(:selfhost, updated_config) end - - defp prevent_first_launch do - insert(:user) - end end diff --git a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs index a88968e9a..0a8837310 100644 --- a/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs +++ b/test/plausible_web/controllers/api/external_stats_controller/auth_test.exs @@ -67,6 +67,7 @@ defmodule PlausibleWeb.Api.ExternalStatsController.AuthTest do end describe "super admin access" do + @describetag :full_build_only setup %{user: user} do patch_env(:super_admin_user_ids, [user.id]) end diff --git a/test/plausible_web/controllers/api/internal_controller/sync_test.exs b/test/plausible_web/controllers/api/internal_controller/sync_test.exs index 6f32589af..f5d3b883b 100644 --- a/test/plausible_web/controllers/api/internal_controller/sync_test.exs +++ b/test/plausible_web/controllers/api/internal_controller/sync_test.exs @@ -5,6 +5,7 @@ defmodule PlausibleWeb.Api.InternalController.SyncTest do describe "PUT /api/:domain/disable-feature" do setup [:create_user, :log_in] + @tag :full_build_only test "when the logged-in user is an super-admin", %{conn: conn, user: user} do site = insert(:site) patch_env(:super_admin_user_ids, [user.id]) diff --git a/test/plausible_web/controllers/auth_controller/logs_test.exs b/test/plausible_web/controllers/auth_controller/logs_test.exs index e2b1bc293..2babcfb32 100644 --- a/test/plausible_web/controllers/auth_controller/logs_test.exs +++ b/test/plausible_web/controllers/auth_controller/logs_test.exs @@ -2,6 +2,8 @@ defmodule PlausibleWeb.AuthController.LogsTest do use PlausibleWeb.ConnCase import ExUnit.CaptureLog + setup {PlausibleWeb.FirstLaunchPlug.Test, :skip} + describe "POST /login" do setup do patch_env(:log_failed_login_attempts, true) diff --git a/test/plausible_web/controllers/auth_controller_test.exs b/test/plausible_web/controllers/auth_controller_test.exs index cf579e81e..7c2e0edcc 100644 --- a/test/plausible_web/controllers/auth_controller_test.exs +++ b/test/plausible_web/controllers/auth_controller_test.exs @@ -13,7 +13,8 @@ defmodule PlausibleWeb.AuthControllerTest do alias Plausible.Auth.User alias Plausible.Billing.Subscription - setup :verify_on_exit! + setup {PlausibleWeb.FirstLaunchPlug.Test, :skip} + setup [:verify_on_exit!] @v3_plan_id "749355" @v4_plan_id "857097" @@ -442,7 +443,8 @@ defmodule PlausibleWeb.AuthControllerTest do assert location = "/login" = redirected_to(conn, 302) - conn = get(recycle(conn), location) + {:ok, %{conn: conn}} = PlausibleWeb.FirstLaunchPlug.Test.skip(%{conn: recycle(conn)}) + conn = get(conn, location) assert html_response(conn, 200) =~ "Password updated successfully" end end @@ -457,6 +459,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert resp =~ "Change email address" end + @tag :full_build_only test "shows subscription", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "558018", user: user) conn = get(conn, "/settings") @@ -464,6 +467,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "monthly billing" end + @tag :full_build_only test "shows yearly subscription", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "590752", user: user) conn = get(conn, "/settings") @@ -471,6 +475,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "yearly billing" end + @tag :full_build_only test "shows free subscription", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "free_10k", user: user) conn = get(conn, "/settings") @@ -478,6 +483,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "N/A billing" end + @tag :full_build_only test "shows enterprise plan subscription", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "123", user: user) @@ -488,6 +494,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "yearly billing" end + @tag :full_build_only test "shows current enterprise plan subscription when user has a new one to upgrade to", %{ conn: conn, user: user @@ -511,6 +518,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "yearly billing" end + @tag :full_build_only test "renders two links to '/billing/choose-plan` with the text 'Upgrade'", %{conn: conn} do doc = get(conn, "/settings") @@ -526,6 +534,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert text_of_attr(upgrade_link_2, "href") == Routes.billing_path(conn, :choose_plan) end + @tag :full_build_only test "renders a link to '/billing/choose-plan' with the text 'Change plan' + cancel link", %{ conn: conn, user: user @@ -585,6 +594,7 @@ defmodule PlausibleWeb.AuthControllerTest do refute element_exists?(doc, "#upgrade-or-change-plan-link") end + @tag :full_build_only test "renders two links to '/billing/choose-plan' with the text 'Upgrade' for a configured enterprise plan", %{conn: conn, user: user} do configure_enterprise_plan(user) @@ -607,6 +617,7 @@ defmodule PlausibleWeb.AuthControllerTest do Routes.billing_path(conn, :choose_plan) end + @tag :full_build_only test "links to '/billing/choose-plan' with the text 'Change plan' for a configured enterprise plan with an existing subscription + renders cancel button", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: @v3_plan_id, user: user) @@ -628,6 +639,7 @@ defmodule PlausibleWeb.AuthControllerTest do Routes.billing_path(conn, :choose_plan) end + @tag :full_build_only test "renders cancelled subscription notice", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: @v4_plan_id, @@ -645,6 +657,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert notice_text =~ "Upgrade your subscription to get access to your stats again" end + @tag :full_build_only test "renders cancelled subscription notice with some subscription days still left", %{ conn: conn, user: user @@ -666,6 +679,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert notice_text =~ "Upgrade your subscription to make sure you don't lose access" end + @tag :full_build_only test "renders cancelled subscription notice with a warning about losing grandfathering", %{ conn: conn, user: user @@ -689,6 +703,7 @@ defmodule PlausibleWeb.AuthControllerTest do "by letting your subscription expire, you lose access to our grandfathered terms" end + @tag :full_build_only test "shows invoices for subscribed user", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "558018", @@ -703,6 +718,7 @@ defmodule PlausibleWeb.AuthControllerTest do assert html_response(conn, 200) =~ "$22.00" end + @tag :full_build_only test "shows 'something went wrong' on failed invoice request'", %{conn: conn, user: user} do insert(:subscription, paddle_plan_id: "558018", diff --git a/test/plausible_web/controllers/avatar_controller_test.exs b/test/plausible_web/controllers/avatar_controller_test.exs index 9ac531eb9..7639f7ca1 100644 --- a/test/plausible_web/controllers/avatar_controller_test.exs +++ b/test/plausible_web/controllers/avatar_controller_test.exs @@ -4,6 +4,8 @@ defmodule PlausibleWeb.AvatarControllerTest do import Mox setup :verify_on_exit! + setup {PlausibleWeb.FirstLaunchPlug.Test, :skip} + describe "GET /avatar/:hash" do test "proxies the request to gravatar", %{conn: conn} do expect( diff --git a/test/plausible_web/controllers/error_report_controller_test.exs b/test/plausible_web/controllers/error_report_controller_test.exs index 3af101c7d..52eed6613 100644 --- a/test/plausible_web/controllers/error_report_controller_test.exs +++ b/test/plausible_web/controllers/error_report_controller_test.exs @@ -1,5 +1,7 @@ defmodule PlausibleWeb.ErrorReportControllerTest do use PlausibleWeb.ConnCase, async: true + @moduletag :full_build_only + use Bamboo.Test import Phoenix.View diff --git a/test/plausible_web/controllers/page_controller_test.exs b/test/plausible_web/controllers/page_controller_test.exs index f9246391d..b4c390a0f 100644 --- a/test/plausible_web/controllers/page_controller_test.exs +++ b/test/plausible_web/controllers/page_controller_test.exs @@ -1,6 +1,8 @@ defmodule PlausibleWeb.PageControllerTest do use PlausibleWeb.ConnCase, async: true + setup {PlausibleWeb.FirstLaunchPlug.Test, :skip} + describe "GET /" do test "shows landing page when user not authenticated", %{conn: conn} do assert conn |> get("/") |> html_response(200) =~ "Welcome to Plausible Analytics!" diff --git a/test/plausible_web/controllers/site_controller_test.exs b/test/plausible_web/controllers/site_controller_test.exs index a7601a429..d8629cb9f 100644 --- a/test/plausible_web/controllers/site_controller_test.exs +++ b/test/plausible_web/controllers/site_controller_test.exs @@ -298,6 +298,7 @@ defmodule PlausibleWeb.SiteControllerTest do assert_no_emails_delivered() end + @tag :full_build_only test "does not allow site creation when the user is at their site limit", %{ conn: conn, user: user @@ -552,7 +553,6 @@ defmodule PlausibleWeb.SiteControllerTest do test "deletes the site", %{conn: conn, user: user} do site = insert(:site, members: [user]) insert(:google_auth, user: user, site: site) - insert(:custom_domain, site: site) insert(:spike_notification, site: site) delete(conn, "/#{site.domain}") @@ -563,7 +563,6 @@ defmodule PlausibleWeb.SiteControllerTest do test "fails to delete a site with insufficient permissions", %{conn: conn, user: user} do site = insert(:site, memberships: [build(:site_membership, user: user, role: :viewer)]) insert(:google_auth, user: user, site: site) - insert(:custom_domain, site: site) insert(:spike_notification, site: site) conn = delete(conn, "/#{site.domain}") @@ -581,7 +580,6 @@ defmodule PlausibleWeb.SiteControllerTest do 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) my_conn = delete(my_conn, "/#{other_site.domain}") @@ -1229,35 +1227,6 @@ defmodule PlausibleWeb.SiteControllerTest do end end - describe "DELETE sites/:website/custom-domains/:id" do - setup [:create_user, :log_in, :create_site] - - test "deletes custom domain", %{conn: conn, site: site} do - domain = insert(:custom_domain, site: site) - - conn = delete(conn, "/sites/#{site.domain}/custom-domains/#{domain.id}") - - assert Phoenix.Flash.get(conn.assigns.flash, :success) == - "Custom domain deleted successfully" - - assert Repo.aggregate(Plausible.Site.CustomDomain, :count, :id) == 0 - end - - test "fails to delete custom domain not owning it", %{conn: conn, site: site} do - _og_domain = insert(:custom_domain, site: site) - - foreign_site = insert(:site) - foreign_domain = insert(:custom_domain, site: foreign_site) - - assert Repo.aggregate(Plausible.Site.CustomDomain, :count, :id) == 2 - - conn = delete(conn, "/sites/#{site.domain}/custom-domains/#{foreign_domain.id}") - assert Phoenix.Flash.get(conn.assigns.flash, :error) == "Failed to delete custom domain" - - assert Repo.aggregate(Plausible.Site.CustomDomain, :count, :id) == 2 - end - end - describe "GET /:website/import/google-analytics/view-id" do setup [:create_user, :log_in, :create_new_site] diff --git a/test/plausible_web/controllers/stats_controller_test.exs b/test/plausible_web/controllers/stats_controller_test.exs index cf3ef2382..721749ae0 100644 --- a/test/plausible_web/controllers/stats_controller_test.exs +++ b/test/plausible_web/controllers/stats_controller_test.exs @@ -3,6 +3,8 @@ defmodule PlausibleWeb.StatsControllerTest do use Plausible.Repo import Plausible.Test.Support.HTML + @react_container "div#stats-react-container" + describe "GET /:website - anonymous user" do test "public site - shows site stats", %{conn: conn} do site = insert(:site, public: true) @@ -10,7 +12,10 @@ defmodule PlausibleWeb.StatsControllerTest do conn = get(conn, "/#{site.domain}") resp = html_response(conn, 200) - assert element_exists?(resp, "div#stats-react-container") + assert element_exists?(resp, @react_container) + + assert text_of_attr(resp, @react_container, "data-domain") == site.domain + assert text_of_attr(resp, @react_container, "data-is-dbip") == "false" assert ["noindex, nofollow"] == resp @@ -26,7 +31,7 @@ defmodule PlausibleWeb.StatsControllerTest do conn = get(conn, "/#{site.domain}") resp = html_response(conn, 200) - assert element_exists?(resp, "div#stats-react-container") + assert element_exists?(resp, @react_container) assert ["index, nofollow"] == resp @@ -44,6 +49,7 @@ defmodule PlausibleWeb.StatsControllerTest do end test "can not view stats of a private website", %{conn: conn} do + _ = insert(:user) conn = get(conn, "/test-site.com") assert html_response(conn, 404) =~ "There's nothing here" end @@ -72,6 +78,7 @@ defmodule PlausibleWeb.StatsControllerTest do end describe "GET /:website - as a super admin" do + @describetag :full_build_only setup [:create_user, :make_user_super_admin, :log_in] test "can view a private dashboard with stats", %{conn: conn} do diff --git a/test/plausible_web/controllers/unsubscribe_controller_test.exs b/test/plausible_web/controllers/unsubscribe_controller_test.exs index d95527064..4d398e7b2 100644 --- a/test/plausible_web/controllers/unsubscribe_controller_test.exs +++ b/test/plausible_web/controllers/unsubscribe_controller_test.exs @@ -2,6 +2,8 @@ defmodule PlausibleWeb.UnsubscribeControllerTest do use PlausibleWeb.ConnCase, async: true use Plausible.Repo + setup {PlausibleWeb.FirstLaunchPlug.Test, :skip} + describe "GET /sites/:website/weekly-report/unsubscribe" do test "removes a recipient from the weekly report without them having to log in", %{conn: conn} do site = insert(:site) diff --git a/test/plausible_web/plugs/first_launch_plug_test.exs b/test/plausible_web/plugs/first_launch_plug_test.exs index 7048a8fe9..6f33f99b1 100644 --- a/test/plausible_web/plugs/first_launch_plug_test.exs +++ b/test/plausible_web/plugs/first_launch_plug_test.exs @@ -1,5 +1,6 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do use PlausibleWeb.ConnCase + @moduletag :small_build_only import Plug.Test alias PlausibleWeb.FirstLaunchPlug @@ -25,7 +26,6 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do refute conn.halted # even when it's the first launch - patch_env(:is_selfhost, true) assert Release.should_be_first_launch?() conn = conn("GET", "/register") @@ -34,6 +34,7 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do end test "no-op when not first launch" do + insert(:user) refute Release.should_be_first_launch?() conn = conn("GET", "/sites") conn = FirstLaunchPlug.call(conn, @opts) @@ -41,7 +42,6 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do end test "redirects to :redirect_to when first launch" do - patch_env(:is_selfhost, true) assert Release.should_be_first_launch?() conn = conn("GET", "/sites") @@ -53,21 +53,10 @@ defmodule PlausibleWeb.FirstLaunchPlugTest do describe "first launch plug in :browser pipeline" do test "redirects to /register on first launch", %{conn: conn} do - patch_env(:is_selfhost, true) assert Release.should_be_first_launch?() conn = get(conn, "/") assert redirected_to(conn) == "/register" end - - test "no-op when not first launch", %{conn: conn} do - patch_env(:is_selfhost, false) - refute Release.should_be_first_launch?() - - # gets redirected to login by auth plugs - # "first launch" doesn't interfere - conn = get(conn, "/sites") - assert redirected_to(conn) == "/login" - end end end diff --git a/test/support/conn_case.ex b/test/support/conn_case.ex index 60c0d8200..a09926d59 100644 --- a/test/support/conn_case.ex +++ b/test/support/conn_case.ex @@ -19,6 +19,7 @@ defmodule PlausibleWeb.ConnCase do quote do # Import conveniences for testing with connections use Plausible.TestUtils + use Plausible import Plug.Conn import Phoenix.ConnTest alias PlausibleWeb.Router.Helpers, as: Routes diff --git a/test/support/factory.ex b/test/support/factory.ex index 6ee76a287..4b3606ee8 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -135,12 +135,6 @@ defmodule Plausible.Factory do } end - def custom_domain_factory do - %Plausible.Site.CustomDomain{ - domain: sequence(:custom_domain, &"domain-#{&1}.com") - } - end - def weekly_report_factory do %Plausible.Site.WeeklyReport{} end diff --git a/test/support/test_utils.ex b/test/support/test_utils.ex index 078b958c1..b0987a860 100644 --- a/test/support/test_utils.ex +++ b/test/support/test_utils.ex @@ -35,12 +35,7 @@ defmodule Plausible.TestUtils do end def create_user(_) do - {:ok, - user: - Factory.insert(:user, - inserted_at: ~U[2024-01-01T00:00:00Z], - trial_expiry_date: ~D[2024-02-01] - )} + {:ok, user: Factory.insert(:user)} end def create_site(%{user: user}) do diff --git a/test/test_helper.exs b/test/test_helper.exs index 68ecf5e1c..ed8c80428 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -6,7 +6,9 @@ FunWithFlags.enable(:window_time_on_page) Ecto.Adapters.SQL.Sandbox.mode(Plausible.Repo, :manual) if Mix.env() == :small_test do + IO.puts("Test mode: SMALL") ExUnit.configure(exclude: [:slow, :full_build_only]) else + IO.puts("Test mode: FULL") ExUnit.configure(exclude: [:slow, :small_build_only]) end