Skip to content

Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.

Verification v2 (#5549)

RobertJoonasb76996b3a465
diff --git a/.dockerignore b/.dockerignore
index 8d917ba063..1181cba96b 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -59,6 +59,7 @@ npm-debug.log
# Auto-generated tracker files
/priv/tracker/js/*.js
+/priv/tracker/verifier/
# Dializer
/priv/plts/*.plt
diff --git a/.github/workflows/elixir.yml b/.github/workflows/elixir.yml
index d2c418668b..d63f2f2aa3 100644
--- a/.github/workflows/elixir.yml
+++ b/.github/workflows/elixir.yml
@@ -11,7 +11,7 @@ concurrency:
cancel-in-progress: true
env:
- CACHE_VERSION: v14
+ CACHE_VERSION: v15
PERSISTENT_CACHE_DIR: cached
jobs:
@@ -81,6 +81,7 @@ jobs:
_build
tracker/node_modules
priv/tracker/js
+ priv/tracker/verifier
${{ env.PERSISTENT_CACHE_DIR }}
key: ${{ env.MIX_ENV }}-${{ env.CACHE_VERSION }}-${{ github.head_ref || github.ref }}-${{ hashFiles('**/mix.lock') }}
restore-keys: |
@@ -94,9 +95,9 @@ jobs:
filters: |
tracker:
- 'tracker/**'
- - name: Check if priv/tracker/js/plausible-web.js exists
+ - name: Check if tracker and verifier are built already
run: |
- if [ -f priv/tracker/js/plausible-web.js ]; then
+ if [ -f priv/tracker/js/plausible-web.js ] && [ -f priv/tracker/verifier/verifier-v1.js ]; then
echo "HAS_BUILT_TRACKER=true" >> $GITHUB_ENV
else
echo "HAS_BUILT_TRACKER=false" >> $GITHUB_ENV
diff --git a/.gitignore b/.gitignore
index 9f55635c42..6583ec1f62 100644
--- a/.gitignore
+++ b/.gitignore
@@ -92,6 +92,7 @@ plausible-report.xml
# Auto-generated tracker files
/priv/tracker/js/plausible*.js*
+/priv/tracker/verifier/*.js
# Docker volumes
.clickhouse_db_vol*
diff --git a/lib/plausible/telemetry/plausible_metrics.ex b/lib/plausible/telemetry/plausible_metrics.ex
index 84ac365f57..ba9b4d7fdc 100644
--- a/lib/plausible/telemetry/plausible_metrics.ex
+++ b/lib/plausible/telemetry/plausible_metrics.ex
@@ -121,6 +121,14 @@ defmodule Plausible.PromEx.Plugins.PlausibleMetrics do
event_name: PlausibleWeb.TrackerPlug.telemetry_event(:legacy),
tags: [:status],
tag_values: &%{status: &1.status}
+ ),
+ counter(
+ metric_prefix ++ [:verification, :js_elixir_diff],
+ event_name: Plausible.Verification.Checks.Installation.telemetry_event(_diff = true)
+ ),
+ counter(
+ metric_prefix ++ [:verification, :js_elixir_match],
+ event_name: Plausible.Verification.Checks.Installation.telemetry_event(_diff = false)
)
]
)
diff --git a/lib/plausible/verification/checks/installation.ex b/lib/plausible/verification/checks/installation.ex
index 0776afeaac..93182f5b85 100644
--- a/lib/plausible/verification/checks/installation.ex
+++ b/lib/plausible/verification/checks/installation.ex
@@ -1,20 +1,67 @@
defmodule Plausible.Verification.Checks.Installation do
require Logger
- @verification_script_filename "verification/verify_plausible_installed.js"
- @verification_script_path Path.join(:code.priv_dir(:plausible), @verification_script_filename)
- @external_resource @verification_script_path
- @code File.read!(@verification_script_path)
+ path = Application.app_dir(:plausible, "priv/tracker/verifier/verifier-v1.js")
+ # On CI, the file might not be present for static checks so we create an empty one
+ File.touch!(path)
+
+ @verifier_code File.read!(path)
+ @external_resource "priv/tracker/verifier/verifier-v1.js"
+
+ # Puppeteer wrapper function that executes the vanilla JS verifier code
+ @puppeteer_wrapper_code """
+ export default async function({ page, context }) {
+ try {
+ await page.setUserAgent(context.userAgent);
+ await page.goto(context.url);
+
+ await page.evaluate(() => {
+ #{@verifier_code}
+ });
+
+ return await page.evaluate(async (expectedDataDomain, debug) => {
+ return await window.verifyPlausibleInstallation(expectedDataDomain, debug);
+ }, context.expectedDataDomain, context.debug);
+ } catch (error) {
+ const msg = error.message ? error.message : JSON.stringify(error)
+ return {data: {completed: false, error: msg}}
+ }
+ }
+ """
@moduledoc """
Calls the browserless.io service (local instance can be spawned with `make browserless`)
- and runs #{@verification_script_filename} via the [function API](https://docs.browserless.io/HTTP-APIs/function).
+ and runs verifier script via the [function API](https://docs.browserless.io/HTTP-APIs/function).
+
+ The verification uses a vanilla JS script that runs in the browser context,
+ performing a comprehensive Plausible installation verification. Providing
+ the following information:
+
+ - `data.snippetsFoundInHead` - plausible snippets found in <head>
+
+ - `data.snippetsFoundInBody` - plausible snippets found in <body>
+
+ - `data.plausibleInstalled` - whether or not the `plausible()` window function was found
+
+ - `data.callbackStatus` - integer. 202 indicates that the server acknowledged the test event.
+ The test event ingestion is discarded based on user-agent, see:
+ `Plausible.Verification.user_agent/0`
+
+ - `data.dataDomainMismatch` - whether or not script[data-domain] mismatched with site.domain
+
+ - `data.proxyLikely` - whether the script[src] is not a plausible.io URL
+
+ - `data.manualScriptExtension` - whether the site is using script.manual.js
- The successful execution assumes the following JSON payload:
- - `data.plausibleInstalled` - boolean indicating whether the `plausible()` window function was found
- - `data.callbackStatus` - integer. 202 indicates that the server acknowledged the test event.
+ - `data.unknownAttributes` - whether the script tag has any unknown attributes
- The test event ingestion is discarded based on user-agent, see: `Plausible.Verification.user_agent/0`
+ - `data.wordpressPlugin` - whether or not there's a `<meta>` tag with the WP plugin version
+
+ - `data.wordpressLikely` - whether or not the site is built on WordPress
+
+ - `data.gtmLikely` - whether or not the site uses GTM
+
+ - `data.cookieBannerLikely` - whether or not there's a cookie banner blocking Plausible
"""
use Plausible.Verification.Check
@@ -22,13 +69,14 @@ defmodule Plausible.Verification.Checks.Installation do
def report_progress_as, do: "We're verifying that your visitors are being counted correctly"
@impl true
- def perform(%State{url: url} = state) do
+ def perform(%State{url: url, data_domain: data_domain} = state) do
opts = [
headers: %{content_type: "application/json"},
body:
Jason.encode!(%{
- code: @code,
+ code: @puppeteer_wrapper_code,
context: %{
+ expectedDataDomain: data_domain,
url: Plausible.Verification.URL.bust_url(url),
userAgent: Plausible.Verification.user_agent(),
debug: Application.get_env(:plausible, :environment) == "dev"
@@ -43,30 +91,69 @@ defmodule Plausible.Verification.Checks.Installation do
opts = Keyword.merge(opts, extra_opts)
case Req.post(verification_endpoint(), opts) do
- {:ok,
- %{
- status: 200,
- body: %{
- "data" =>
- %{"plausibleInstalled" => installed?, "callbackStatus" => callback_status} = data
- }
- }}
- when is_boolean(installed?) ->
- if data["error"] do
- Logger.warning("Browserless error: #{Map.get(data, "error")}")
- end
+ {:ok, %{status: 200, body: %{"data" => %{"completed" => true} = js_data}}} ->
+ emit_telemetry_and_log(state.diagnostics, js_data, data_domain)
- put_diagnostics(state, plausible_installed?: installed?, callback_status: callback_status)
+ put_diagnostics(state,
+ plausible_installed?: js_data["plausibleInstalled"],
+ callback_status: js_data["callbackStatus"]
+ )
- {:ok, %{status: status}} ->
+ {:ok, %{status: status, body: %{"data" => %{"error" => error}}}} ->
+ Logger.warning("[VERIFICATION] Browserless JS error: #{inspect(error)}")
put_diagnostics(state, plausible_installed?: false, service_error: status)
{:error, %{reason: reason}} ->
- Logger.warning("Browserless error: #{inspect(reason)}")
+ Logger.warning("[VERIFICATION] Browserless request error: #{inspect(reason)}")
put_diagnostics(state, plausible_installed?: false, service_error: reason)
end
end
+ def telemetry_event(true = _diff), do: [:plausible, :verification, :js_elixir_diff]
+ def telemetry_event(false = _diff), do: [:plausible, :verification, :js_elixir_match]
+
+ def emit_telemetry_and_log(elixir_data, js_data, data_domain) do
+ diffs =
+ for {diff, elixir_diagnostic, js_diagnostic} <- [
+ {:data_domain_mismatch_diff, :data_domain_mismatch?, "dataDomainMismatch"},
+ {:proxy_likely_diff, :proxy_likely?, "proxyLikely"},
+ {:manual_script_extension_diff, :manual_script_extension?, "manualScriptExtension"},
+ {:unknown_attributes_diff, :snippet_unknown_attributes?, "unknownAttributes"},
+ {:wordpress_plugin_diff, :wordpress_plugin?, "wordpressPlugin"},
+ {:wordpress_likely_diff, :wordpress_likely?, "wordpressLikely"},
+ {:gtm_likely_diff, :gtm_likely?, "gtmLikely"},
+ {:cookie_banner_likely_diff, :cookie_banner_likely?, "cookieBannerLikely"}
+ ] do
+ case {Map.get(elixir_data, elixir_diagnostic), js_data[js_diagnostic]} do
+ {true, false} -> {diff, -1}
+ {false, true} -> {diff, 1}
+ {_, _} -> {diff, 0}
+ end
+ end
+ |> Map.new()
+ |> Map.merge(%{
+ snippets_head_diff: js_data["snippetsFoundInHead"] - elixir_data.snippets_found_in_head,
+ snippets_body_diff: js_data["snippetsFoundInBody"] - elixir_data.snippets_found_in_body
+ })
+ |> Map.reject(fn {_k, v} -> v == 0 end)
+
+ any_diff? = map_size(diffs) > 0
+
+ if any_diff? do
+ info =
+ %{
+ domain: data_domain,
+ plausible_installed_js: js_data["plausibleInstalled"],
+ callback_status_js: js_data["callbackStatus"]
+ }
+ |> Map.merge(diffs)
+
+ Logger.info("[VERIFICATION] js_elixir_diff: #{inspect(info)}")
+ end
+
+ :telemetry.execute(telemetry_event(any_diff?), %{})
+ end
+
defp verification_endpoint() do
config = Application.fetch_env!(:plausible, __MODULE__)
token = Keyword.fetch!(config, :token)
diff --git a/priv/tracker/verifier/.gitkeep b/priv/tracker/verifier/.gitkeep
new file mode 100644
index 0000000000..e69de29bb2
diff --git a/test/plausible/site/verification/checks_test.exs b/test/plausible/site/verification/checks_test.exs
index f7108f535a..32267e6a3b 100644
--- a/test/plausible/site/verification/checks_test.exs
+++ b/test/plausible/site/verification/checks_test.exs
@@ -918,7 +918,15 @@ defmodule Plausible.Verification.ChecksTest do
end
defp plausible_installed(bool \\ true, callback_status \\ 202) do
- %{"data" => %{"plausibleInstalled" => bool, "callbackStatus" => callback_status}}
+ %{
+ "data" => %{
+ "completed" => true,
+ "snippetsFoundInHead" => 0,
+ "snippetsFoundInBody" => 0,
+ "plausibleInstalled" => bool,
+ "callbackStatus" => callback_status
+ }
+ }
end
defp refute_unhandled(interpretation) do
diff --git a/test/plausible_web/live/verification_test.exs b/test/plausible_web/live/verification_test.exs
index c8443e2e10..9e3554fcc3 100644
--- a/test/plausible_web/live/verification_test.exs
+++ b/test/plausible_web/live/verification_test.exs
@@ -208,7 +208,15 @@ defmodule PlausibleWeb.Live.VerificationTest do
end
defp plausible_installed(bool \\ true, callback_status \\ 202) do
- %{"data" => %{"plausibleInstalled" => bool, "callbackStatus" => callback_status}}
+ %{
+ "data" => %{
+ "completed" => true,
+ "snippetsFoundInHead" => 0,
+ "snippetsFoundInBody" => 0,
+ "plausibleInstalled" => bool,
+ "callbackStatus" => callback_status
+ }
+ }
end
defp source(domain) do
diff --git a/tracker/compiler/index.js b/tracker/compiler/index.js
index ffbd51c20f..226f528fce 100644
--- a/tracker/compiler/index.js
+++ b/tracker/compiler/index.js
@@ -63,7 +63,13 @@ export async function compileAll(options = {}) {
export async function compileFile(variant, options) {
const globals = { ...DEFAULT_GLOBALS, ...variant.globals }
- let code = options.bundledCode || await bundleCode()
+ let code
+
+ if (variant.entry_point) {
+ code = await bundleCode(variant.entry_point)
+ } else {
+ code = options.bundledCode || await bundleCode()
+ }
if (!variant.npm_package) {
code = wrapInstantlyEvaluatingFunction(code)
@@ -101,18 +107,20 @@ export function compileWebSnippet() {
`
}
-async function bundleCode(format = 'esm') {
+async function bundleCode(entryPoint = 'src/plausible.js') {
const bundle = await rollup({
- input: 'src/plausible.js',
+ input: entryPoint,
})
- const { output } = await bundle.generate({ format })
+ const { output } = await bundle.generate({ format: 'esm' })
return output[0].code
}
function outputPath(variant, options) {
- if (variant.npm_package) {
+ if (variant.output_path) {
+ return relPath(`../../${variant.output_path}${options.suffix || ""}`)
+ } else if (variant.npm_package) {
return relPath(`../${variant.name}${options.suffix || ""}`)
} else {
return relPath(`../../priv/tracker/js/${variant.name}${options.suffix || ""}`)
diff --git a/tracker/compiler/variants.json b/tracker/compiler/variants.json
index c0e51b502c..8cec87c7d3 100644
--- a/tracker/compiler/variants.json
+++ b/tracker/compiler/variants.json
@@ -30,6 +30,12 @@
"COMPILE_REVENUE": true
},
"npm_package": "esm"
+ },
+ {
+ "name": "verifier-v1.js",
+ "entry_point": "verifier/verifier-v1.js",
+ "output_path": "priv/tracker/verifier/verifier-v1.js",
+ "globals": {}
}
],
"legacyVariants": [
diff --git a/tracker/playwright.config.js b/tracker/playwright.config.js
index 7288966320..0fbfc1c5e9 100644
--- a/tracker/playwright.config.js
+++ b/tracker/playwright.config.js
@@ -25,11 +25,13 @@ export default defineConfig({
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
+ testIgnore: 'test/verifier/**',
},
{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
+ testIgnore: 'test/verifier/**',
},
],
webServer: {
diff --git a/tracker/test/support/initialize-page-dynamically.ts b/tracker/test/support/initialize-page-dynamically.ts
index 855a22aafb..493c585bf4 100644
--- a/tracker/test/support/initialize-page-dynamically.ts
+++ b/tracker/test/support/initialize-page-dynamically.ts
@@ -2,14 +2,23 @@ import { Page } from '@playwright/test'
import { ScriptConfig } from './types'
import { compileWebSnippet } from '../../compiler'
-interface DynamicPageOptions {
+interface SharedOptions {
+ /** unique ID that becomes part of the dynamic page URL */
+ testId: string
+ /** optional path to append to the dynamic page URL */
+ path?: string
+}
+
+interface TemplatedResponse {
/** string like `<script defer id="plausible" src="/plausible.compat.local.js"></script>` or ScriptConfig to be set to web snippet */
scriptConfig: ScriptConfig | string
/** vanilla HTML string, which can contain JS, will be set in the body of the page */
bodyContent: string
- testId: string
- /** optional path to append to the dynamic page URL */
- path?: string
+}
+
+interface FullResponse {
+ // Full html response
+ response: string
}
interface DynamicPageInfo {
@@ -55,16 +64,23 @@ function getConfiguredPlausibleWebSnippet({
export async function initializePageDynamically(
page: Page,
- { testId, scriptConfig, bodyContent, path = '' }: DynamicPageOptions
+ options: SharedOptions & (TemplatedResponse | FullResponse)
): Promise<DynamicPageInfo> {
- const url = `/dynamic/${testId}${path}`
+ const url = `/dynamic/${options.testId}${options.path || ''}`
await page.context().route(url, async (route) => {
- const responseBody = RESPONSE_BODY_TEMPLATE.replace(
- '<script>// Plausible script</script>',
- typeof scriptConfig === 'string'
- ? scriptConfig
- : getConfiguredPlausibleWebSnippet(scriptConfig)
- ).replace('<body></body>', `<body>${bodyContent}</body>`)
+ let responseBody: string
+
+ if ('response' in options) {
+ responseBody = options.response
+ } else {
+ responseBody = RESPONSE_BODY_TEMPLATE.replace(
+ '<script>// Plausible script</script>',
+ typeof options.scriptConfig === 'string'
+ ? options.scriptConfig
+ : getConfiguredPlausibleWebSnippet(options.scriptConfig)
+ ).replace('<body></body>', `<body>${options.bodyContent}</body>`)
+ }
+
await route.fulfill({
body: responseBody,
contentType: 'text/html'
diff --git a/tracker/test/support/verify-playwright-wrapper.js b/tracker/test/support/verify-playwright-wrapper.js
new file mode 100644
index 0000000000..796d214d75
--- /dev/null
+++ b/tracker/test/support/verify-playwright-wrapper.js
@@ -0,0 +1,18 @@
+import { compileFile } from '../../compiler/index.js'
+import variantsFile from '../../compiler/variants.json' with { type: 'json' }
+
+const VARIANT = variantsFile.manualVariants.find(variant => variant.name === 'verifier-v1.js')
+
+export default async function verify(page, context) {
+ const {url, expectedDataDomain} = context
+ const debug = context.debug ? true : false
+
+ const verifierCode = await compileFile(VARIANT, { returnCode: true })
+
+ await page.goto(url)
+ await page.evaluate(verifierCode)
+
+ return await page.evaluate(async ({expectedDataDomain, debug}) => {
+ return await window.verifyPlausibleInstallation(expectedDataDomain, debug)
+ }, {expectedDataDomain, debug})
+}
diff --git a/tracker/test/verifier/check-data-domain-mismatch.spec.js b/tracker/test/verifier/check-data-domain-mismatch.spec.js
new file mode 100644
index 0000000000..b1051f179c
--- /dev/null
+++ b/tracker/test/verifier/check-data-domain-mismatch.spec.js
@@ -0,0 +1,70 @@
+import { test, expect } from '@playwright/test'
+import { checkDataDomainMismatch } from '../../verifier/check-data-domain-mismatch'
+
+function mockSnippet(dataDomain) {
+ return { getAttribute: _ => dataDomain }
+}
+
+test.describe('checkDataDomainMismatch', () => {
+ test('returns false when no snippets provided', () => {
+ expect(checkDataDomainMismatch([], 'example.com')).toBe(false)
+ expect(checkDataDomainMismatch(null, 'example.com')).toBe(false)
+ expect(checkDataDomainMismatch(undefined, 'example.com')).toBe(false)
+ })
+
+ test('handles empty data-domain attribute', () => {
+ const snippets = [mockSnippet('')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(true)
+ })
+
+ test('returns false when snippet data-domain matches expected domain', () => {
+ const snippets = [mockSnippet('example.com')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+
+ test('returns true when snippet data-domain does not match expected domain', () => {
+ const snippets = [mockSnippet('wrong.com')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(true)
+ })
+
+ test('allows www. in data-domain', () => {
+ const snippets = [mockSnippet('www.example.com')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+
+ test('handles multiple domains in data-domain attribute', () => {
+ const snippets = [mockSnippet('example.org,example.com,example.net')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+
+ test('handles multiple domains with spaces in data-domain attribute', () => {
+ const snippets = [mockSnippet('example.org, example.com, example.net')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+
+ test('handles multiple domains with www prefix', () => {
+ const snippets = [mockSnippet('www.example.org, www.example.com, www.example.net')]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+
+ test('returns true when expected domain not in multi-domain list', () => {
+ const snippets = [mockSnippet('example.org,example.com,example.net')]
+ expect(checkDataDomainMismatch(snippets, 'example.typo')).toBe(true)
+ })
+
+ test('handles multiple snippets - returns true if any snippet has domain mismatch', () => {
+ const snippets = [
+ mockSnippet('example.com'),
+ mockSnippet('wrong.com')
+ ]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(true)
+ })
+
+ test('handles multiple snippets - returns false if all snippets match', () => {
+ const snippets = [
+ mockSnippet('example.com'),
+ mockSnippet('example.org,example.com,example.net')
+ ]
+ expect(checkDataDomainMismatch(snippets, 'example.com')).toBe(false)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/check-gtm.spec.js b/tracker/test/verifier/check-gtm.spec.js
new file mode 100644
index 0000000000..40973bf8d8
--- /dev/null
+++ b/tracker/test/verifier/check-gtm.spec.js
@@ -0,0 +1,29 @@
+import { test, expect } from '@playwright/test'
+import { checkGTM } from '../../verifier/check-gtm'
+
+function mockDocument(html) {
+ return {
+ documentElement: {outerHTML: `<html>${html}</html>`}
+ }
+}
+
+test.describe('checkGTM (gtmLikely)', () => {
+ test('handles document undefined', () => {
+ expect(checkGTM(undefined)).toBe(false)
+ })
+
+ test('handles document.documentElement undefined', () => {
+ const document = {documentElement: undefined}
+ expect(checkGTM(document)).toBe(false)
+ })
+
+ test('false when no GTM indicators present', () => {
+ const document = mockDocument('<head></head><body></body>')
+ expect(checkGTM(document)).toBe(false)
+ })
+
+ test('detects gtmLikely by googletagmanager.com/gtm.js signature', () => {
+ const document = mockDocument('<head><script src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXX"></script></head>')
+ expect(checkGTM(document)).toBe(true)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/check-manual-extension.spec.js b/tracker/test/verifier/check-manual-extension.spec.js
new file mode 100644
index 0000000000..e3a85edc43
--- /dev/null
+++ b/tracker/test/verifier/check-manual-extension.spec.js
@@ -0,0 +1,45 @@
+import { test, expect } from '@playwright/test'
+import { checkManualExtension } from '../../verifier/check-manual-extension'
+
+function mockSnippet(dataDomain) {
+ return { getAttribute: _ => dataDomain }
+}
+
+test.describe('checkManualExtension', () => {
+ test('returns false when no snippets provided', () => {
+ expect(checkManualExtension([])).toBe(false)
+ expect(checkManualExtension(null)).toBe(false)
+ expect(checkManualExtension(undefined)).toBe(false)
+ })
+
+ test('handles empty src attribute', () => {
+ const snippets = [mockSnippet('')]
+ expect(checkManualExtension(snippets)).toBe(false)
+ })
+
+ test('returns true when snippet src includes manual', () => {
+ const snippets = [mockSnippet('https://plausible.io/js/script.manual.js')]
+ expect(checkManualExtension(snippets)).toBe(true)
+ })
+
+ test('returns false when snippet src does not include manual', () => {
+ const snippets = [mockSnippet('https://plausible.io/js/script.js')]
+ expect(checkManualExtension(snippets)).toBe(false)
+ })
+
+ test('handles multiple snippets - returns true if any src includes manual', () => {
+ const snippets = [
+ mockSnippet('https://plausible.io/js/plausible.manual.js'),
+ mockSnippet('https://plausible.io/js/plausible.js')
+ ]
+ expect(checkManualExtension(snippets)).toBe(true)
+ })
+
+ test('handles multiple snippets - returns false if no manual snippets', () => {
+ const snippets = [
+ mockSnippet('https://plausible.io/js/plausible.outbound-links.js'),
+ mockSnippet('https://plausible.io/js/plausible.compat.js')
+ ]
+ expect(checkManualExtension(snippets)).toBe(false)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/check-proxy-likely.spec.js b/tracker/test/verifier/check-proxy-likely.spec.js
new file mode 100644
index 0000000000..460f28b82d
--- /dev/null
+++ b/tracker/test/verifier/check-proxy-likely.spec.js
@@ -0,0 +1,60 @@
+import { test, expect } from '@playwright/test'
+import { checkProxyLikely } from '../../verifier/check-proxy-likely'
+
+function mockSnippet(src) {
+ return { getAttribute: _ => src }
+}
+
+test.describe('checkProxyLikely', () => {
+ test('returns false when no snippets provided', () => {
+ expect(checkProxyLikely([])).toBe(false)
+ expect(checkProxyLikely(null)).toBe(false)
+ expect(checkProxyLikely(undefined)).toBe(false)
+ })
+
+ test('handles empty src attribute', () => {
+ const snippets = [mockSnippet('')]
+ expect(checkProxyLikely(snippets)).toBe(false)
+ })
+
+ test('returns false when snippet src is official plausible.io URL', () => {
+ const snippets = [mockSnippet('https://plausible.io/js/plausible.js')]
+ expect(checkProxyLikely(snippets)).toBe(false)
+ })
+
+ test('returns false when snippet src is official plausible.io URL with query params', () => {
+ const snippets = [mockSnippet('https://plausible.io/js/plausible.js?v=1.0.0')]
+ expect(checkProxyLikely(snippets)).toBe(false)
+ })
+
+ test('handles similar domain names (should be true)', () => {
+ const snippets = [mockSnippet('https://plausible.io.example.com/js/plausible.js')]
+ expect(checkProxyLikely(snippets)).toBe(true)
+ })
+
+ test('returns true when snippet src is relative path', () => {
+ const snippets = [mockSnippet('/js/plausible.js')]
+ expect(checkProxyLikely(snippets)).toBe(true)
+ })
+
+ test('handles multiple snippets - returns true if any snippet is proxied', () => {
+ const snippets = [
+ mockSnippet('https://plausible.io/js/plausible.js'),
+ mockSnippet('https://analytics.example.com/js/plausible.js')
+ ]
+ expect(checkProxyLikely(snippets)).toBe(true)
+ })
+
+ test('handles multiple snippets - returns false if all snippets are official', () => {
+ const snippets = [
+ mockSnippet('https://plausible.io/js/plausible.js'),
+ mockSnippet('https://plausible.io/js/plausible.outbound-links.js')
+ ]
+ expect(checkProxyLikely(snippets)).toBe(false)
+ })
+
+ test('handles plausible.io subdomain (should be true)', () => {
+ const snippets = [mockSnippet('https://staging.plausible.io/js/plausible.js')]
+ expect(checkProxyLikely(snippets)).toBe(true)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/check-unknown-attributes.test.js b/tracker/test/verifier/check-unknown-attributes.test.js
new file mode 100644
index 0000000000..8b00485cbf
--- /dev/null
+++ b/tracker/test/verifier/check-unknown-attributes.test.js
@@ -0,0 +1,114 @@
+import { test, expect } from '@playwright/test'
+import { checkUnknownAttributes } from '../../verifier/check-unknown-attributes.js'
+
+test.describe('checkUnknownAttributes', () => {
+ test('returns false when no snippets', () => {
+ expect(checkUnknownAttributes([])).toBe(false)
+ expect(checkUnknownAttributes(null)).toBe(false)
+ expect(checkUnknownAttributes(undefined)).toBe(false)
+ })
+
+ test('returns false when all attributes are known', () => {
+ const mockSnippet = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'defer', value: '' },
+ { name: 'data-api', value: '/api/event' },
+ { name: 'data-exclude', value: '/admin/*' },
+ { name: 'data-include', value: '/blog/*' },
+ { name: 'data-cfasync', value: 'false' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet])).toBe(false)
+ })
+
+ test('returns false when type="text/javascript" attribute is present', () => {
+ const mockSnippet = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'type', value: 'text/javascript' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet])).toBe(false)
+ })
+
+ test('returns false when event-* attributes are present', () => {
+ const mockSnippet = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'event-click', value: 'handler' },
+ { name: 'event-load', value: 'loadHandler' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet])).toBe(false)
+ })
+
+ test('returns true when unknown attributes are present', () => {
+ const mockSnippet = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'unknown-attribute', value: 'value' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet])).toBe(true)
+ })
+
+ test('returns true when multiple unknown attributes are present', () => {
+ const mockSnippet = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'unknown-attribute-1', value: 'value1' },
+ { name: 'unknown-attribute-2', value: 'value2' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet])).toBe(true)
+ })
+
+ test('returns true when at least one snippet has unknown attributes', () => {
+ const mockSnippet1 = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' }
+ ]
+ }
+
+ const mockSnippet2 = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'unknown-attribute', value: 'value' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet1, mockSnippet2])).toBe(true)
+ })
+
+ test('returns false when all snippets have only known attributes', () => {
+ const mockSnippet1 = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' }
+ ]
+ }
+
+ const mockSnippet2 = {
+ attributes: [
+ { name: 'data-domain', value: 'example.com' },
+ { name: 'src', value: 'https://plausible.io/js/script.js' },
+ { name: 'defer', value: '' }
+ ]
+ }
+
+ expect(checkUnknownAttributes([mockSnippet1, mockSnippet2])).toBe(false)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/check-wp.spec.js b/tracker/test/verifier/check-wp.spec.js
new file mode 100644
index 0000000000..e77ac130d2
--- /dev/null
+++ b/tracker/test/verifier/check-wp.spec.js
@@ -0,0 +1,98 @@
+import { test, expect } from '@playwright/test'
+import { checkWordPress, WORDPRESS_PLUGIN_VERSION_SELECTOR } from '../../verifier/check-wordpress'
+
+function mockDocument(html, hasMetaTag) {
+ return {
+ documentElement: {outerHTML: `<html>${html}</html>`},
+ querySelector: (selector) => {
+ if (selector === WORDPRESS_PLUGIN_VERSION_SELECTOR) {
+ return hasMetaTag ? {} : null
+ }
+ return null
+ }
+ }
+}
+
+test.describe('checkWordPress (wordpressPlugin, wordPressLikely)', () => {
+ test('handles document undefined', () => {
+ const result = checkWordPress(undefined)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(false)
+ })
+
+ test('handles document.querySelector undefined', () => {
+ const document = {documentElement: {outerHTML: '<html></html>'}}
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(false)
+ })
+
+ test('handles document.documentElement undefined', () => {
+ const document = {documentElement: undefined, querySelector: (_) => null}
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(false)
+ })
+
+ test('both false when no WordPress indicators present', () => {
+ const document = mockDocument('<head></head><body></body>', false)
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(false)
+ })
+
+ test('both true if WordPress plugin version meta tag detected', () => {
+ const document = mockDocument('<head></head><body></body>', true)
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(true)
+ expect(result.wordpressLikely).toBe(true)
+ })
+
+ test('detects wordpressLikely by wp-content signature', () => {
+ const document = mockDocument('<head><script src="/wp-content/themes/mytheme/script.js"></script></head>', false)
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(true)
+ })
+
+ test('detects wordpressLikely by wp-includes signature', () => {
+ const document = mockDocument('<head><link rel="stylesheet" href="/wp-includes/css/style.css"></head>', false)
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(true)
+ })
+
+ test('detects wordpressLikely by wp-json signature', () => {
+ const document = mockDocument('<body><script>fetch("/wp-json/wp/v2/posts")</script></body>', false)
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(true)
+ })
+
+ test('detects wordpressLikely by multiple signatures', () => {
+ const document = mockDocument(
+ `
+ <head>
+ <script src="/wp-content/themes/mytheme/script.js"></script>
+ </head>
+ <body>
+ <script>fetch("/wp-json/wp/v2/posts")</script>
+ </body>
+ `,
+ false
+ )
+
+ const result = checkWordPress(document)
+
+ expect(result.wordpressPlugin).toBe(false)
+ expect(result.wordpressLikely).toBe(true)
+ })
+})
\ No newline at end of file
diff --git a/tracker/test/verifier/verifier-v1.spec.js b/tracker/test/verifier/verifier-v1.spec.js
new file mode 100644
index 0000000000..fcf68ba2d9
--- /dev/null
+++ b/tracker/test/verifier/verifier-v1.spec.js
@@ -0,0 +1,508 @@
+import { test, expect } from '@playwright/test'
+import verify from '../support/verify-playwright-wrapper'
+import { delay } from '../support/test-utils'
+import { initializePageDynamically } from '../support/initialize-page-dynamically'
+import { compileFile } from '../../compiler'
+
+const SOME_DOMAIN = 'somesite.com'
+
+async function mockEventResponseSuccess(page, responseDelay = 0) {
+ await page.context().route('**/api/event', async (route) => {
+ if (responseDelay > 0) {
+ await delay(responseDelay)
+ }
+
+ await route.fulfill({
+ status: 202,
+ contentType: 'text/plain',
+ body: 'ok'
+ })
+ })
+}
+
+test.describe('v1 verifier (basic diagnostics)', () => {
+ test('correct installation', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(1)
+ expect(result.data.snippetsFoundInBody).toBe(0)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ expect(result.data.wordpressPlugin).toBe(false)
+ expect(result.data.wordpressLikely).toBe(false)
+ expect(result.data.cookieBannerLikely).toBe(false)
+ expect(result.data.manualScriptExtension).toBe(false)
+
+ // `data.proxyLikely` is mostly expected to be true in tests because
+ // any local script src is considered a proxy. More involved behaviour
+ // is covered by unit tests under `check-proxy-likely.spec.js`
+ expect(result.data.proxyLikely).toBe(true)
+ })
+
+ test('handles a dynamically loaded snippet', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head></head>
+ <body>
+ <script>
+ const script = document.createElement('script')
+
+ script.defer = true
+ script.dataset.domain = '${SOME_DOMAIN}'
+ script.src = "/tracker/js/plausible.local.manual.js"
+
+ setTimeout(() => {
+ document.getElementsByTagName('head')[0].appendChild(script)
+ }, 500)
+ </script>
+ </body>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN, debug: true})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(1)
+ expect(result.data.snippetsFoundInBody).toBe(0)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+ test('missing snippet', async ({ page }, { testId }) => {
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: ''
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(false)
+ expect(result.data.callbackStatus).toBe(0)
+ expect(result.data.snippetsFoundInHead).toBe(0)
+ expect(result.data.snippetsFoundInBody).toBe(0)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+ test('snippet in body', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `<body><script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script></body>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(0)
+ expect(result.data.snippetsFoundInBody).toBe(1)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+ test('figures out well placed snippet in a multi-domain setup', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `<head><script defer data-domain="example.org,example.com,example.net" src="/tracker/js/plausible.local.js"></script></head>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: "example.com"})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(1)
+ expect(result.data.snippetsFoundInBody).toBe(0)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+ test('figures out well placed snippet in a multi-domain mismatch', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `<head><script defer data-domain="example.org,example.com,example.net" src="/tracker/js/plausible.local.js"></script></head>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: "example.typo"})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(1)
+ expect(result.data.snippetsFoundInBody).toBe(0)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(true)
+ })
+
+ test('proxyLikely is false when every snippet starts with an official plausible.io URL', async ({ page }, { testId }) => {
+ const prodScriptLocation = 'https://plausible.io/js/'
+
+ mockEventResponseSuccess(page)
+
+ // We speed up the test by serving "just some script"
+ // (avoiding the event callback delay in verifier)
+ const code = await compileFile({
+ name: "plausible.local.js",
+ globals: {
+ "COMPILE_LOCAL": true,
+ "COMPILE_PLAUSIBLE_LEGACY_VARIANT": true
+ }
+ }, { returnCode: true })
+
+ await page.context().route(`${prodScriptLocation}**`, async (route) => {
+ await route.fulfill({
+ status: 200,
+ contentType: 'application/javascript',
+ body: code
+ })
+ })
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <head><script defer src="${prodScriptLocation + 'script.js'}" data-domain="${SOME_DOMAIN}"></script></head>
+ <body><script defer src="${prodScriptLocation + 'plausible.outbound-links.js'}" data-domain="${SOME_DOMAIN}"></script></body>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.proxyLikely).toBe(false)
+ })
+
+ test('counting snippets', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <head>
+ <script defer data-domain="example.com" src="/tracker/js/plausible.local.js"></script>
+ <script defer data-domain="example.com" src="/tracker/js/plausible.local.js"></script>
+ </head>
+ <body>
+ <script defer data-domain="example.com" src="/tracker/js/plausible.local.js"></script>
+ <script defer data-domain="example.com" src="/tracker/js/plausible.local.js"></script>
+ <script defer data-domain="example.com" src="/tracker/js/plausible.local.js"></script>
+ </body>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: "example.com"})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.snippetsFoundInHead).toBe(2)
+ expect(result.data.snippetsFoundInBody).toBe(3)
+ expect(result.data.callbackStatus).toBe(202)
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+ test('detects dataDomainMismatch', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="wrong.com" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: 'right.com'})
+
+ expect(result.data.dataDomainMismatch).toBe(true)
+ })
+
+ test('dataDomainMismatch is false when data-domain without "www." prefix matches', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="www.right.com" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: 'right.com'})
+
+ expect(result.data.dataDomainMismatch).toBe(false)
+ })
+
+})
+
+test.describe('v1 verifier (window.plausible)', () => {
+ test('callbackStatus is 404 when /api/event not found', async ({ page }, { testId }) => {
+ await page.context().route('**/api/event', async (route) => {
+ await route.fulfill({status: 404})
+ })
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.callbackStatus).toBe(404)
+ })
+
+ test('callBackStatus is 0 when event request times out', async ({ page }, { testId }) => {
+ mockEventResponseSuccess(page, 20000)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.callbackStatus).toBe(0)
+ })
+
+ test('callBackStatus is -1 when a network error occurs on sending event', async ({ page }, { testId }) => {
+ await page.context().route('**/api/event', async (route) => {
+ await route.abort()
+ })
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.plausibleInstalled).toBe(true)
+ expect(result.data.callbackStatus).toBe(-1)
+ })
+})
+
+test.describe('v1 verifier (WordPress detection)', () => {
+ test('if wordpress plugin detected, wordpressLikely is also true', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <head>
+ <meta name="plausible-analytics-version" content="2.3.1">
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>
+ </head>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.wordpressPlugin).toBe(true)
+ expect(result.data.wordpressLikely).toBe(true)
+ })
+
+ test('detects wordpressLikely by wp signatures', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <head>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>
+ </head>
+ <body>
+ <script src="/wp-content/themes/mytheme/script.js"></script>
+ </body>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.wordpressPlugin).toBe(false)
+ expect(result.data.wordpressLikely).toBe(true)
+ })
+})
+
+test.describe('v1 verifier (GTM detection)', () => {
+ test('detects GTM', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>
+ <!-- Google Tag Manager -->
+ <script>
+ (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
+ new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
+ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
+ 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
+ })(window,document,'script','dataLayer','XXXX');
+ </script>
+ <!-- End Google Tag Manager -->
+ </head>
+ <body>Hello</body>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.gtmLikely).toBe(true)
+ })
+})
+
+test.describe('v1 verifier (cookieBanner detection)', () => {
+ test('detects a dynamically loaded cookiebot', async ({ page }, { testId }) => {
+ // While in real world the plausible script would be prevented
+ // from loading when cookiebot is present, to speed up the test
+ // we let it load, but mock a general network error. That is to
+ // avoid the a 202 response which skips cookiebot detection.
+ await page.context().route('**/api/event', async (route) => {
+ // To make sure the banner gets dynamically loaded before the
+ // event callback finishes, we mock a 1s delay before aborting.
+ await delay(1000)
+ await route.abort()
+ })
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>
+ </head>
+ <body>
+ <script>
+ setTimeout(() => {
+ const banner = document.createElement('div')
+ banner.id = 'CybotCookiebotDialog'
+ document.body.appendChild(banner)
+ }, 500);
+ </script>
+ </body>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.cookieBannerLikely).toBe(true)
+ })
+})
+
+test.describe('v1 verifier (manualScriptExtension detection)', () => {
+ test('manualScriptExtension is true when any snippet src has "manual." in it', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.hash.js"></script>
+ </head>
+ <body>
+ <script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.manual.js"></script>
+ </body>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.manualScriptExtension).toBe(true)
+ })
+})
+
+test.describe('v1 verifier (unknownAttributes detection)', () => {
+ test('unknownAttributes is false when all attrs are known', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head>
+ <script
+ defer
+ type="text/javascript"
+ data-cfasync="false"
+ data-api="some"
+ data-include="some"
+ data-exclude="some"
+ data-domain="${SOME_DOMAIN}"
+ src="/tracker/js/plausible.manual.js">
+ </script>
+ </head>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.unknownAttributes).toBe(false)
+ })
+
+ test('unknownAttributes is true when any unknown attributes are present', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ response: `
+ <html>
+ <head>
+ <script defer weird="one" data-domain="${SOME_DOMAIN}" src="/tracker/js/script.js"></script>
+ </head>
+ </html>
+ `
+ })
+
+ const result = await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(result.data.unknownAttributes).toBe(true)
+ })
+})
+
+test.describe('v1 verifier (logging)', () => {
+ test('console logs in debug mode', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ let logs = []
+ page.context().on('console', msg => msg.type() === 'log' && logs.push(msg.text()))
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN, debug: true})
+
+ expect(logs.find(str => str.includes('Starting snippet detection'))).toContain('[Plausible Verification] Starting snippet detection')
+ expect(logs.find(str => str.includes('Checking for Plausible function'))).toContain('[Plausible Verification] Checking for Plausible function')
+ })
+
+ test('does not log by default', async ({ page }, { testId }) => {
+ await mockEventResponseSuccess(page)
+
+ let logs = []
+ page.context().on('console', msg => msg.type() === 'log' && logs.push(msg.text()))
+
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: `<script defer data-domain="${SOME_DOMAIN}" src="/tracker/js/plausible.local.js"></script>`
+ })
+
+ await verify(page, {url: url, expectedDataDomain: SOME_DOMAIN})
+
+ expect(logs.length).toBe(0)
+ })
+})
diff --git a/tracker/verifier/check-cookie-banner.js b/tracker/verifier/check-cookie-banner.js
new file mode 100644
index 0000000000..bedf054253
--- /dev/null
+++ b/tracker/verifier/check-cookie-banner.js
@@ -0,0 +1,38 @@
+const SELECTORS = {
+ // https://github.com/cavi-au/Consent-O-Matic/blob/master/rules/cookiebot.json
+ // We check whether any of the selectors mentioner under
+ // `cookiebot.detectors[0].showingMatcher[0].target.selector`
+ // is visible on the page.
+ cookiebot: [
+ '#CybotCookiebotDialogBodyButtonAccept',
+ '#CybotCookiebotDialogBody',
+ '#CybotCookiebotDialogBodyLevelButtonPreferences',
+ '#cb-cookieoverlay',
+ '#CybotCookiebotDialog',
+ '#cookiebanner',
+ ]
+}
+
+function isVisible(element) {
+ const style = window.getComputedStyle(element);
+
+ return (
+ style.display !== 'none' &&
+ style.visibility !== 'hidden' &&
+ element.offsetParent !== null
+ );
+}
+
+export function checkCookieBanner() {
+ for (const provider of Object.keys(SELECTORS)) {
+ for (const selector of SELECTORS[provider]) {
+ const element = document.querySelector(selector)
+
+ if (element && isVisible(element)) {
+ return true
+ }
+ }
+ }
+
+ return false
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-data-domain-mismatch.js b/tracker/verifier/check-data-domain-mismatch.js
new file mode 100644
index 0000000000..a370c55c4d
--- /dev/null
+++ b/tracker/verifier/check-data-domain-mismatch.js
@@ -0,0 +1,12 @@
+export function checkDataDomainMismatch(snippets, expectedDataDomain) {
+ if (!snippets || snippets.length === 0) return false
+
+ return snippets.some(snippet => {
+ const scriptDataDomain = snippet.getAttribute('data-domain')
+
+ const multiple = scriptDataDomain.split(',').map(d => d.trim())
+ const dataDomainMismatch = !multiple.some((domain) => domain.replace(/^www\./, '') === expectedDataDomain)
+
+ return dataDomainMismatch
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-gtm.js b/tracker/verifier/check-gtm.js
new file mode 100644
index 0000000000..cc91b00a78
--- /dev/null
+++ b/tracker/verifier/check-gtm.js
@@ -0,0 +1,21 @@
+const GTM_SIGNATURES = [
+ 'googletagmanager.com/gtm.js'
+]
+
+function scanGTM(html) {
+ if (typeof html === 'string') {
+ return GTM_SIGNATURES.some(signature => {
+ return html.includes(signature)
+ })
+ }
+
+ return false
+}
+
+export function checkGTM(document) {
+ if (typeof document === 'object') {
+ return scanGTM(document.documentElement?.outerHTML)
+ }
+
+ return false
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-manual-extension.js b/tracker/verifier/check-manual-extension.js
new file mode 100644
index 0000000000..8060e59d8f
--- /dev/null
+++ b/tracker/verifier/check-manual-extension.js
@@ -0,0 +1,7 @@
+export function checkManualExtension(snippets) {
+ if (!snippets || snippets.length === 0) return false
+
+ return snippets.some(snippet => {
+ return snippet.getAttribute('src').includes('manual.')
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-proxy-likely.js b/tracker/verifier/check-proxy-likely.js
new file mode 100644
index 0000000000..c086634f33
--- /dev/null
+++ b/tracker/verifier/check-proxy-likely.js
@@ -0,0 +1,8 @@
+export function checkProxyLikely(snippets) {
+ if (!snippets || snippets.length === 0) return false
+
+ return snippets.some(snippet => {
+ const src = snippet.getAttribute('src')
+ return src && !/^https:\/\/plausible\.io\//.test(src)
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-unknown-attributes.js b/tracker/verifier/check-unknown-attributes.js
new file mode 100644
index 0000000000..6d85e0fafd
--- /dev/null
+++ b/tracker/verifier/check-unknown-attributes.js
@@ -0,0 +1,35 @@
+const KNOWN_ATTRIBUTES = [
+ 'data-domain',
+ 'src',
+ 'defer',
+ 'data-api',
+ 'data-exclude',
+ 'data-include',
+ 'data-cfasync'
+]
+
+export function checkUnknownAttributes(snippets) {
+ if (!snippets || snippets.length === 0) return false
+
+ return snippets.some(snippet => {
+ const attributes = snippet.attributes
+
+ for (let i = 0; i < attributes.length; i++) {
+ const attr = attributes[i]
+
+ if (attr.name === 'type' && attr.value === 'text/javascript') {
+ continue
+ }
+
+ if (attr.name.startsWith('event-')) {
+ continue
+ }
+
+ if (!KNOWN_ATTRIBUTES.includes(attr.name)) {
+ return true
+ }
+ }
+
+ return false
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/check-wordpress.js b/tracker/verifier/check-wordpress.js
new file mode 100644
index 0000000000..bba815e7ec
--- /dev/null
+++ b/tracker/verifier/check-wordpress.js
@@ -0,0 +1,37 @@
+export const WORDPRESS_PLUGIN_VERSION_SELECTOR = 'meta[name="plausible-analytics-version"]'
+
+const WORDPRESS_SIGNATURES = [
+ 'wp-content',
+ 'wp-includes',
+ 'wp-json'
+]
+
+function scanWpPlugin(document) {
+ if (typeof document.querySelector === 'function') {
+ const metaTag = document.querySelector(WORDPRESS_PLUGIN_VERSION_SELECTOR)
+ return metaTag !== null
+ }
+
+ return false
+}
+
+function scanWp(html) {
+ if (typeof html === 'string') {
+ return WORDPRESS_SIGNATURES.some(signature => {
+ return html.includes(signature)
+ })
+ }
+
+ return false
+}
+
+export function checkWordPress(document) {
+ if (typeof document === 'object') {
+ const wordpressPlugin = scanWpPlugin(document)
+ const wordpressLikely = wordpressPlugin || scanWp(document.documentElement?.outerHTML)
+
+ return {wordpressPlugin, wordpressLikely}
+ }
+
+ return {wordpressPlugin: false, wordpressLikely: false}
+}
\ No newline at end of file
diff --git a/tracker/verifier/plausible-function-check.js b/tracker/verifier/plausible-function-check.js
new file mode 100644
index 0000000000..e9e6ff8669
--- /dev/null
+++ b/tracker/verifier/plausible-function-check.js
@@ -0,0 +1,58 @@
+import { runThrottledCheck } from "./run-check"
+
+export async function plausibleFunctionCheck(log) {
+ log('Checking for Plausible function...')
+ const plausibleFound = await waitForPlausibleFunction()
+
+ if (plausibleFound) {
+ log('Plausible function found. Executing test event...')
+ const callbackResult = await testPlausibleCallback(log)
+ log(`Test event callback response: ${callbackResult.status}`)
+ return { plausibleInstalled: true, callbackStatus: callbackResult.status }
+ } else {
+ log('Plausible function not found')
+ return { plausibleInstalled: false}
+ }
+}
+
+async function waitForPlausibleFunction() {
+ const checkFn = (opts) => {
+ if (window.plausible) { return true }
+ if (opts.timeout) { return false }
+ return 'continue'
+ }
+ return await runThrottledCheck(checkFn, {timeout: 5000, interval: 100})
+}
+
+function testPlausibleCallback(log) {
+ return new Promise((resolve) => {
+ let callbackResolved = false
+
+ const callbackTimeout = setTimeout(() => {
+ if (!callbackResolved) {
+ callbackResolved = true
+ log('Timeout waiting for Plausible function callback')
+ resolve({ status: undefined })
+ }
+ }, 5000)
+
+ try {
+ window.plausible('verification-agent-test', {
+ callback: function(options) {
+ if (!callbackResolved) {
+ callbackResolved = true
+ clearTimeout(callbackTimeout)
+ resolve({status: options && options.status ? options.status : -1 })
+ }
+ }
+ })
+ } catch (error) {
+ if (!callbackResolved) {
+ callbackResolved = true
+ clearTimeout(callbackTimeout)
+ log('Error calling plausible function:', error)
+ resolve({ status: -1 })
+ }
+ }
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/run-check.js b/tracker/verifier/run-check.js
new file mode 100644
index 0000000000..c9218e147d
--- /dev/null
+++ b/tracker/verifier/run-check.js
@@ -0,0 +1,17 @@
+export async function runThrottledCheck(checkFn, {timeout, interval}) {
+ return runCheckRecursive(checkFn, timeout, interval, 1)
+}
+
+async function runCheckRecursive(checkFn, timeout, interval, iteration) {
+ return new Promise((resolve) => {
+ if (iteration * interval >= timeout) {
+ resolve(checkFn({timeout: true}))
+ } else if (checkFn({timeout: false}) !== 'continue') {
+ resolve(checkFn({timeout: false}))
+ } else {
+ setTimeout(() => {
+ resolve(runCheckRecursive(checkFn, timeout, interval, iteration + 1))
+ }, interval)
+ }
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/snippet-checks.js b/tracker/verifier/snippet-checks.js
new file mode 100644
index 0000000000..8807f30d1c
--- /dev/null
+++ b/tracker/verifier/snippet-checks.js
@@ -0,0 +1,64 @@
+import { runThrottledCheck } from "./run-check"
+
+export async function waitForSnippetsV1(log) {
+ log('Starting snippet detection...')
+
+ let snippetCounts = await waitForFirstSnippet()
+
+ if (snippetCounts.all > 0) {
+ log(`Found snippets: head=${snippetCounts.head}; body=${snippetCounts.body}`)
+ log('Waiting for additional snippets to appear...')
+
+ snippetCounts = await waitForAdditionalSnippets()
+
+ log(`Final snippet count: head=${snippetCounts.head}; body=${snippetCounts.body}`)
+ } else {
+ log('No snippets found after 5 seconds')
+ }
+
+ return {
+ nodes: [...getHeadSnippets(), ...getBodySnippets()],
+ counts: snippetCounts,
+ }
+}
+
+function getHeadSnippets() {
+ return document.querySelectorAll('head script[data-domain][src]')
+}
+
+function getBodySnippets() {
+ return document.querySelectorAll('body script[data-domain][src]')
+}
+
+function countSnippets() {
+ const headSnippets = getHeadSnippets()
+ const bodySnippets = getBodySnippets()
+
+ return {
+ head: headSnippets.length,
+ body: bodySnippets.length,
+ all: headSnippets.length + bodySnippets.length
+ }
+}
+
+async function waitForFirstSnippet() {
+ const checkFn = (opts) => {
+ const snippetsFound = countSnippets()
+
+ if (snippetsFound.all > 0 || opts.timeout) {
+ return snippetsFound
+ }
+
+ return 'continue'
+ }
+
+ return await runThrottledCheck(checkFn, {timeout: 5000, interval: 100})
+}
+
+async function waitForAdditionalSnippets() {
+ return new Promise((resolve) => {
+ setTimeout(() => {
+ resolve(countSnippets())
+ }, 1000)
+ })
+}
\ No newline at end of file
diff --git a/tracker/verifier/verifier-v1.js b/tracker/verifier/verifier-v1.js
new file mode 100644
index 0000000000..2b59066dd6
--- /dev/null
+++ b/tracker/verifier/verifier-v1.js
@@ -0,0 +1,70 @@
+import { waitForSnippetsV1 } from "./snippet-checks"
+import { plausibleFunctionCheck } from "./plausible-function-check"
+import { checkDataDomainMismatch } from "./check-data-domain-mismatch"
+import { checkProxyLikely } from "./check-proxy-likely"
+import { checkWordPress } from "./check-wordpress"
+import { checkGTM } from "./check-gtm"
+import { checkCookieBanner } from "./check-cookie-banner"
+import { checkManualExtension } from "./check-manual-extension"
+import { checkUnknownAttributes } from "./check-unknown-attributes"
+
+window.verifyPlausibleInstallation = async function(expectedDataDomain, debug) {
+ function log(message) {
+ if (debug) console.log('[Plausible Verification]', message)
+ }
+
+ const [snippetData, plausibleFunctionDiagnostics] = await Promise.all([
+ waitForSnippetsV1(log),
+ plausibleFunctionCheck(log)
+ ])
+
+ const plausibleInstalled = plausibleFunctionDiagnostics.plausibleInstalled
+ const callbackStatus = plausibleFunctionDiagnostics.callbackStatus || 0
+
+ const dataDomainMismatch = checkDataDomainMismatch(snippetData.nodes, expectedDataDomain)
+ log(`dataDomainMismatch: ${dataDomainMismatch}`)
+
+ const manualScriptExtension = checkManualExtension(snippetData.nodes)
+ log(`manualScriptExtension: ${manualScriptExtension}`)
+
+ const unknownAttributes = checkUnknownAttributes(snippetData.nodes)
+ log(`unknownAttributes: ${unknownAttributes}`)
+
+ const proxyLikely = checkProxyLikely(snippetData.nodes)
+ log(`proxyLikely: ${proxyLikely}`)
+
+ const {wordpressPlugin, wordpressLikely} = checkWordPress(document)
+ log(`wordpressPlugin: ${wordpressPlugin}`)
+ log(`wordpressLikely: ${wordpressLikely}`)
+
+ const gtmLikely = checkGTM(document)
+ log(`gtmLikely: ${gtmLikely}`)
+
+ let cookieBannerLikely
+
+ if (plausibleInstalled && [200, 202].includes(callbackStatus)) {
+ cookieBannerLikely = false
+ } else {
+ cookieBannerLikely = checkCookieBanner()
+ }
+
+ log(`cookieBannerLikely: ${cookieBannerLikely}`)
+
+ return {
+ data: {
+ completed: true,
+ plausibleInstalled: plausibleFunctionDiagnostics.plausibleInstalled,
+ callbackStatus: plausibleFunctionDiagnostics.callbackStatus || 0,
+ snippetsFoundInHead: snippetData.counts.head,
+ snippetsFoundInBody: snippetData.counts.body,
+ dataDomainMismatch: dataDomainMismatch,
+ proxyLikely: proxyLikely,
+ wordpressPlugin: wordpressPlugin,
+ wordpressLikely: wordpressLikely,
+ gtmLikely: gtmLikely,
+ cookieBannerLikely: cookieBannerLikely,
+ manualScriptExtension: manualScriptExtension,
+ unknownAttributes: unknownAttributes
+ }
+ }
+}
\ No newline at end of file