From 28fb2014e2f2a6bd1a46e387e21097fb0a9b405e Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Tue, 21 May 2024 12:40:27 +0200 Subject: [PATCH] Put verification behind a feature flag/env setting --- config/.env.dev | 2 + config/runtime.exs | 5 ++ lib/plausible/verification.ex | 10 ++++ lib/plausible/verification/diagnostics.ex | 9 ++- .../templates/site/settings_general.html.heex | 2 +- .../templates/site/snippet.html.heex | 13 ++++- .../stats/waiting_first_pageview.html.heex | 55 ++++++++++++++++--- test/test_helper.exs | 2 + 8 files changed, 87 insertions(+), 11 deletions(-) diff --git a/config/.env.dev b/config/.env.dev index 58e4ca827d15c..d7625ed806792 100644 --- a/config/.env.dev +++ b/config/.env.dev @@ -28,3 +28,5 @@ S3_REGION=us-east-1 S3_ENDPOINT=http://localhost:10000 S3_EXPORTS_BUCKET=dev-exports S3_IMPORTS_BUCKET=dev-imports + +VERIFICATION_ENABLED=true diff --git a/config/runtime.exs b/config/runtime.exs index cdb9d44f0120b..fd1eee2185247 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -701,6 +701,11 @@ config :plausible, Plausible.PromEx, grafana: :disabled, metrics_server: :disabled +config :plausible, Plausible.Verification, + enabled?: + get_var_from_path_or_env(config_dir, "VERIFICATION_ENABLED", "false") + |> String.to_existing_atom() + config :plausible, Plausible.Verification.Checks.Installation, token: get_var_from_path_or_env(config_dir, "BROWSERLESS_TOKEN", "dummy_token"), endpoint: get_var_from_path_or_env(config_dir, "BROWSERLESS_ENDPOINT", "http://0.0.0.0:3000") diff --git a/lib/plausible/verification.ex b/lib/plausible/verification.ex index 35bd85881d205..aa5771916fb13 100644 --- a/lib/plausible/verification.ex +++ b/lib/plausible/verification.ex @@ -4,6 +4,16 @@ defmodule Plausible.Verification do """ use Plausible + @feature_flag :verification + + def enabled?(user) do + enabled_via_config? = + :plausible |> Application.get_env(__MODULE__) |> Keyword.fetch!(:enabled?) + + enabled_for_user? = not is_nil(user) and FunWithFlags.enabled?(@feature_flag, for: user) + enabled_via_config? or enabled_for_user? + end + on_ee do def user_agent() do "Plausible Verification Agent - if abused, contact support@plausible.io" diff --git a/lib/plausible/verification/diagnostics.ex b/lib/plausible/verification/diagnostics.ex index 68fd06a62b300..07e2c332459e3 100644 --- a/lib/plausible/verification/diagnostics.ex +++ b/lib/plausible/verification/diagnostics.ex @@ -55,7 +55,14 @@ defmodule Plausible.Verification.Diagnostics do } end - def rate(%__MODULE__{plausible_installed?: false, snippets_found_in_head: 1, disallowed_via_csp?: true}, _url) do + def rate( + %__MODULE__{ + plausible_installed?: false, + snippets_found_in_head: 1, + disallowed_via_csp?: true + }, + _url + ) do %Rating{ ok?: false, errors: ["We encountered an issue with your site's CSP"], diff --git a/lib/plausible_web/templates/site/settings_general.html.heex b/lib/plausible_web/templates/site/settings_general.html.heex index a22eb43913ea0..a561d24254149 100644 --- a/lib/plausible_web/templates/site/settings_general.html.heex +++ b/lib/plausible_web/templates/site/settings_general.html.heex @@ -109,7 +109,7 @@ -
+
<%= live_render(@conn, PlausibleWeb.Live.Verification, session: %{ "site_id" => @site.id, diff --git a/lib/plausible_web/templates/site/snippet.html.heex b/lib/plausible_web/templates/site/snippet.html.heex index ff165d93046c9..729962354dc72 100644 --- a/lib/plausible_web/templates/site/snippet.html.heex +++ b/lib/plausible_web/templates/site/snippet.html.heex @@ -7,10 +7,13 @@ <%= form_for @conn, "/", [class: "max-w-lg w-full mx-auto bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-8"], fn f -> %>

Add JavaScript snippet

-

+

Include this snippet in the <head> section of your website.
To verify your integration, click the button below to confirm that everything is working correctly.

+

+ Paste this snippet in the <head> of your website. +

<%= textarea(f, :domain, @@ -61,7 +64,13 @@

- <%= link("Verify your integration to start collecting data →", + <% button_label = + if Plausible.Verification.enabled?(@current_user) do + "Verify your integration to start collecting data →" + else + "Start collecting data →" + end %> + <%= link(button_label, class: "button mt-4 w-full", to: "/#{URI.encode_www_form(@site.domain)}" ) %> diff --git a/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex b/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex index 7efd290354478..78b0a14fc0bf0 100644 --- a/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex +++ b/lib/plausible_web/templates/stats/waiting_first_pageview.html.heex @@ -21,12 +21,53 @@

This dashboard is actually locked. You are viewing it with super-admin access

<% 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 + - <%= live_render(@conn, PlausibleWeb.Live.Verification, - session: %{ - "site_id" => @site.id, - "domain" => @site.domain, - "slowdown" => @conn.private[:verification_slowdown] - } - ) %> +
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 + + +
+ + Check the + <.styled_link href={Routes.site_path(@conn, :settings_general, @site.domain)}> + site settings + + to invite team members,
import historical stats and more. +
+ + Still not working? Ask on our + <.styled_link new_tab href="https://github.com/plausible/analytics/discussions"> + community-supported forum + + +

+
+
+ + <%= if Plausible.Verification.enabled?(assigns[:current_user]), + do: + live_render(@conn, PlausibleWeb.Live.Verification, + session: %{ + "site_id" => @site.id, + "domain" => @site.domain, + "slowdown" => @conn.private[:verification_slowdown] + } + ) %>
diff --git a/test/test_helper.exs b/test/test_helper.exs index 9b1b07f7a8f75..d9ec1e60a1514 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -1,3 +1,5 @@ +FunWithFlags.enable(:verification) + if not Enum.empty?(Path.wildcard("lib/**/*_test.exs")) do raise "Oops, test(s) found in `lib/` directory. Move them to `test/`." end