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
Script v2: Handle verifying sites where there's an immediate JS redirect (#5619)
Artur Pata5807878a18da
diff --git a/lib/plausible/installation_support/checks/installation_v2.ex b/lib/plausible/installation_support/checks/installation_v2.exindex 0928b845ee..869ce89aa5 100644--- a/lib/plausible/installation_support/checks/installation_v2.ex+++ b/lib/plausible/installation_support/checks/installation_v2.ex@@ -1,40 +1,73 @@defmodule Plausible.InstallationSupport.Checks.InstallationV2 dorequire Logger- path = Application.app_dir(:plausible, "priv/tracker/installation_support/verifier-v2.js")- # On CI, the file might not be present for static checks so we create an empty one- File.touch!(path)+ @verifier_code_path "priv/tracker/installation_support/verifier-v2.js"+ @external_resource @verifier_code_path- @verifier_code File.read!(path)- @external_resource "priv/tracker/installation_support/verifier-v2.js"+ # On CI, the file might not be present for static checks so we default to empty string+ @verifier_code (case File.read(Application.app_dir(:plausible, @verifier_code_path)) do+ {:ok, content} -> content+ {:error, _} -> ""+ end)- @function_check_timeout 10_000+ # To support browserless API being unavailable or overloaded, we retry the endpoint call if it doesn't return a successful response+ @max_retries 2++ # We define a timeout for the browserless endpoint call to avoid waiting too long for a response+ @endpoint_timeout_ms 10_000++ # This timeout determines how long we wait for window.plausible to be initialized on the page, including sending the test event+ @plausible_window_check_timeout_ms 4_000++ # To handle navigation that happens immediately on the page, we attempt to verify the installation multiple times _within a single browserless endpoint call_+ @max_attempts 2+ @timeout_between_attempts_ms 500# Puppeteer wrapper function that executes the vanilla JS verifier code@puppeteer_wrapper_code """- export default async function({ page, context }) {+ export default async function({ page, context: { url, userAgent, maxAttempts, timeoutBetweenAttemptsMs, ...functionContext } }) {try {- await page.setUserAgent(context.userAgent);- const response = await page.goto(context.url);-- await page.evaluate(() => {- #{@verifier_code}- });-- return await page.evaluate(async ({ responseHeaders, debug, timeoutMs, cspHostToCheck }) => {- return await window.verifyPlausibleInstallation({ responseHeaders, debug, timeoutMs, cspHostToCheck });- }, {- timeoutMs: context.timeoutMs,- responseHeaders: response.headers(),- debug: context.debug,- cspHostToCheck: context.cspHostToCheck- });+ await page.setUserAgent(userAgent)+ const response = await page.goto(url)+ const responseHeaders = response.headers()++ async function verify() {+ await page.evaluate(() => {#{@verifier_code}}) // injects window.verifyPlausibleInstallation+ return await page.evaluate(+ (c) => window.verifyPlausibleInstallation(c),+ { ...functionContext, responseHeaders }+ );+ }++ let lastError;+ for (let attempts = 1; attempts <= maxAttempts; attempts++) {+ try {+ const output = await verify();+ return {+ data: {+ ...output.data,+ attempts+ },+ };+ } catch (error) {+ lastError = error;+ if (+ typeof error?.message === "string" &&+ error.message.toLowerCase().includes("execution context")+ ) {+ await new Promise((resolve) => setTimeout(resolve, timeoutBetweenAttemptsMs));+ continue;+ }+ throw error+ }+ }+ throw lastError;} catch (error) {return {data: {completed: false,error: {- message: error?.message ?? JSON.stringify(error),+ message: error?.message ?? JSON.stringify(error)}}}@@ -59,16 +92,19 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 doJSON.encode!(%{code: @puppeteer_wrapper_code,context: %{+ maxAttempts: @max_attempts,+ timeoutMs: @plausible_window_check_timeout_ms,+ timeoutBetweenAttemptsMs: @timeout_between_attempts_ms,cspHostToCheck: PlausibleWeb.Endpoint.host(),- timeoutMs: @function_check_timeout,url: Plausible.InstallationSupport.URL.bust_url(url),userAgent: Plausible.InstallationSupport.user_agent(),debug: Application.get_env(:plausible, :environment) == "dev"}}),+ params: %{timeout: @endpoint_timeout_ms},retry: :transient,retry_log_level: :warning,- max_retries: 2+ max_retries: @max_retries]extra_opts = Application.get_env(:plausible, __MODULE__)[:req_opts] || []@@ -100,6 +136,7 @@ defmodule Plausible.InstallationSupport.Checks.InstallationV2 doplausible_variant: data["plausibleVariant"],test_event: data["testEvent"],cookie_banner_likely: data["cookieBannerLikely"],+ attempts: data["attempts"],service_error: nil)elsediff --git a/lib/plausible/installation_support/verification/diagnostics.ex b/lib/plausible/installation_support/verification/diagnostics.exindex 9b4d81e275..24b0cc64a2 100644--- a/lib/plausible/installation_support/verification/diagnostics.ex+++ b/lib/plausible/installation_support/verification/diagnostics.ex@@ -14,7 +14,8 @@ defmodule Plausible.InstallationSupport.Verification.Diagnostics dodiagnostics_are_from_cache_bust: nil,test_event: nil,cookie_banner_likely: nil,- service_error: nil+ service_error: nil,+ attempts: nil@type t :: %__MODULE__{}diff --git a/lib/plausible_web/live/components/verification.ex b/lib/plausible_web/live/components/verification.exindex 6037ddd3d4..807bd541fd 100644--- a/lib/plausible_web/live/components/verification.ex+++ b/lib/plausible_web/live/components/verification.ex@@ -127,34 +127,35 @@ defmodule PlausibleWeb.Live.Components.Verification do</.styled_link></:item></.focus_list>- <div- :if={@verification_state && @super_admin? && @finished?}- class="flex flex-col dark:text-gray-200 mt-4 pt-4 border-t border-gray-300 dark:border-gray-700"- x-data="{ showDiagnostics: false }"- id="super-admin-report"- >- <p class="text-sm">- <a- href="#"- @click.prevent="showDiagnostics = !showDiagnostics"- class="bg-yellow-100 dark:text-gray-800"- >- As a super-admin, you're eligible to see diagnostics details. Click to expand.- </a>- </p>- <div x-show="showDiagnostics" x-cloak>- <.focus_list>- <:item :for={{diag, value} <- Map.from_struct(@verification_state.diagnostics)}>- <span class="text-sm">- {Phoenix.Naming.humanize(diag)}:- <span class="font-mono">{to_string_value(value)}</span>- </span>- </:item>- </.focus_list>- </div>- </div></:footer></.focus_box>+ <.focus_box :if={not is_nil(@verification_state) && @super_admin? && @finished?}>+ <div+ class="flex flex-col dark:text-gray-200"+ x-data="{ showDiagnostics: false }"+ id="super-admin-report"+ >+ <p class="text-sm">+ <a+ href="#"+ @click.prevent="showDiagnostics = !showDiagnostics"+ class="bg-yellow-100 dark:text-gray-800"+ >+ As a super-admin, you're eligible to see diagnostics details. Click to expand.+ </a>+ </p>+ <div x-show="showDiagnostics" x-cloak>+ <.focus_list>+ <:item :for={{diag, value} <- Map.from_struct(@verification_state.diagnostics)}>+ <span class="text-sm">+ {Phoenix.Naming.humanize(diag)}:+ <span class="font-mono">{to_string_value(value)}</span>+ </span>+ </:item>+ </.focus_list>+ </div>+ </div>+ </.focus_box></div>"""enddiff --git a/lib/plausible_web/live/verification.ex b/lib/plausible_web/live/verification.exindex 42e574b982..488c0585a2 100644--- a/lib/plausible_web/live/verification.ex+++ b/lib/plausible_web/live/verification.ex@@ -136,7 +136,9 @@ defmodule PlausibleWeb.Live.Verification doinstallation_type = socket.assigns.installation_type{:ok, pid} =- if(FunWithFlags.enabled?(:scriptv2, for: socket.assigns.site),+ if(+ FunWithFlags.enabled?(:scriptv2, for: socket.assigns.site) or+ FunWithFlags.enabled?(:scriptv2, for: socket.assigns.current_user),do:Verification.Checks.run(url_to_verify, domain, installation_type,report_to: report_to,diff --git a/tracker/test/installation_support/verifier-v2.spec.ts b/tracker/test/installation_support/verifier-v2.spec.tsindex 2e134e0868..50f868504f 100644--- a/tracker/test/installation_support/verifier-v2.spec.ts+++ b/tracker/test/installation_support/verifier-v2.spec.ts@@ -5,7 +5,14 @@ import { mockManyRequests } from '../support/mock-many-requests'import { LOCAL_SERVER_ADDR } from '../support/server'import { tracker_script_version as version } from '../support/test-utils'-const CSP_HOST_TO_CHECK = 'plausible.io'+const DEFAULT_VERIFICATION_OPTIONS = {+ responseHeaders: {},+ debug: true,+ timeoutMs: 1000,+ cspHostToCheck: 'plausible.io',+ maxAttempts: 2,+ timeoutBetweenAttemptsMs: 500+}test.describe('installed plausible web variant', () => {test('using provided snippet', async ({ page }, { testId }) => {@@ -32,15 +39,11 @@ test.describe('installed plausible web variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -66,6 +69,8 @@ test.describe('installed plausible web variant', () => {test('using provided snippet and the events endpoint responds slower than the timeout', async ({page}, { testId }) => {+ const responseDelayMs = 2000+ const timeoutMs = 1000await mockManyRequests({page,path: `https://plausible.io/api/event`,@@ -75,7 +80,7 @@ test.describe('installed plausible web variant', () => {contentType: 'text/plain',body: 'ok'},- responseDelay: 2000+ responseDelay: responseDelayMs})const { url } = await initializePageDynamically(page, {@@ -91,14 +96,13 @@ test.describe('installed plausible web variant', () => {await page.goto(url)const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK+ ...DEFAULT_VERIFICATION_OPTIONS,+ timeoutMs})expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -147,15 +151,11 @@ test.describe('installed plausible web variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -193,15 +193,11 @@ test.describe('installed plausible web variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -219,6 +215,125 @@ test.describe('installed plausible web variant', () => {}})})++ test('there is a JS navigation immediately on page load to a page with the provided snippet', async ({+ page+ }, { testId }) => {+ const { url: urlBeta } = await initializePageDynamically(page, {+ path: '/beta',+ testId,+ scriptConfig: {+ domain: 'example.com/beta',+ endpoint: 'https://plausible.io/api/event',+ captureOnLocalhost: true+ },+ bodyContent: 'beta'+ })++ const { url } = await initializePageDynamically(page, {+ testId,+ scriptConfig: '', // no tracker on initial page+ bodyContent: 'alfa'+ })++ await page.goto(url)+ await expect(page.getByText('alfa')).toBeVisible()++ const [result, _] = await Promise.all([+ executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS),+ page.evaluate(+ ({ targetUrl }) =>+ setTimeout(() => (window.location.href = targetUrl), 250),+ { targetUrl: urlBeta }+ )+ ])+ await expect(page.getByText('beta')).toBeVisible()++ expect(result).toEqual({+ data: {+ attempts: 2,+ completed: true,+ plausibleIsInitialized: true,+ plausibleIsOnWindow: true,+ disallowedByCsp: false,+ plausibleVersion: version,+ plausibleVariant: 'web',+ testEvent: {+ callbackResult: { status: 202 },+ requestUrl: 'https://plausible.io/api/event',+ normalizedBody: {+ domain: 'example.com/beta',+ name: 'verification-agent-test',+ version+ },+ responseStatus: 202,+ error: undefined+ },+ cookieBannerLikely: false+ }+ })+ })++ test('there are more than maxAttempts JS navigations', async ({ page }, {+ testId+ }) => {+ const timeoutBetweenAttemptsMs = 100+ const maxAttempts = 2++ const { url: urlGamma } = await initializePageDynamically(page, {+ path: '/gamma',+ testId,+ scriptConfig: {+ domain: 'example.com/gamma',+ endpoint: 'https://plausible.io/api/event',+ captureOnLocalhost: true+ },+ bodyContent: 'gamma'+ })++ const { url: urlBeta } = await initializePageDynamically(page, {+ path: '/beta',+ testId,+ scriptConfig: '', // no tracker+ bodyContent: `+ <script>setTimeout(() => window.location.href = "${urlGamma}", ${+ timeoutBetweenAttemptsMs + 250+ })</script>`+ })++ const { url } = await initializePageDynamically(page, {+ testId,+ scriptConfig: '', // no tracker+ bodyContent: 'alfa'+ })++ await page.goto(url)+ await expect(page.getByText('alfa')).toBeVisible()++ const [result] = await Promise.all([+ executeVerifyV2(page, {+ ...DEFAULT_VERIFICATION_OPTIONS,+ timeoutBetweenAttemptsMs,+ maxAttempts+ }),+ page.evaluate(+ (url) => setTimeout(() => (window.location.href = url), 250),+ urlBeta+ )+ ])++ await expect(page.getByText('gamma')).toBeVisible()++ expect(result).toEqual({+ data: {+ completed: false,+ error: {+ message:+ 'page.evaluate: Execution context was destroyed, most likely because of a navigation.'+ }+ }+ })+ })})test.describe('installed plausible esm variant', () => {@@ -248,15 +363,11 @@ test.describe('installed plausible esm variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -307,15 +418,11 @@ test.describe('installed plausible esm variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -366,15 +473,11 @@ test.describe('installed plausible esm variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,@@ -414,15 +517,11 @@ test.describe('installed plausible esm variant', () => {await page.goto(url)- const result = await executeVerifyV2(page, {- responseHeaders: {},- debug: true,- timeoutMs: 1000,- cspHostToCheck: CSP_HOST_TO_CHECK- })+ const result = await executeVerifyV2(page, DEFAULT_VERIFICATION_OPTIONS)expect(result).toEqual({data: {+ attempts: 1,completed: true,plausibleIsInitialized: true,plausibleIsOnWindow: true,diff --git a/tracker/test/support/installation-support-playwright-wrappers.ts b/tracker/test/support/installation-support-playwright-wrappers.tsindex 6c2dd5b5e0..b49c000af9 100644--- a/tracker/test/support/installation-support-playwright-wrappers.ts+++ b/tracker/test/support/installation-support-playwright-wrappers.ts@@ -14,27 +14,45 @@ const DETECTOR_JS_VARIANT = variantsFile.manualVariants.find()export async function executeVerifyV2(- page: Page,- { debug, responseHeaders, timeoutMs, cspHostToCheck }: VerifyV2Args+ page: Page,+ { responseHeaders, maxAttempts, timeoutBetweenAttemptsMs, ...functionContext }: VerifyV2Args & { maxAttempts: number, timeoutBetweenAttemptsMs: number }): Promise<VerifyV2Result> {const verifierCode = (await compileFile(VERIFIER_V2_JS_VARIANT, {returnCode: true})) as stringtry {- await page.evaluate(verifierCode)+ async function verify() {+ await page.evaluate(verifierCode) // injects window.verifyPlausibleInstallation+ return await page.evaluate(+ (c) => {return (window as any).verifyPlausibleInstallation(c)},+ {...functionContext, responseHeaders}+ );+ }- return await page.evaluate(- async ({ responseHeaders, debug, timeoutMs, cspHostToCheck }) => {- return await (window as any).verifyPlausibleInstallation({- responseHeaders,- debug,- timeoutMs,- cspHostToCheck- })- },- { responseHeaders, debug, timeoutMs, cspHostToCheck }- )+ let lastError;+ for (let attempts = 1; attempts <= maxAttempts; attempts++) {+ try {+ const output = await verify();+ return {+ data: {+ ...output.data,+ attempts+ },+ };+ } catch (error) {+ lastError = error;+ if (+ typeof error?.message === "string" &&+ error.message.toLowerCase().includes("execution context")+ ) {+ await new Promise((resolve) => setTimeout(resolve, timeoutBetweenAttemptsMs));+ continue;+ }+ throw error+ }+ }+ throw lastError;} catch (error) {return {data: {