Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Add `window.plausible` to NPM + `window.plausible.v2` (#5580)
RobertJoonas9be0b71233e9
diff --git a/test/plausible_web/live/choose_plan_test.exs b/test/plausible_web/live/choose_plan_test.exsindex ea8a25d1d1..bfc02c798e 100644--- a/test/plausible_web/live/choose_plan_test.exs+++ b/test/plausible_web/live/choose_plan_test.exs@@ -433,24 +433,16 @@ defmodule PlausibleWeb.Live.ChoosePlanTest dotest "allows upgrade to a 10k plan with a pageview allowance margin of 0.3 when trial ended 10 days ago",%{conn: conn, site: site, user: user} do- team =- user- |> team_of()- |> Ecto.Changeset.change(trial_expiry_date: Date.shift(Date.utc_today(), day: -10))- |> Repo.update!()+ user+ |> team_of()+ |> Ecto.Changeset.change(trial_expiry_date: Date.shift(Date.utc_today(), day: -10))+ |> Repo.update!()generate_usage_for(site, 13_000){:ok, lv, _doc} = get_liveview(conn)doc = set_slider(lv, "10k")- # NOTE: drop the else clause once Starter tier is live for a trial that ended recently- if Plausible.Teams.Billing.show_new_upgrade_page?(team) do- refute class_of_element(doc, @starter_checkout_button) =~ "pointer-events-none"- else- refute element_exists?(doc, @starter_plan_box)- end-refute class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"refute class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"@@ -459,13 +451,6 @@ defmodule PlausibleWeb.Live.ChoosePlanTest do{:ok, lv, _doc} = get_liveview(conn)doc = set_slider(lv, "10k")- # NOTE: drop the else clause once Starter tier is live for a trial that ended recently- if Plausible.Teams.Billing.show_new_upgrade_page?(team) do- refute class_of_element(doc, @starter_checkout_button) =~ "pointer-events-none"- else- refute element_exists?(doc, @starter_plan_box)- end-assert class_of_element(doc, @growth_checkout_button) =~ "pointer-events-none"assert class_of_element(doc, @business_checkout_button) =~ "pointer-events-none"enddiff --git a/tracker/npm_package/CHANGELOG.md b/tracker/npm_package/CHANGELOG.mdindex 00aa1a0082..0be2068915 100644--- a/tracker/npm_package/CHANGELOG.md+++ b/tracker/npm_package/CHANGELOG.md@@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0## Unreleased+- Bind the `track` function into `window.plausible`. This makes it possible for the Plausible verification agent to verify a successful installation. Can be disabled setting the `bindToWindow` config option to `false`.+## [0.3.2] - 2025-07-14- "Form: Submission" event payload does not need to contain props.path any more: it is saved to be the same as the pathname of the eventdiff --git a/tracker/npm_package/README.md b/tracker/npm_package/README.mdindex d242f09d86..1c40374a59 100644--- a/tracker/npm_package/README.md+++ b/tracker/npm_package/README.md@@ -54,6 +54,7 @@ See also [plausible.d.ts](https://github.com/plausible/analytics/blob/master/tra| `logging` | Whether to log on ignored events. | `true` || `customProperties` | Object or function that returns custom properties for a given event. | `{}` || `transformRequest` | Function that allows transforming or ignoring requests | |+| `bindToWindow` | Binds `track` to `window.plausible` which is used by Plausible installation verification tool to detect whether Plausible has been installed correctly. If `bindToWindow` is set to false, the installation verification tool won't be able to automatically detect it on your site. | `true` |#### Using `customProperties`diff --git a/tracker/npm_package/plausible.d.ts b/tracker/npm_package/plausible.d.tsindex 9ea7fa48cb..5277f971e8 100644--- a/tracker/npm_package/plausible.d.ts+++ b/tracker/npm_package/plausible.d.ts@@ -44,6 +44,10 @@ export interface PlausibleConfig {// This can be used to avoid sending certain types of events, or modifying any event// parameters, e.g. to clean URLs of values that should not be recorded.transformRequest?: (payload: PlausibleRequestPayload) => PlausibleRequestPayload | null++ // If enabled (the default), the script will set `window.plausible` after `init` is called.+ // This is used by the verifier to detect if the script is loaded from npm package.+ bindToWindow?: boolean}export interface PlausibleEventOptions {diff --git a/tracker/package.json b/tracker/package.jsonindex dd222e3e7c..e331c5d7ae 100644--- a/tracker/package.json+++ b/tracker/package.json@@ -1,5 +1,5 @@{- "tracker_script_version": 23,+ "tracker_script_version": 24,"type": "module","scripts": {"deploy": "node compile.js",diff --git a/tracker/src/config.js b/tracker/src/config.jsindex 61d48967d0..4903394e07 100644--- a/tracker/src/config.js+++ b/tracker/src/config.js@@ -43,7 +43,8 @@ export function init(overrides) {}Object.assign(config, overrides, {autoCapturePageviews: overrides.autoCapturePageviews !== false,- logging: overrides.logging !== false+ logging: overrides.logging !== false,+ bindToWindow: overrides.bindToWindow !== false})config.isInitialized = true} else {diff --git a/tracker/src/plausible.js b/tracker/src/plausible.jsindex 3a78571f97..87a7856102 100644--- a/tracker/src/plausible.js+++ b/tracker/src/plausible.js@@ -35,6 +35,20 @@ function init(overrides) {window.plausible = trackwindow.plausible.init = initwindow.plausible.l = true+ window.plausible.v = COMPILE_TRACKER_SCRIPT_VERSION++ if (COMPILE_PLAUSIBLE_WEB) {+ window.plausible.s = 'web'+ }+ }++ // Bind to window to be detectable by the verifier tool+ // This is done in a 'safe' way to avoid breaking the page if window is frozen or running without window+ if (COMPILE_PLAUSIBLE_NPM && config.bindToWindow && typeof window !== 'undefined') {+ window.plausible = track+ window.plausible.l = true+ window.plausible.s = 'npm'+ window.plausible.v = COMPILE_TRACKER_SCRIPT_VERSION}}diff --git a/tracker/test/plausible-npm.spec.js b/tracker/test/plausible-npm.spec.jsindex fb65c61651..ff225105c7 100644--- a/tracker/test/plausible-npm.spec.js+++ b/tracker/test/plausible-npm.spec.js@@ -60,4 +60,18 @@ test.describe('NPM package', () => {await callInit(page, DEFAULT_CONFIG, 'window')}).rejects.toThrow("plausible.init() can only be called once")})++ test('binds to window by default', async ({ page }) => {+ await openPage(page, {}, { skipPlausibleInit: true })+ await callInit(page, DEFAULT_CONFIG, 'window')+ await page.waitForFunction('window.plausible !== undefined')+ })++ test('does not bind to window if bindToWindow is false', async ({ page }) => {+ await openPage(page, {}, { skipPlausibleInit: true })+ await callInit(page, { ...DEFAULT_CONFIG, bindToWindow: false }, 'window')+ await expect(+ page.waitForFunction(() => window.plausible !== undefined, undefined, { timeout: 1000 }),+ ).rejects.toThrow('page.waitForFunction: Timeout 1000ms exceeded.')+ })})