Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add fun with flags library
Uku Taht7c1d64458ec8
diff --git a/config/runtime.exs b/config/runtime.exsindex 09007331d4..28bcc00869 100644--- a/config/runtime.exs+++ b/config/runtime.exs@@ -219,6 +219,12 @@ elsedatabase: 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.exindex 52a21dc5a1..b1c49b24b4 100644--- a/lib/plausible/auth/user.ex+++ b/lib/plausible/auth/user.ex@@ -4,6 +4,12 @@ defimpl Bamboo.Formatter, for: Plausible.Auth.User doendend+defimpl FunWithFlags.Actor, for: Plausible.Auth.User do+ def id(%{id: id}) do+ "user:#{id}"+ end+end+defmodule Plausible.Auth.GracePeriod douse Ecto.Schemadiff --git a/lib/plausible_web/controllers/stats_controller.ex b/lib/plausible_web/controllers/stats_controller.exindex 02acbf4349..a55901c67d 100644--- a/lib/plausible_web/controllers/stats_controller.ex+++ b/lib/plausible_web/controllers/stats_controller.ex@@ -25,7 +25,8 @@ defmodule PlausibleWeb.StatsController dostats_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 doshared_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 doenddefp shared_link_cookie_name(slug), do: "shared-link-" <> slug++ defp get_flags(user) do+ %{+ custom_dimension_filter: FunWithFlags.enabled?(:custom_dimension_filter, for: user)+ }+ endenddiff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.exindex 09abf4a446..7687dcf7c2 100644--- a/lib/plausible_web/router.ex+++ b/lib/plausible_web/router.ex@@ -41,12 +41,22 @@ defmodule PlausibleWeb.Router doplug PlausibleWeb.Firewallend+ pipeline :mounted_apps do+ plug :accepts, ["html"]+ plug :put_secure_browser_headers+ end+if Mix.env() == :dev doforward "/sent-emails", Bamboo.SentEmailViewerPlugenduse 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 dopipe_through :internal_stats_apidiff --git a/lib/plausible_web/templates/stats/stats.html.eex b/lib/plausible_web/templates/stats/stats.html.eexindex 76fb8bb7fc..e8b05bb228 100644--- a/lib/plausible_web/templates/stats/stats.html.eex+++ b/lib/plausible_web/templates/stats/stats.html.eex@@ -12,7 +12,7 @@<% end %><div class="pt-6"></div>- <div id="stats-react-container" style="overflow-anchor: none;" data-domain="<%= @site.domain %>" data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= [email protected][:current_user] %>" data-stats-begin="<%= @stats_start_date %>" data-shared-link-auth="<%= assigns[:shared_link_auth] %>" data-embedded="<%= @conn.assigns[:embedded] %>" data-background="<%= @conn.assigns[:background] %>" data-selfhosted="<%= Application.get_env(:plausible, :is_selfhost) %>" data-current-user-role="<%= @conn.assigns[:current_user_role] %>"></div>+ <div id="stats-react-container" style="overflow-anchor: none;" data-domain="<%= @site.domain %>" data-offset="<%= Timex.Timezone.total_offset(Timex.Timezone.get(@site.timezone)) %>" data-has-goals="<%= @has_goals %>" data-logged-in="<%= [email protected][:current_user] %>" data-stats-begin="<%= @stats_start_date %>" data-shared-link-auth="<%= assigns[:shared_link_auth] %>" data-embedded="<%= @conn.assigns[:embedded] %>" data-background="<%= @conn.assigns[:background] %>" data-selfhosted="<%= Application.get_env(:plausible, :is_selfhost) %>" data-current-user-role="<%= @conn.assigns[:current_user_role] %>" data-flags="<%= Jason.encode!(@flags) %>"></div><div id="modal_root"></div><%= if [email protected][:current_user] && @conn.assigns[:demo] do %><div class="bg-gray-50 dark:bg-gray-850">diff --git a/mix.exs b/mix.exsindex f0ff441267..9b25479269 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"}]enddiff --git a/mix.lock b/mix.lockindex ccc0bae82f..180ad04008 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.exsnew file mode 100644index 0000000000..2785fcf2f4--- /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