Commit Graph

28 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
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
Karl-Aksel Puulmann 844c118135
ScriptV2: `plausible.init`, `customProperties`, snippet compilation, double initialization protection, `swc` (#5391)
* WIP: init() for plausible.js

* Reformat whitespace

* Handle double-initialization gracefully

* Script customProperties option

* Remove pageview-props from tracker script interpolation - its now an advanced config

* Add compile option for compiling the web snippet. This can then be copied to e.g. onboarding

* Switch minification from uglify-js to swc

* Use 4 passes for optimal output

* Remove use strict to remove it from output. Uglifyjs already removed this

* Minor optimization: Remove nulls from output via runningEngagementStart

* Minor optimization: Reduce onIgnoredEvent

* config_json -> config_js

* Dead attribute

* Remove guard for engagements

* More protective customProperties

* Object.assign

* chore: Bump tracker_script_version to 10

* No need for default in Object.assign

* Remove dead guard
2025-05-20 06:57:35 +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 a3db3af806
Avoid compiling pageleave variant and p.js (#5351)
* Avoid compiling pageleave variant

Instead, it's treated as a no-op. Tracker still serves it.

Compile wall time went down from 86s -> 46s.

* Vendor, no longer compile p.js

This was a legacy script variant which we keep for backwards
compatibility but little else.

* chore: Bump tracker_script_version to 6
2025-04-29 06:42:48 +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
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
RobertJoonas 5c72de0155
Experimental pageleave events (#4624)
* add experimental pageleave script variant

* also send pageleave events on SPA navigation

* disallow goals with 'pageleave' event name

* do not count pageleaves towards the event metric

* remove duplication in test file

* do not update sessions on pageleave events

* ignore pageleaves in the current time_on_page implementation

* make pageleave events not billable

* rename function

* Prevent multiple pageleaves being sent at the same time
2024-09-30 12:52:40 +00:00
Vini Brasil 8f6224b5de
Revenue tracking: goal conversions and tracker script (#3019)
* Add revenue average and total to Goal Conversions

* Add revenue option to tracker script

* Simplify revenue tracker script
2023-06-14 10:23:11 +01:00
RobertJoonas bc835895a2
Rename dimensions script to pageview-props (#2786)
* rename dimensions script to pageview-props

* commit mix.lock change
2023-03-27 17:50:15 +03:00
RobertJoonas 135471c32e
Add tagged-events script extension (#2333)
Adds a new script extension that allows tracking interactions with specific HTML elements on a website. For example - to track link clicks on one specific `<a>` element, you can tag it like this:

```html
<a href=... class="plausible-event-name=<your_event_name>">
```

And you can also tag the link with custom property names and values:

```html
<a href=... class="plausible-event-name=<your_event_name> plausible-event-<your_custom_prop>=<your_value>">
```

Tagging a link as above will send a custom event with the given name and props, if a `click` or `auxclick` browser event happens, and targets the link element.

The tracking behavior is somewhat different based on the HTML element type:  

- `<a>` 
  - triggers on `click` and `auxclick` events
  - intercepts navigation based on the same rules as `outbound-links` and `file-downloads`
- `<form>`
  - triggers on `submit` event
  - always intercepts navigation (calls `form.submit()` after preventing default and sending the Plausible event)
- other (`<img>`, `<button>`, `<span>`, `<div>`, `<h2>`, etc ...)
  - triggers on `click` and `auxclick` events
  - does not prevent default to intercept possible navigation. Simply calls Plausible with the event name and props read from the element class list.
2022-11-21 16:17:44 +02: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
RobertJoonas 11654ddc07
Script extension additions (#1915)
* added data-include attribute to plausible.exclusions.js

* reorder extensions in filename when serving the plausible script

* fix formatting

* tweaks after review

* changelog update
2022-05-27 10:11:40 +03:00
RobertJoonas 199206babc
Dimensions continued (#1847)
* added the first version of dimensions extension

* finished dimensions script extension + updated tracking to use it

* script variants build
2022-04-25 10:56:11 +03:00
RobertJoonas 8616dd46fb
added file-downloads script extension (#1775)
* added file-downloads script extension

* fixed the issues and made it compatible with IE

* changelog update
2022-03-31 13:52:09 +03:00
Uku Taht 045268f080 Remove console log 2021-12-29 12:46:01 +02:00
Uku Taht e4b99dbad6
Fix Security error with localstorage access (#1568)
* Fix Security error with localstorage access

* Changelog
2021-12-29 11:08:11 +02:00
Uku Taht 23a4f37ab4 Add 'manual' script variant 2021-10-10 13:07:37 +02:00
Martin DONADIEU 56b485f2e6
feat: add variant local (#1218)
* Update plausible.js

* feat: add new variant

* feat: add allow-localhost

* Update tracker.ex

* Update compile.js

* Update plausible.js

* Update tracker.ex

* Update compile.js

* Update plausible.js
2021-08-13 15:13:25 +03:00
Uku Taht a378935aac Add IE11 compatibility mode 2021-06-17 10:55:45 +03:00
Vignesh Joglekar 7f3e55418b
Changes from static tracker script name assignments to dynamic (#786)
Co-authored-by: Uku Taht <Uku.taht@gmail.com>
2021-05-18 11:36:12 +03:00
Vignesh Joglekar fb7a3fef89
Adds data-exclude support and localStorage.plausible_ignore support (#489)
* Adds data-exclude support and plausible_ignore support

* Splits exclusion into separate script option

* localStorage parsing upgrades

* Additional script type additions

I'm unsure about the formatting decision in tracker.ex - lmk.

* Adds new compiled files

This will certainly have conflicts with my other PRs related to the tracking scripts right now, I'll make one extra PR after both are done to ensure they're consolidated into the compiled scripts.

* Moves localStorage blocker out of special script

* Changelog

* Second thoughts on localStorage exclusion

* Updates `*` to not match `/` or whitespace

* Fix formatting

* Removes zero-length asterisks

* Adds support for double glob, zero-length replacements

* Update to reduce size+allow localStorage exclude

Co-authored-by: Uku Taht <Uku.taht@gmail.com>
2021-01-22 13:32:40 +02:00
Gustavo Maronato 77ec529f19
Fix #401 (#402) 2020-11-10 10:11:01 +02:00
Uku Taht f0cbf33d7c
Add tracker feature to automatically track outbound links (#389)
* Add tracker feature to automatically track outbound links

* Precompute templates and allow aliases

* Fix /js/analytics.js

* Remove change to tracking code

* Update CHANGELOG
2020-11-03 11:09:50 +02:00
Uku Taht 4aa4dfdcaf
Hash mode (#299)
* Build tracker with hash mode

* Extract hash fragment in hash mode

* Serve new hash-based tracker
2020-08-25 10:56:36 +03:00
Uku Taht bb36bae15c
Inject base url to tracker at runtime (#78)
* Inject base url to tracker at runtime

* Cache tracker script for 1 hour
2020-06-11 09:30:00 +03:00
Uku Taht 8d36d27ca3 Default script to prod 2020-06-09 14:06:02 +03:00
Uku Taht 4bd12f03fc
Reduce script size (#68)
* Remove cookie code

* Compile tracker separetly from website assets

* Install uglify

* Help minifier reduce varaibles
2020-05-29 09:51:16 +03:00