Skip to content

Commit

Permalink
Put verification behind a feature flag/env setting
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed May 21, 2024
1 parent 5d288db commit 28fb201
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 11 deletions.
2 changes: 2 additions & 0 deletions config/.env.dev
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 10 additions & 0 deletions lib/plausible/verification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 [email protected]"
Expand Down
9 changes: 8 additions & 1 deletion lib/plausible/verification/diagnostics.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
</a>
</div>

<div>
<div :if={Plausible.Verification.enabled?(@current_user)}>
<%= live_render(@conn, PlausibleWeb.Live.Verification,
session: %{
"site_id" => @site.id,
Expand Down
13 changes: 11 additions & 2 deletions lib/plausible_web/templates/site/snippet.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -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 -> %>
<h2 class="text-xl font-bold dark:text-gray-100">Add JavaScript snippet</h2>
<div class="mt-4">
<p class="dark:text-gray-100">
<p :if={Plausible.Verification.enabled?(@current_user)} class="dark:text-gray-100">
Include this snippet in the <code>&lt;head&gt;</code>
section of your website.<br />To verify your integration, click the button below to confirm that everything is working correctly.
</p>
<p :if={not Plausible.Verification.enabled?(@current_user)} class="dark:text-gray-100">
Paste this snippet in the <code>&lt;head&gt;</code> of your website.
</p>

<div class="relative">
<%= textarea(f, :domain,
Expand Down Expand Up @@ -61,7 +64,13 @@
</.styled_link>
</p>
</div>
<%= 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)}"
) %>
Expand Down
55 changes: 48 additions & 7 deletions lib/plausible_web/templates/stats/waiting_first_pageview.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,53 @@
<p>This dashboard is actually locked. You are viewing it with super-admin access</p>
</div>
<% end %>
<div
:if={not Plausible.Verification.enabled?(assigns[:current_user])}
class="bg-white dark:bg-gray-800 shadow-md rounded px-8 pt-6 pb-8 mb-4 mt-16 relative text-center"
>
<h2 class="text-xl font-bold dark:text-gray-100">Waiting for first pageview</h2>
<h2 class="text-xl font-bold dark:text-gray-100">on <%= @site.domain %></h2>
<div class="my-44">
<div class="block pulsating-circle top-1/2 left-1/2"></div>
<p class="text-gray-600 dark:text-gray-400 text-xs absolute left-0 bottom-0 mb-6 w-full text-center leading-normal">
Need to see the snippet again?
<.styled_link href={"/#{URI.encode_www_form(@site.domain)}/snippet"}>
Click here
</.styled_link>

<%= live_render(@conn, PlausibleWeb.Live.Verification,
session: %{
"site_id" => @site.id,
"domain" => @site.domain,
"slowdown" => @conn.private[:verification_slowdown]
}
) %>
<br /> 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
</.styled_link>

<br />
<span :if={ee?()}>
Check the
<.styled_link href={Routes.site_path(@conn, :settings_general, @site.domain)}>
site settings
</.styled_link>
to invite team members, <br /> import historical stats and more.
</span>
<span :if={ce?()}>
Still not working? Ask on our
<.styled_link new_tab href="https://github.com/plausible/analytics/discussions">
community-supported forum
</.styled_link>
</span>
</p>
</div>
</div>

<%= 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]
}
) %>
</div>
2 changes: 2 additions & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 28fb201

Please sign in to comment.