Skip to content

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

Fix unexpected config switch from calling init twice with plausible web (#5601)

Artur Pataa533116444a2
diff --git a/tracker/npm_package/CHANGELOG.md b/tracker/npm_package/CHANGELOG.md
index 60cbca8422..9f61fa535f 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
+- Fix issue with a second init() call changing the config option unexpectedly.
+
## [0.3.5] - 2025-08-04
- Fix issue with link tracking features (tagged events, file downloads, outbound links) causing errors on the page when clicking on `a` tags within `svg` tags.
diff --git a/tracker/package.json b/tracker/package.json
index 3ed45622d9..37a8bd6c4a 100644
--- a/tracker/package.json
+++ b/tracker/package.json
@@ -1,5 +1,5 @@
{
- "tracker_script_version": 26,
+ "tracker_script_version": 27,
"type": "module",
"scripts": {
"deploy": "node compile.js",
diff --git a/tracker/src/config.js b/tracker/src/config.js
index 4903394e07..ff38c12447 100644
--- a/tracker/src/config.js
+++ b/tracker/src/config.js
@@ -20,32 +20,41 @@ function defaultEndpoint() {
}
}
-export function init(overrides) {
+export function getOptionsWithDefaults(initOptions) {
+ if (COMPILE_PLAUSIBLE_WEB) {
+ return Object.assign(initOptions, {
+ autoCapturePageviews: initOptions.autoCapturePageviews !== false,
+ logging: initOptions.logging !== false,
+ })
+ }
+ if (COMPILE_PLAUSIBLE_NPM) {
+ return Object.assign(initOptions, {
+ autoCapturePageviews: initOptions.autoCapturePageviews !== false,
+ logging: initOptions.logging !== false,
+ bindToWindow: initOptions.bindToWindow !== false,
+ })
+ }
+}
+
+export function init(options) {
if (COMPILE_PLAUSIBLE_WEB) {
// This will be dynamically replaced by a config json object in the script serving endpoint
config = "<%= @config_js %>"
- Object.assign(config, overrides, {
- // Explicitly set domain before any overrides are applied as `plausible-web` does not support overriding it
+ Object.assign(config, options, {
+ // Explicitly set domain after other options are applied as `plausible-web` does not support overriding it, except by transformRequest
domain: config.domain,
- // Configuration which defaults to `true`
- autoCapturePageviews: overrides.autoCapturePageviews !== false,
- logging: overrides.logging !== false
})
} else if (COMPILE_PLAUSIBLE_NPM) {
if (config.isInitialized) {
throw new Error('plausible.init() can only be called once')
}
- if (!overrides || !overrides.domain) {
+ if (!options || !options.domain) {
throw new Error('plausible.init(): domain argument is required')
}
- if (!overrides.endpoint) {
- overrides.endpoint = 'https://plausible.io/api/event'
+ if (!options.endpoint) {
+ options.endpoint = 'https://plausible.io/api/event'
}
- Object.assign(config, overrides, {
- autoCapturePageviews: overrides.autoCapturePageviews !== false,
- logging: overrides.logging !== false,
- bindToWindow: overrides.bindToWindow !== false
- })
+ Object.assign(config, options)
config.isInitialized = true
} else {
// Legacy variant
diff --git a/tracker/src/plausible.js b/tracker/src/plausible.js
index 6dbf34bb9b..37e4c22232 100644
--- a/tracker/src/plausible.js
+++ b/tracker/src/plausible.js
@@ -1,19 +1,21 @@
import { init as initEngagementTracking } from './engagement'
-import { init as initConfig, config } from './config'
+import { init as initConfig, getOptionsWithDefaults, config } from './config'
import { init as initCustomEvents, DEFAULT_FILE_TYPES } from './custom-events'
import { init as initAutocapture } from './autocapture'
import { track } from './track'
function init(overrides) {
- initConfig(overrides || {})
+ const options = getOptionsWithDefaults(overrides || {})
if (COMPILE_PLAUSIBLE_WEB && window.plausible && window.plausible.l) {
- if (config.logging) {
+ if (options.logging) {
console.warn('Plausible analytics script was already initialized, skipping init')
}
return
}
+ initConfig(options)
+
initEngagementTracking()
if (!COMPILE_MANUAL || (COMPILE_CONFIG && config.autoCapturePageviews)) {
diff --git a/tracker/test/plausible-web-init.spec.ts b/tracker/test/plausible-web-init.spec.ts
index a07509e966..5c8de75a6e 100644
--- a/tracker/test/plausible-web-init.spec.ts
+++ b/tracker/test/plausible-web-init.spec.ts
@@ -85,11 +85,11 @@ test('handles double-initialization of the script with a console.warn', async ({
await expectPlausibleInAction(page, {
action: () => page.click('button'),
- expectedRequests: [{ n: 'Purchase', p: { init: 2 } }] // bug or maybe feature: expected to be { init: 1 }
+ expectedRequests: [{ n: 'Purchase', p: { init: 1 } }]
})
})
-test('if there are two snippets on the page, the second one that loads interacts with the first one, no warning is emitted', async ({ page }, {
+test('if there are two snippets on the page, one wins, no warning is emitted', async ({ page }, {
testId
}) => {
const config = { ...DEFAULT_CONFIG }