Skip to content

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

Script v2: Do not send Form: Submission events for tagged forms (#5546)

Artur Pata04f455cbfc0f
diff --git a/tracker/npm_package/CHANGELOG.md b/tracker/npm_package/CHANGELOG.md
index 59692eb9e5..0133627502 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
+- Do not send "Form: Submission" event if the form is tagged
+
## [0.3.0] - 2025-06-27
- Remove now unnecessary navigation delays on link clicks and form submissions.
diff --git a/tracker/package.json b/tracker/package.json
index 3d3c462330..da87a7dc95 100644
--- a/tracker/package.json
+++ b/tracker/package.json
@@ -1,5 +1,5 @@
{
- "tracker_script_version": 21,
+ "tracker_script_version": 22,
"type": "module",
"scripts": {
"deploy": "node compile.js",
diff --git a/tracker/src/custom-events.js b/tracker/src/custom-events.js
index 218b021220..552ee7baa9 100644
--- a/tracker/src/custom-events.js
+++ b/tracker/src/custom-events.js
@@ -187,6 +187,10 @@ export function init() {
if (COMPILE_CONFIG && config.formSubmissions) {
function trackFormSubmission(e) {
if (e.target.hasAttribute('novalidate') || e.target.checkValidity()) {
+ if (COMPILE_TAGGED_EVENTS && isElementOrParentTagged(e.target, 0)) {
+ // If the form is tagged, we don't track it as a generic form submission.
+ return
+ }
track('Form: Submission', { props: { path: location.pathname } });
}
}
diff --git a/tracker/test/features-hierarchy-on-overlap.spec.ts b/tracker/test/features-hierarchy-on-overlap.spec.ts
index 17796f4019..16ce009c61 100644
--- a/tracker/test/features-hierarchy-on-overlap.spec.ts
+++ b/tracker/test/features-hierarchy-on-overlap.spec.ts
@@ -1,9 +1,16 @@
-import { expectPlausibleInAction, switchByMode } from './support/test-utils'
+import {
+ ensurePlausibleInitialized,
+ expectPlausibleInAction,
+ isEngagementEvent,
+ isPageviewEvent,
+ switchByMode
+} from './support/test-utils'
import { test, expect } from '@playwright/test'
import { LOCAL_SERVER_ADDR } from './support/server'
import { initializePageDynamically } from './support/initialize-page-dynamically'
import { mockManyRequests } from './support/mock-many-requests'
import { ScriptConfig } from './support/types'
+import { customSubmitHandlerStub } from './support/html-fixtures'
const DEFAULT_CONFIG: ScriptConfig = {
domain: 'example.com',
@@ -11,7 +18,7 @@ const DEFAULT_CONFIG: ScriptConfig = {
captureOnLocalhost: true
}
-for (const mode of ['legacy', 'web']) {
+for (const mode of ['legacy', 'web'] as const) {
test.describe(`outbound links, file downloads, tagged events features hierarchy on overlap legacy/v2 parity (${mode})`, () => {
test('sends only tagged event if the link is a tagged outbound download link', async ({
page
@@ -149,3 +156,100 @@ for (const mode of ['legacy', 'web']) {
})
})
}
+
+for (const mode of ['web', 'esm'] as const) {
+ test.describe(`form submissions and tagged events features hierarchy on overlap v2-specific (${mode})`, () => {
+ test('sends only tagged event if a form is tagged', async ({
+ page
+ }, { testId }) => {
+ const config = { ...DEFAULT_CONFIG, formSubmissions: true }
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: switchByMode(
+ {
+ web: config,
+ esm: `
+ <script type="module">
+ import { init, track } from '/tracker/js/npm_package/plausible.js';
+ init(${JSON.stringify(config)})
+ window.plausible = { l: true }
+ </script>
+ `
+ },
+ mode
+ ),
+ bodyContent: `
+ <form method="POST" class="plausible-event-name=A+Tagged+Form">
+ <input id="name" type="text" placeholder="Name" /><input type="submit" value="Submit" />
+ </form>
+ `
+ })
+
+ await expectPlausibleInAction(page, {
+ action: async () => {
+ await page.goto(url)
+ await ensurePlausibleInitialized(page)
+ await page.fill('input[type="text"]', 'Any Name')
+ await page.click('input[type="submit"]')
+ },
+ shouldIgnoreRequest: [isPageviewEvent, isEngagementEvent],
+ expectedRequests: [
+ {
+ n: 'A Tagged Form',
+ u: `${LOCAL_SERVER_ADDR}${url}`
+ }
+ ],
+ refutedRequests: [{ n: 'Form: Submission' }],
+ mockRequestTimeout: 1000
+ })
+ })
+
+ test('sends only tagged event if the form parent is tagged', async ({
+ page
+ }, { testId }) => {
+ const config = { ...DEFAULT_CONFIG, formSubmissions: true }
+ const { url } = await initializePageDynamically(page, {
+ testId,
+ scriptConfig: switchByMode(
+ {
+ web: config,
+ esm: `
+ <script type="module">
+ import { init, track } from '/tracker/js/npm_package/plausible.js';
+ init(${JSON.stringify(config)})
+ window.plausible = { l: true }
+ </script>
+ `
+ },
+ mode
+ ),
+ bodyContent: `
+ <div class="plausible-event-name--A+Tagged+Form">
+ <form onsubmit="${customSubmitHandlerStub}">
+ <input type="email" />
+ </form>
+ </div>
+ `
+ })
+
+ await expectPlausibleInAction(page, {
+ action: async () => {
+ await page.goto(url)
+ await ensurePlausibleInitialized(page)
+ await page.fill('input[type="email"]', '[email protected]')
+ await page.press('input[type="email"]', 'Enter')
+ },
+ shouldIgnoreRequest: [isPageviewEvent, isEngagementEvent],
+ expectedRequests: [
+ { n: 'A Tagged Form', u: `${LOCAL_SERVER_ADDR}${url}` }
+ ],
+ refutedRequests: [
+ {
+ n: 'Form: Submission'
+ }
+ ],
+ mockRequestTimeout: 1000
+ })
+ })
+ })
+}
diff --git a/tracker/test/form-submissions.spec.ts b/tracker/test/form-submissions.spec.ts
index 95b3a81444..6aa5b5d16c 100644
--- a/tracker/test/form-submissions.spec.ts
+++ b/tracker/test/form-submissions.spec.ts
@@ -1,12 +1,14 @@
-import { test, Page } from '@playwright/test'
+import { test } from '@playwright/test'
import { LOCAL_SERVER_ADDR } from './support/server'
import {
+ ensurePlausibleInitialized,
expectPlausibleInAction,
isEngagementEvent,
isPageviewEvent
} from './support/test-utils'
import { initializePageDynamically } from './support/initialize-page-dynamically'
import { ScriptConfig } from './support/types'
+import { customSubmitHandlerStub } from './support/html-fixtures'
const DEFAULT_CONFIG: ScriptConfig = {
domain: 'example.com',
@@ -21,12 +23,10 @@ test('does not track form submissions when the feature is disabled', async ({
testId,
scriptConfig: DEFAULT_CONFIG,
bodyContent: `
- <div>
- <form>
- <input type="text" /><input type="submit" value="Submit" />
- </form>
- </div>
- `
+ <form>
+ <input type="text" /><input type="submit" value="Submit" />
+ </form>
+ `
})
await expectPlausibleInAction(page, {
@@ -52,11 +52,9 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form method="GET">
<input id="name" type="text" placeholder="Name" /><input type="submit" value="Submit" />
</form>
- </div>
`
})
@@ -84,11 +82,9 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form onsubmit="${customSubmitHandlerStub}">
<input type="text" /><input type="submit" value="Submit" />
</form>
- </div>
`
})
@@ -154,12 +150,10 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form novalidate onsubmit="${customSubmitHandlerStub}">
<input type="email" />
<input type="submit" value="Submit" />
</form>
- </div>
`
})
@@ -188,12 +182,10 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form>
<input type="email" />
<input type="submit" value="Submit" />
</form>
- </div>
`
})
@@ -221,12 +213,10 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form id="form">
<input type="text" placeholder="Name" />
</form>
<button id="trigger-FormElement-submit" onclick="document.getElementById('form').submit()">Submit</button>
- </div>
`
})
@@ -254,7 +244,6 @@ test.describe('form submissions feature is enabled', () => {
testId,
scriptConfig: { ...DEFAULT_CONFIG, formSubmissions: true },
bodyContent: `
- <div>
<form onsubmit="${customSubmitHandlerStub}">
<h2>Form 1</h2>
<input type="text" /><input type="submit" value="Submit" />
@@ -263,7 +252,6 @@ test.describe('form submissions feature is enabled', () => {
<h2>Form 2</h2>
<input type="email" />
</form>
- </div>
`
})
@@ -297,18 +285,3 @@ test.describe('form submissions feature is enabled', () => {
})
})
})
-/**
- * This function ensures that the tracker script has attached the event listener before test is run.
- * Note that this race condition happens in the real world as well:
- * forms submitted before the tracker script is initialized will not be tracked.
- */
-function ensurePlausibleInitialized(page: Page) {
- return page.waitForFunction(() => (window as any).plausible?.l === true)
-}
-
-/**
- * This is a stub for custom form onsubmit handlers Plausible users may have on their websites.
- * Overriding onsubmit with a custom handler is common practice in web development for a variety of reasons (mostly UX),
- * so it's important to track form submissions from forms with such handlers.
- */
-const customSubmitHandlerStub = "event.preventDefault(); console.log('Form submitted')"
diff --git a/tracker/test/support/html-fixtures.ts b/tracker/test/support/html-fixtures.ts
new file mode 100644
index 0000000000..1694d1f48d
--- /dev/null
+++ b/tracker/test/support/html-fixtures.ts
@@ -0,0 +1,6 @@
+/**
+ * This is a stub for custom form onsubmit handlers Plausible users may have on their websites.
+ * Overriding onsubmit with a custom handler is common practice in web development for a variety of reasons (mostly UX),
+ * so this stub can be used in tests to simulate such behavior.
+ */
+export const customSubmitHandlerStub = "event.preventDefault(); console.log('Submitted')"
diff --git a/tracker/test/support/test-utils.js b/tracker/test/support/test-utils.js
index f11253278a..55f1be1ff6 100644
--- a/tracker/test/support/test-utils.js
+++ b/tracker/test/support/test-utils.js
@@ -202,4 +202,15 @@ export function switchByMode(cases, mode) {
default:
throw new Error(`Unimplemented mode: ${mode}`)
}
-}
\ No newline at end of file
+}
+
+/**
+ * This function ensures that the tracker script has attached the event listener before test is run.
+ * Note that this race condition happens in the real world as well:
+ * Events from features like form submissions, file downloads, outbound links, tagged events
+ * that work with event handlers registered on the document
+ * will not be tracked if the event happens before the tracker script has attached the event listener.
+ */
+export function ensurePlausibleInitialized(page) {
+ return page.waitForFunction(() =>(window.plausible?.l === true))
+}
diff --git a/tracker/test/support/types.ts b/tracker/test/support/types.ts
index e7a29f0504..800af7cf74 100644
--- a/tracker/test/support/types.ts
+++ b/tracker/test/support/types.ts
@@ -1,7 +1,7 @@
export type Options = {
hashBasedRouting: boolean
outboundLinks: boolean
- fileDownloads: boolean | string[]
+ fileDownloads: boolean | { fileExtensions: string[] }
formSubmissions: boolean
captureOnLocalhost: boolean
autoCapturePageviews: boolean
diff --git a/tracker/test/tagged-events.spec.ts b/tracker/test/tagged-events.spec.ts
index bda9a35ac1..5a3a98edce 100644
--- a/tracker/test/tagged-events.spec.ts
+++ b/tracker/test/tagged-events.spec.ts
@@ -527,7 +527,7 @@ test.describe('tagged events feature when using legacy .compat extension', () =>
const [[, trackingResponseTime], [, navigationTime]] =
await resolveWithTimestamps([trackingPromise, navigationPromise])
await expect(page.getByText('Subscription successful')).toBeVisible()
- expect(trackingResponseTime).toBeLessThan(navigationTime)
+ expect(trackingResponseTime).toBeLessThanOrEqual(navigationTime)
await expect(eventsApiMock.getRequestList()).resolves.toEqual([
expect.objectContaining({
n: 'Subscribe',