Skip to content

ScriptV2: NPM library codebase#5459

Merged
macobo merged 26 commits intomasterfrom
tracker-script-npm-2
Jun 4, 2025
Merged

ScriptV2: NPM library codebase#5459
macobo merged 26 commits intomasterfrom
tracker-script-npm-2

Conversation

@macobo
Copy link
Copy Markdown
Contributor

@macobo macobo commented Jun 3, 2025

Depends on #5449. If that gets rejected, I will rebase accordingly.

This PR is the start of work for creating a new NPM library for our tracker script off of the same tracker codebase.

import { init, track } from '@plausible/tracker' # or some other library name

init({ 
  domain: "plausible.io"
})

It:

  • Has typescript types (via a type declaration file)
  • Shares majority of tests with plausible-web (needs a rebase after Update playwright #5432 is merged)
  • Has protections against double-init/track-before-init, throwing in that case

Work left for the future:

  • Release process via CI
  • Renaming the library, MIT license, CHANGELOG.md and README.md
  • Commonjs variant. Work on this was in this PR but was hard to test for.

The PR also:

  • Refactors tests to be less susceptible to timing issues (specifically around pageview trigger timing)
  • Removes features from compile.js that allowed limiting what to compile. Not needed with the new compile being faster.

@github-actions
Copy link
Copy Markdown

github-actions bot commented Jun 3, 2025

Analyzed 1026 tracker script variants for size changes.
The following tables summarize the results, with comparison with the baseline version in parentheses.

Main variants:

Brotli Gzip Uncompressed
npm_package/plausible.js (new variant) 2328B 2671B 6275B
plausible-web.js 2270B (-11B / -0.5%) 2594B (-11B / -0.4%) 6103B (-20B / -0.3%)

Important legacy variants:

Brotli Gzip Uncompressed
plausible.js 1140B (+0B / 0%) 1328B (-5B / -0.4%) 2883B (+0B / 0%)
plausible.hash.js 1103B (+0B / 0%) 1294B (-4B / -0.3%) 2769B (+0B / 0%)
plausible.pageview-props.tagged-events.js 1842B (+0B / 0%) 2144B (-5B / -0.2%) 4797B (+0B / 0%)
plausible.file-downloads.hash.pageview-props.revenue.js 1666B (+9B / +0.5%) 1954B (-4B / -0.2%) 4143B (+4B / +0.1%)
plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js 2250B (+2B / +0.1%) 2655B (-5B / -0.2%) 5941B (+4B / +0.1%)

Summary:

Brotli Gzip Uncompressed
Largest variant (plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js) 2250B (+2B / +0.1%) 2655B (-5B / -0.2%) 5941B (+4B / +0.1%)
Max change (plausible.compat.local.manual.pageview-props.tagged-events.js) 1602B (+14B / +0.9%) 1900B (-3B / -0.2%) 4120B (+4B / +0.1%)
Min change (plausible.compat.exclusions.local.manual.outbound-links.pageview-props.js) 1419B (-13B / -0.9%) 1712B (-8B / -0.5%) 3495B (+4B / +0.1%)
Median change 1696B (+0B / 0%) 2006.5B (-4B / -0.2%) 4322B (+4B / +0.1%)

In total, 378 variants brotli size increased and 256 variants brotli size decreased.

@macobo macobo marked this pull request as draft June 3, 2025 07:52
@macobo macobo force-pushed the tracker-script-npm-2 branch from c100ba0 to f3e2022 Compare June 3, 2025 08:14
@macobo macobo force-pushed the tracker-script-bundler branch from a9f0e49 to ac2a119 Compare June 3, 2025 10:40
@macobo macobo force-pushed the tracker-script-npm-2 branch 2 times, most recently from 6db2136 to f25673e Compare June 4, 2025 07:44
@macobo macobo force-pushed the tracker-script-npm-2 branch from 7c1b61f to 5e7f7de Compare June 4, 2025 07:48
@macobo macobo changed the base branch from tracker-script-bundler to master June 4, 2025 07:48
@macobo macobo force-pushed the tracker-script-npm-2 branch 2 times, most recently from 1b32310 to b514c00 Compare June 4, 2025 08:11
@macobo macobo marked this pull request as ready for review June 4, 2025 08:52
@macobo macobo requested review from apata and ukutaht and removed request for ukutaht June 4, 2025 08:52
@macobo macobo requested a review from ukutaht June 4, 2025 08:53
@macobo macobo force-pushed the tracker-script-npm-2 branch from 1e5a750 to 6d867b1 Compare June 4, 2025 08:59
Copy link
Copy Markdown
Contributor

@apata apata left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this! I appreciate the shared tests and that the npm module is tested at all. Lots of removed lines and small nuisances fixed.

@@ -0,0 +1,243 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Less repetition in tests, very nice!

macobo and others added 2 commits June 4, 2025 13:36
Co-authored-by: Artur Pata <artur.pata@gmail.com>
@macobo macobo added this pull request to the merge queue Jun 4, 2025
Merged via the queue into master with commit 595e71a Jun 4, 2025
10 checks passed
@Daniel15
Copy link
Copy Markdown

Daniel15 commented Sep 1, 2025

If you search Google for "Plausible npm", the old package is still returned, and the new one isn't in the results at all. Most people are still downloading the old script rather than the new one, based on npm download stats. Do you have plans to mark the old one as deprecated at some point?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants