Commit Graph

21 Commits

Author SHA1 Message Date
Artur Pata cc5ca4b752
Script v2: Apply prettier (#5718)
* Apply prettier

* Fix issue with quotes in test setup

* Fix other issues with customSubmitHandlerStub

* Fix format with cookies fixtures
2025-09-15 11:21:27 +00:00
Artur Pata 9c35a0f47c
Script v2: Handle exceptions on clicks on links within svgs (#5538)
* Access href attribute more defensively to avoid uncaught errors

WIP

chore: Bump tracker_script_version to 22

Try accept limitation

Clarify issue with tagged link clicks within svgs

Fix potential error chain accessing object

Revert fixes about tagged events props

* Refactor script tests
2025-08-04 10:10:08 +00:00
Artur Pata 04f455cbfc
Script v2: Do not send Form: Submission events for tagged forms (#5546)
* Do not send duplicate tagged form events

* Update changelog

* chore: Bump tracker_script_version to 22

* Share HTML fixture

---------

Co-authored-by: apata <apata@users.noreply.github.com>
2025-07-08 08:01:14 +00:00
Artur Pata 74dddc241c
Script v2: Remove delays except when using .compat extension (#5465)
* Make delays conditional on COMPILE_COMPAT, otherwise rely on keepalive fetch

* Add tests

* Fix webkit related test file-download feature tests issues

* Remove ternaries for scriptConfig in tests

* Make sure .compat scripts don't contain unreachable code blocks

* Test legacy .compat extension in a separate block

* Tests for tagged events

* Remove extraneous tests, add needed tests

* Add tests for v2 features overlap and outbound link clicks

* Add changelog, add current behaviour test for outbound links

* Remove mockRequest, unify API

* Fix coverage of legacy compat navigation, add back interactive event test

* chore: Bump tracker_script_version to 21

* Update specs

* Fix issues with _ variable

* Fix args

* Types and formatting cleanup

---------

Co-authored-by: apata <apata@users.noreply.github.com>
2025-06-27 12:10:02 +00:00
Karl-Aksel Puulmann 1f86ae55ef
ScriptV2: `callback`, file downloads and `logging` changes (#5514)
* Make logging in tracker script configurable

* Improved callbacks

* Improved fileDownloads

* Export DEFAULT_FILE_TYPES

* Cross-browser compatibility

* Rename

* Rename a test

* chore: Bump tracker_script_version to 20
2025-06-19 08:45:20 +00:00
Karl-Aksel Puulmann 595e71a399
ScriptV2: NPM library codebase (#5459)
* Compile NPM modules into commonjs and esm modules

Note that since the module uses only vanilla es5 outside the export,
it's safe to just append to the minified output

* package.json, Typescript definition, generation for commonjs

* Scrap commonjs for the moment

Running into too big rabbit holes trying to get it off the ground

* Shared tests for npm and web

* Better way to identify npm module in fixture

* Simplify code

* Add NPM library specific tests

* Fix a flaky test

* Standardize a callsite

* npm_package standardize on

* Make legacy variant code more explicit

* Wrap code pre and post

* analyze-sizes.js should work with npm_package

* Port test changes from PR #5432

* Update outbound link feature test

* Await callInit

* Smaller npm module - mangle variables

* Clean up wrapping code

* chore: Bump tracker_script_version to 15

* Add debug messaging to expect

* Remove dead code

* Refactor, account for initialization delays in tests

* Remove needless features from compiler

As the compiler currently manages to compile everything in ~3 seconds,
the `features`/`compileIds` feature serves no more point. Scrapping it

* Re-inline readOutput

* Update tracker/test/plausible-npm.spec.js

Co-authored-by: Artur Pata <artur.pata@gmail.com>

* Remove redundant comment

---------

Co-authored-by: Artur Pata <artur.pata@gmail.com>
2025-06-04 10:47:44 +00:00
Artur Pata a01f8e1c05
Update playwright (#5432)
* Refactor mock-many-requests

* Add commented fallback route

* Fix outbound link tracking expectation, fix file downloads import

* Remove console logs

* Add test skips and remove unnecessary skip

* Revert test name change

* Remove webkit related conditional in test expectations

* chore: Bump tracker_script_version to 13

---------

Co-authored-by: apata <apata@users.noreply.github.com>
2025-06-03 09:03:55 +00:00
Artur Pata 9be012362c
Track form submission events (#5381)
* Track form submissions

* chore: Bump tracker_script_version to 11

* Check that script loadedin tests

* Track form submissions

* Fix format

* Fix missing form submit handler

* Make id casing consistent

* Automatically sync compiled script snippet in dynamic test pages

* Enforce format manually

* Skip one test on Firefox, explain reason for stub

* Revert "Automatically sync compiled script snippet in dynamic test pages"

This reverts commit 7622b23f31.

---------

Co-authored-by: apata <apata@users.noreply.github.com>
2025-05-26 06:30:21 +00:00
Karl-Aksel Puulmann 96abac2d4e
ScriptV2: plausible-main, script size measurements (#5375)
* analyze-sizes.js script

* Start work on plausible-main

Size changes:
```
Main variants:
|                                 | Brotli | Gzip  | Uncompressed |
| ------------------------------- | ------ | ----- | ------------ |
| plausible-main.js (new variant) | 2344B  | 2675B | 6062B        |

Important legacy variants:
|                                                                                                   | Brotli               | Gzip                 | Uncompressed          |
| ------------------------------------------------------------------------------------------------- | -------------------- | -------------------- | --------------------- |
| plausible.js                                                                                      | 1086B (-6B / -0.5%)  | 1283B (+5B / +0.4%)  | 2684B (+4B / +0.1%)   |
| plausible.hash.js                                                                                 | 1078B (+24B / +2.3%) | 1268B (+29B / +2.3%) | 2642B (+76B / +3%)    |
| plausible.pageview-props.tagged-events.js                                                         | 1846B (+57B / +3.2%) | 2158B (+63B / +3%)   | 4739B (+185B / +4.1%) |
| plausible.file-downloads.hash.pageview-props.revenue.js                                           | 1669B (+62B / +3.9%) | 1958B (+54B / +2.8%) | 4097B (+175B / +4.5%) |
| plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js | 2286B (+81B / +3.7%) | 2692B (+84B / +3.2%) | 5978B (+280B / +4.9%) |

Summary:
|                                                                                                                     | Brotli               | Gzip                 | Uncompressed            |
| ------------------------------------------------------------------------------------------------------------------- | -------------------- | -------------------- | ----------------------- |
| Largest variant (plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js) | 2286B (+81B / +3.7%) | 2692B (+84B / +3.2%) | 5978B (+280B / +4.9%)   |
| Max change (plausible.exclusions.hash.local.manual.outbound-links.pageview-props.js)                                | 1460B (+80B / +5.8%) | 1755B (+83B / +5%)   | 3557B (+241B / +7.3%)   |
| Min change (plausible.js)                                                                                           | 1086B (-6B / -0.5%)  | 1283B (+5B / +0.4%)  | 2684B (+4B / +0.1%)     |
| Median change                                                                                                       | 1723B (+65B / +3.9%) | 2037B (+70B / +3.6%) | 4335B (+220.5B / +5.3%) |
```

* Janky conditionals for COMPILE_CONFIG

Results in no size increase for historical scripts:

Main variants:
|                                 | Brotli | Gzip  | Uncompressed |
| ------------------------------- | ------ | ----- | ------------ |
| plausible-main.js (new variant) | 2339B  | 2672B | 6058B        |

Important legacy variants:
|                                                                                                   | Brotli          | Gzip                | Uncompressed    |
| ------------------------------------------------------------------------------------------------- | --------------- | ------------------- | --------------- |
| plausible.js                                                                                      | 1092B (0B / 0%) | 1280B (+2B / +0.2%) | 2680B (0B / 0%) |
| plausible.hash.js                                                                                 | 1054B (0B / 0%) | 1241B (+2B / +0.2%) | 2566B (0B / 0%) |
| plausible.pageview-props.tagged-events.js                                                         | 1789B (0B / 0%) | 2097B (+2B / +0.1%) | 4554B (0B / 0%) |
| plausible.file-downloads.hash.pageview-props.revenue.js                                           | 1607B (0B / 0%) | 1906B (+2B / +0.1%) | 3922B (0B / 0%) |
| plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js | 2205B (0B / 0%) | 2610B (+2B / +0.1%) | 5698B (0B / 0%) |

Summary:
|                                                                                                                     | Brotli                   | Gzip                     | Uncompressed             |
| ------------------------------------------------------------------------------------------------------------------- | ------------------------ | ------------------------ | ------------------------ |
| Largest variant (plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js) | 2205B (0B / 0%)          | 2610B (+2B / +0.1%)      | 5698B (0B / 0%)          |
| Max change (plausible.js)                                                                                           | 1092B (0B / 0%)          | 1280B (+2B / +0.2%)      | 2680B (0B / 0%)          |
| Median change                                                                                                       | 1655B (0B / 0%)          | 1969B (+2B / +0.1%)      | 4116B (0B / 0%)          |

* plausible-main: no COMPILE_EXCLUSIONS

Size change from this:
|                   | Brotli              | Gzip                  | Uncompressed          |
| ----------------- | ------------------- | --------------------- | --------------------- |
| plausible-main.js | 2223B (-116B / -5%) | 2533B (-139B / -5.2%) | 5710B (-348B / -5.7%) |

* Rework server.js to be less compiler-aware

* Add first test for plausible-main.js

* Tests for plausible-main variant

* Remove report-sizes.js

* Tracker - report size changes in a comment

* Update summary notes

* Handle outbound links not being captured by network tab in webkit

See: https://3.basecamp.com/5308029/buckets/39034214/card_tables/cards/8429433464

* Solve a TODO

* Sign in comment

* Update pin

* Mock file downloads

* Update base branch

* Pin add-and-commit as per cenk request

* Use bot for checkout

* Attempt to reduce permissions

* Smaller fetch depth

* Fix rebase
2025-05-12 06:55:54 +00:00
Karl-Aksel Puulmann 7265d04a8c
ScriptV2: Improved tracker compile.js (#5363)
* Add CLI arguments to compile.js and logging

* Rename folder

* Extract compile code, es modules

* Add a progress bar

* Remove handlebars

* Update report-sizes

* Remove debug code

* inline

* More generous split

* Allow positional arguments for compiling

* Add watch option to compile

* Add compileFile logic

* Most tests run under playwright

* All tests runnable

* Update playwright, remove hack

Note that upgrading to latest failed due to a new test failure. This
might be due to a chrome update.

* Compile script on the fly for tests

* Minor refactor for compileAll

* es module for generate-variants.js

* Allow passing suffix to compilation script - this can be used to generate separate files for comparison

* Fix positionals

* Switch from 2 passes to 1 pass

Did some data analysis on this data:
- Compared to master, 1 pass increased brotli size by 0.7%, 2 passes 0.4%.

Given the change is insignificant enough, we can ignore it for now

The increase is likely due to order of operations in compilation and
some inlined functions getting lost.

* Move customEvents.js to plausible.js

* Clean up API

* Suffix default

* Rework variants.json, globals stored there

* Add more variants under test

* Distribute work across multiple worker threads

Compile time went on my machine from 60s -> 30s

* Fixup server

* Update canSkipCompile

* chore: Bump tracker_script_version to 7

* Update scripts

* Update node-version

* Experiment with adding a small delay to page

* Casing

* rename variable

* Update help text

* features -> compileIds, backport functionality from other branch
2025-05-08 07:05:09 +00:00
Karl-Aksel Puulmann 539f4a3f98
Tracker script version reported to /api/event (#5198)
* WIP

* tracker_script_version over reportedVersion

Will make it easier to grep for this thing

* CHANGELOG.md

* Only add tracker/package.json in workflow commit

* chore: Bump tracker_script_version to 1

* Show tracker_script_version is reported in tracker tests

* Track `engagement_time` while accounting for time page is blurred (#5206)

Consider this scenario:
1. User opens page /blog
2. 3 seconds later, alt-tabs to another window
3. minute later, alt-tabs to the page and closes it

Previously, this would be reported as 1m03s engagement_time. Now this
would be reported as 3s engagement_time.

* remove redundant engagement cooldown (#5199)

---------

Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com>
2025-03-18 07:30:17 +00:00
Karl-Aksel Puulmann 75a8b19571
time-on-page: track engagement time in plausible.js (#5087)
* Track engagement time in plausible.js

* Test page going between background and foreground without sending

* Rename some variables

* Add comment on when we send information or not

* Extract a function

* Test SPA navigation

* Raise threshold

* Update tracker/src/plausible.js

Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com>

---------

Co-authored-by: RobertJoonas <56999674+RobertJoonas@users.noreply.github.com>
2025-02-20 10:31:24 +00:00
Karl-Aksel Puulmann 63b732eb36
Tracker: Track only engagements (pageleave extension) (#5039)
* Tracker: Remove tracking pageleaves

* chore(tracker): rename html files

* Update comment
2025-02-06 11:35:10 +00:00
Karl-Aksel Puulmann 334f8bbdfc
Tracker: Send `engagement` events on visibilitychange (if pageleave extension) (#5023)
* Separate `engagement` event triggered on visibilitychange

* Set up shared tests for engagement requests and pageleaves

* Store scroll_depth on engagement events

* Move test to be shared

* Update cache_store
2025-01-29 06:30:41 +00:00
Karl-Aksel Puulmann 8d238cd340
Scroll depth: Register `pageleave` listener earlier (#5000)
* Register `pageleave` listener earlier

Currently, pageleave listener is only registered when a successful
response is received from plausible API.

After this change pageleave listener is registered immediately when
pageview is triggered, hopefully increasing capture rate.

* Codespell
2025-01-22 09:30:23 +00:00
RobertJoonas f262bea316
Average Scroll Depth Metric: Track custom props on pageleaves (#4964)
* improve tracker test util to handle custom props

* track pageleave props

* make test util more powerful

* also test with pageview-props extension

* add test for sending props on hash navigation

* simplify tracker logic around currentPageLeaveURL

* clarify the intent of a timeout by extracting a fn

* bugfix: stop sending pageleaves from ignored pages

+ fix the test to actually fail if this doesn't work.

* refactor: drop expectCustomEvent in favour of the new test util

* add test for pageleave props ingestion

* add query tests

* improve test util readability
2025-01-16 15:06:19 +00:00
RobertJoonas 2a7d02b6f0
Average Scroll Depth Metric: extracted tracker changes (#4826)
* (cherry-pick) implement scroll depth tracking under pageleave variant

* drop unnecessary vars

* remove unused require

* add scroll depth tests

* improve error messages in test util

* reevaluate currentDocumentHeight on page load

* account for dynamically loaded content when initializing documnent height

* remove all semicolons from tracker specs

* allow window and document globals in tracker eslint

* tweak global tracker dir eslint rules

* update comment

* reevaluate document height on scroll

* add test

* remove unneccessary timeout
2024-11-21 14:29:52 +00:00
RobertJoonas 37116a2b12
Add tests for the pageleave script (#4744)
* move util function to util module

* move playwright config file to tracker dir root

* update Playwright + add gitignores

* Playwright: enable reuseExistingServer (non-CI env)

* store tracker src copies to avoid re-compilation in dev env

* test pageleave on simple navigation

* fix test util fn

* rename local_test npm script

* make test util able to expect multiple requests

* test pageleaves in SPAs

* test pageleave with manual URL

* test pageleave not sent in manual when pageview not triggered

* extend util fn to refute event requests

* test pageleaves not sent in excluded hash pages

* store hashes instead of file copies to detect /tracker/src changes

* drop async

* speed up test suite
2024-10-28 15:30:03 +00:00
Uku Taht 43bf7dd09f
Use user-agent instead of screen_width to get device type (#2711)
* Use user-agent instead of screen_width to get device type

Co-authored-by: eriknakata <erik.nakata5@gmail.com>

* Fix credo

* Log on unhandled UAInspector device type

* Make 'browser' the default tab in devices report

* Remove device tooltip

* Remove screen_width from ingestion completely

* Remove browserstack harness, run playwright directly

* Select meta key based on OS platform

* Run CI tests in parallel

* Improve device match readability

* Add changelog

---------

Co-authored-by: eriknakata <erik.nakata5@gmail.com>
2023-03-02 11:04:01 +01:00
RobertJoonas f75d5106f0
Rework outbound links and file downloads (#2208)
* moved custom event code to the bottom + fix indentation

* add handlebars helper fn + extract getLinkEl fn

* extract isOutboundLink function

* extract shouldFollowLink function

* remove middle and click variables

* use only one click handler for outbounds and downloads

* extract sendLinkClickEvent function

* add error handling when script compilation fails

* use callback instead of fixed timeout

* do not prevent default if externally prevented + test

* add more tests

* generate tracker files in priv/tracker/js

* update changelog

* requested changes after review

* regenerate tracker files

* use return instead of else if

* move middleMouseButton outside the function
2022-09-29 14:12:35 +03:00
Uku Taht 99fd101135
Add basic test harness for browserstack/playwright (#1961)
* Add basic test harness for browserstack/playwright

* Refactor the tests

* added the first test for outbound links

* tests for outbound-links and file-downloads

* added more browser versions to test on

* Lint tracker test files

* Update harness.js with BrowserStack example

* Fix Playwright request mocks

* Add test harness to CI

* Remove Safari on Windows from browsers list

Co-authored-by: Robert <robertjoonas16@gmail.com>
Co-authored-by: Vinicius Brasil <vini@hey.com>
2022-08-04 11:50:09 +03:00