diff --git a/config/runtime.exs b/config/runtime.exs
index 09007331d..28bcc0086 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -219,6 +219,12 @@ else
database: get_var_from_path_or_env(config_dir, "DATABASE_NAME", "plausible")
end
+config :fun_with_flags, :cache_bust_notifications, enabled: false
+
+config :fun_with_flags, :persistence,
+ adapter: FunWithFlags.Store.Persistent.Ecto,
+ repo: Plausible.Repo
+
config :sentry,
dsn: sentry_dsn,
environment_name: env,
diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex
index 52a21dc5a..b1c49b24b 100644
--- a/lib/plausible/auth/user.ex
+++ b/lib/plausible/auth/user.ex
@@ -4,6 +4,12 @@ defimpl Bamboo.Formatter, for: Plausible.Auth.User do
end
end
+defimpl FunWithFlags.Actor, for: Plausible.Auth.User do
+ def id(%{id: id}) do
+ "user:#{id}"
+ end
+end
+
defmodule Plausible.Auth.GracePeriod do
use Ecto.Schema
diff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.ex
index 02acbf434..a55901c67 100644
--- a/lib/plausible_web/controllers/stats_controller.ex
+++ b/lib/plausible_web/controllers/stats_controller.ex
@@ -25,7 +25,8 @@ defmodule PlausibleWeb.StatsController do
stats_start_date: stats_start_date,
title: "Plausible ยท " <> site.domain,
offer_email_report: offer_email_report,
- demo: demo
+ demo: demo,
+ flags: get_flags(conn.assigns[:current_user])
)
!stats_start_date && can_see_stats ->
@@ -186,7 +187,8 @@ defmodule PlausibleWeb.StatsController do
shared_link_auth: shared_link.slug,
embedded: conn.params["embed"] == "true",
background: conn.params["background"],
- theme: conn.params["theme"]
+ theme: conn.params["theme"],
+ flags: get_flags(conn.assigns[:current_user])
)
shared_link.site.locked ->
@@ -207,4 +209,10 @@ defmodule PlausibleWeb.StatsController do
end
defp shared_link_cookie_name(slug), do: "shared-link-" <> slug
+
+ defp get_flags(user) do
+ %{
+ custom_dimension_filter: FunWithFlags.enabled?(:custom_dimension_filter, for: user)
+ }
+ end
end
diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex
index 09abf4a44..7687dcf7c 100644
--- a/lib/plausible_web/router.ex
+++ b/lib/plausible_web/router.ex
@@ -41,12 +41,22 @@ defmodule PlausibleWeb.Router do
plug PlausibleWeb.Firewall
end
+ pipeline :mounted_apps do
+ plug :accepts, ["html"]
+ plug :put_secure_browser_headers
+ end
+
if Mix.env() == :dev do
forward "/sent-emails", Bamboo.SentEmailViewerPlug
end
use Kaffy.Routes, scope: "/crm", pipe_through: [PlausibleWeb.CRMAuthPlug]
+ scope path: "/feature-flags" do
+ pipe_through :mounted_apps
+ forward "/", FunWithFlags.UI.Router, namespace: "feature-flags"
+ end
+
scope "/api/stats", PlausibleWeb.Api do
pipe_through :internal_stats_api
diff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eex
index 76fb8bb7f..e8b05bb22 100644
--- a/lib/plausible_web/templates/stats/stats.html.eex
+++ b/lib/plausible_web/templates/stats/stats.html.eex
@@ -12,7 +12,7 @@
<% end %>
-
+
<%= if !@conn.assigns[:current_user] && @conn.assigns[:demo] do %>
diff --git a/mix.exs b/mix.exs
index f0ff44126..9b2547926 100644
--- a/mix.exs
+++ b/mix.exs
@@ -97,7 +97,9 @@ defmodule Plausible.MixProject do
{:hammer, "~> 6.0"},
{:public_suffix, git: "https://github.com/axelson/publicsuffix-elixir"},
{:floki, "~> 0.32.0", only: :test},
- {:referrer_blocklist, git: "https://github.com/plausible/referrer-blocklist.git"}
+ {:referrer_blocklist, git: "https://github.com/plausible/referrer-blocklist.git"},
+ {:fun_with_flags, "~> 1.8.1"},
+ {:fun_with_flags_ui, "~> 0.8"}
]
end
diff --git a/mix.lock b/mix.lock
index ccc0bae82..180ad0400 100644
--- a/mix.lock
+++ b/mix.lock
@@ -39,6 +39,8 @@
"excoveralls": {:hex, :excoveralls, "0.14.4", "295498f1ae47bdc6dce59af9a585c381e1aefc63298d48172efaaa90c3d251db", [:mix], [{:hackney, "~> 1.16", [hex: :hackney, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "e3ab02f2df4c1c7a519728a6f0a747e71d7d6e846020aae338173619217931c1"},
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
"floki": {:hex, :floki, "0.32.1", "dfe3b8db3b793939c264e6f785bca01753d17318d144bd44b407fb3493acaa87", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "d4b91c713e4a784a3f7b1e3cc016eefc619f6b1c3898464222867cafd3c681a3"},
+ "fun_with_flags": {:hex, :fun_with_flags, "1.8.1", "5999dd64d2e97646554244baa9b1da8ca33d656eed37307238b2dda8c17dba45", [:mix], [{:ecto_sql, "~> 3.0", [hex: :ecto_sql, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: true]}, {:redix, "~> 1.0", [hex: :redix, repo: "hexpm", optional: true]}], "hexpm", "02222ddffccbb1fa339f6ee3f14fac88ba44121b167c72bce93c2390013b3adc"},
+ "fun_with_flags_ui": {:hex, :fun_with_flags_ui, "0.8.0", "70587e344ba2035516a639e7bd8cb2ce8d54ee6c5f5dfd3e4e6f6a776e14ac1d", [:mix], [{:cowboy, ">= 2.0.0", [hex: :cowboy, repo: "hexpm", optional: true]}, {:fun_with_flags, "~> 1.8", [hex: :fun_with_flags, repo: "hexpm", optional: false]}, {:plug, "~> 1.12", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, ">= 2.0.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}], "hexpm", "95e1e5afae77e259a4a43f930f3da97ff3c388a72d4622f7848cb7ee34de6338"},
"gen_retry": {:hex, :gen_retry, "1.4.0", "682fadcb7ebf629f8dc4d84b4e62e03dce1baa526141133a19a8e7cee5ef7e1b", [:mix], [{:exconstructor, "~> 1.0", [hex: :exconstructor, repo: "hexpm", optional: false]}], "hexpm", "c15311afe0770f5fc58f4391b2ff3362fd25f1e107d02ff63592d36f30eaaae4"},
"gen_smtp": {:hex, :gen_smtp, "1.1.1", "bf9303c31735100631b1d708d629e4c65944319d1143b5c9952054f4a1311d85", [:rebar3], [{:hut, "1.3.0", [hex: :hut, repo: "hexpm", optional: false]}, {:ranch, ">= 1.7.0", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "51bc50cc017efd4a4248cbc39ea30fb60efa7d4a49688986fafad84434ff9ab7"},
"geolix": {:hex, :geolix, "1.1.0", "8b0fe847fef486d9e8b7c21eae6cbc2d998fb249e61d3f4f136f8016b9c1c833", [:mix], [{:poolboy, "~> 1.0", [hex: :poolboy, repo: "hexpm", optional: false]}], "hexpm", "980854f2aef30c288dc79e86c5267806d704c4525fde1b75de9a92f67fb16300"},
diff --git a/priv/repo/migrations/20220421074114_create_feature_flags_table.exs b/priv/repo/migrations/20220421074114_create_feature_flags_table.exs
new file mode 100644
index 000000000..2785fcf2f
--- /dev/null
+++ b/priv/repo/migrations/20220421074114_create_feature_flags_table.exs
@@ -0,0 +1,28 @@
+defmodule Plausible.Repo.Migrations.CreateFeatureFlagsTable do
+ use Ecto.Migration
+
+ # This migration assumes the default table name of "fun_with_flags_toggles"
+ # is being used. If you have overridden that via configuration, you should
+ # change this migration accordingly.
+
+ def up do
+ create table(:fun_with_flags_toggles, primary_key: false) do
+ add :id, :bigserial, primary_key: true
+ add :flag_name, :string, null: false
+ add :gate_type, :string, null: false
+ add :target, :string, null: false
+ add :enabled, :boolean, null: false
+ end
+
+ create index(
+ :fun_with_flags_toggles,
+ [:flag_name, :gate_type, :target],
+ unique: true,
+ name: "fwf_flag_name_gate_target_idx"
+ )
+ end
+
+ def down do
+ drop table(:fun_with_flags_toggles)
+ end
+end