Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
ScriptV2: plausible-main, script size measurements (#5375)
Karl-Aksel Puulmann96abac2d4e2c
diff --git a/.github/workflows/node.yml b/.github/workflows/node.ymlindex d2ddefe800..b5aec1de0f 100644--- a/.github/workflows/node.yml+++ b/.github/workflows/node.yml@@ -32,4 +32,3 @@ jobs:- run: npm run check-format --prefix ./assets- run: npm run test --prefix ./assets- run: npm run deploy --prefix ./tracker- - run: npm run report-sizes --prefix ./trackerdiff --git a/.github/workflows/tracker-version-bump.yml b/.github/workflows/tracker-script-update.ymlsimilarity index 61%rename from .github/workflows/tracker-version-bump.ymlrename to .github/workflows/tracker-script-update.ymlindex 35d6bbf4e1..ffa858b7cc 100644--- a/.github/workflows/tracker-version-bump.yml+++ b/.github/workflows/tracker-script-update.yml@@ -1,4 +1,4 @@-name: "Tracker: Increment Reported Version"+name: "Tracker script update"on:pull_request:@@ -8,10 +8,11 @@ on:- 'tracker/package-lock.json'jobs:- tracker-increment-reported-version:+ tracker-script-update:runs-on: ubuntu-latestpermissions:- contents: write+ pull-requests: write+ contents: readsteps:- name: Checkout codeuses: actions/checkout@v4@@ -19,7 +20,7 @@ jobs:repository: ${{ github.event.pull_request.head.repo.full_name }}ref: ${{ github.event.pull_request.head.ref }}token: ${{ secrets.PLAUSIBLE_BOT_GITHUB_TOKEN }}- fetch-depth: 0+ fetch-depth: 1- name: Checkout master for comparisonuses: actions/checkout@v4@@ -27,8 +28,15 @@ jobs:ref: masterpath: master-branch- - name: Install jq- run: sudo apt-get install jq -y+ - name: Install jq and clickhouse-local+ run: |+ sudo apt-get install apt-transport-https ca-certificates dirmngr+ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 8919F6BD2B48D754+ echo "deb https://packages.clickhouse.com/deb stable main" | sudo tee \+ /etc/apt/sources.list.d/clickhouse.list+ sudo apt-get update++ sudo apt-get install jq clickhouse-server -y- name: Compare and increment tracker_script_versionid: increment@@ -64,12 +72,38 @@ jobs:fi- name: Commit and push changes- uses: EndBug/add-and-commit@v9+ uses: EndBug/add-and-commit@a94899bca583c204427a224a7af87c02f9b325d5if: steps.increment.outputs.changed == 'true'with:message: 'chore: Bump tracker_script_version to ${{ steps.increment.outputs.version }}'github_token: ${{ secrets.PLAUSIBLE_BOT_GITHUB_TOKEN }}add: |- tracker/package.json- # Uncomment this once they're whitelisted by CLA agent- # default_author: github_actions++ - name: Compile tracker code+ run: |+ cd master-branch/tracker+ npm install+ node compile.js --suffix master+ cp ../priv/tracker/js/plausible* ../../priv/tracker/js/++ cd ../../tracker+ npm install+ node compile.js --suffix pr++ - name: Run script size analyzer and set output+ id: analyze+ run: |+ cd tracker+ OUT=$(node compiler/analyze-sizes.js --baselineSuffix master --currentSuffix pr)+ # Set multiline output+ echo "sizes<<EOF" >> $GITHUB_OUTPUT+ echo "$OUT" >> $GITHUB_OUTPUT+ echo "EOF" >> $GITHUB_OUTPUT++ - name: Comment script size report on PR+ uses: thollander/[email protected]+ with:+ message: |+ ${{ steps.analyze.outputs.sizes }}+ comment-tag: size-reportdiff --git a/.gitignore b/.gitignoreindex 006bc537fb..5e669089fa 100644--- a/.gitignore+++ b/.gitignore@@ -44,6 +44,8 @@ npm-debug.log# Stored hash of source tracker files used in development environment# to detect changes in /tracker/src and avoid unnecessary compilation./tracker/compiler/last-hash.txt+# Temporary file used by analyze-sizes.js+/tracker/compiler/.analyze-sizes.json# test coverage directory/assets/coveragediff --git a/tracker/compiler/analyze-sizes.js b/tracker/compiler/analyze-sizes.jsnew file mode 100644index 0000000000..3ce6843f6f--- /dev/null+++ b/tracker/compiler/analyze-sizes.js@@ -0,0 +1,259 @@+/*+This script analyzes the size changes of tracker script variants across different versions and git branches.++Requires clickhouse-local to be installed.++To run it:+- Switch to the desired baseline branch+- Run `node compile.js --suffix baseline`+- Switch to the current branch+- Run `node compile.js --suffix current`+- Run `node compiler/analyze-sizes.js --baselineSuffix baseline --currentSuffix current`++It will output tables outlining tracker script size changes.+*/++import fs from 'fs'+import path from 'path'+import { fileURLToPath } from 'url'+import { execSync } from 'child_process'+import { parseArgs } from 'node:util'+import { markdownTable } from 'markdown-table'+import variantsFile from './variants.json' with { type: 'json' }++const { values } = parseArgs({+ options: {+ 'help': {+ type: 'boolean',+ },+ 'currentSuffix': {+ type: 'string',+ default: 'current'+ },+ 'baselineSuffix': {+ type: 'string',+ default: 'master'+ },+ 'usePreviousData': {+ type: 'boolean',+ default: false+ }+ }+})++const { currentSuffix, baselineSuffix } = values+const __dirname = path.dirname(fileURLToPath(import.meta.url))+const TRACKER_FILES_DIR = path.join(__dirname, "../../priv/tracker/js/")++const HEADER = ['', 'Brotli', 'Gzip', 'Uncompressed']++if (values.help) {+ console.log('Usage: node analyze-sizes.js master current')+ console.log('Options:')+ console.log(' --help Show this help message')+ console.log(' --currentSuffix The suffix of the current script variants (see suffix flag for compile.js). Default: current')+ console.log(' --baselineSuffix The suffix of the previous script variants (see suffix flag for compile.js). Default: master')+ console.log(' --usePreviousData Use data from a previous run, speeding up the analysis.')+ process.exit(0);+}+++let fileData+if (values.usePreviousData) {+ fileData = JSON.parse(fs.readFileSync(path.join(__dirname, '.analyze-sizes.json'), 'utf8'))+} else {+ fileData = readPlausibleScriptSizes()+ fs.writeFileSync(path.join(__dirname, '.analyze-sizes.json'), JSON.stringify(fileData))+}++const manualVariants = variantsFile.manualVariants.map((variant) => `'${variant.name}'`).join(', ')+const ctes = `+WITH+ array(${manualVariants}) as manual_variants,+ array(+ 'plausible.js',+ 'plausible.hash.js',+ 'plausible.pageview-props.tagged-events.js',+ 'plausible.file-downloads.hash.pageview-props.revenue.js',+ 'plausible.compat.exclusions.file-downloads.outbound-links.pageview-props.revenue.tagged-events.js'+ ) as important_variants,+ data AS (+ SELECT+ variant,+ not has(manual_variants, variant) as is_legacy_variant,+ sumIf(uncompressed, suffix = '${baselineSuffix}') as baseline_uncompressed,+ sumIf(gzip, suffix = '${baselineSuffix}') as baseline_gzip,+ sumIf(brotli, suffix = '${baselineSuffix}') as baseline_brotli,+ sumIf(uncompressed, suffix = '${currentSuffix}') as current_uncompressed,+ sumIf(gzip, suffix = '${currentSuffix}') as current_gzip,+ sumIf(brotli, suffix = '${currentSuffix}') as current_brotli,+ current_uncompressed - baseline_uncompressed as uncompressed_increase,+ current_gzip - baseline_gzip as gzip_increase,+ current_brotli - baseline_brotli as brotli_increase,+ ifNotFinite((current_uncompressed / baseline_uncompressed - 1.0) * 100.0, NULL) as uncompressed_increase_percentage,+ ifNotFinite((current_gzip / baseline_gzip - 1.0) * 100.0, NULL) as gzip_increase_percentage,+ ifNotFinite((current_brotli / baseline_brotli - 1.0) * 100.0, NULL) as brotli_increase_percentage+ FROM table+ GROUP BY variant+ )+`++const mainVariantResults = clickhouseLocal(`+ ${ctes}+ SELECT *+ FROM data+ WHERE not is_legacy_variant+ ORDER BY variant+`, fileData)++const legacyVariantResults = clickhouseLocal(`+ ${ctes}+ SELECT *+ FROM data+ WHERE is_legacy_variant AND has(important_variants, variant)+ ORDER BY length(variant)+`, fileData)++const rowAsMap = `+map(+ 'variant', variant,+ 'baseline_uncompressed', toString(baseline_uncompressed),+ 'baseline_gzip', toString(baseline_gzip),+ 'baseline_brotli', toString(baseline_brotli),+ 'current_uncompressed', toString(current_uncompressed),+ 'current_gzip', toString(current_gzip),+ 'current_brotli', toString(current_brotli),+ 'uncompressed_increase', toString(uncompressed_increase),+ 'gzip_increase', toString(gzip_increase),+ 'brotli_increase', toString(brotli_increase),+ 'uncompressed_increase_percentage', toString(uncompressed_increase_percentage),+ 'gzip_increase_percentage', toString(gzip_increase_percentage),+ 'brotli_increase_percentage', toString(brotli_increase_percentage)+)+`++const [summary] = clickhouseLocal(`+ ${ctes}+ SELECT+ count() AS total_variants,+ countIf(brotli_increase_percentage > 0.0) AS brotli_increase_percentaged_variants,+ countIf(brotli_increase_percentage < 0.0) AS brotli_decreased_variants,+ argMax(+ ${rowAsMap},+ brotli_increase_percentage+ ) AS max_increase_variant,+ argMin(+ ${rowAsMap},+ brotli_increase_percentage+ ) AS min_increase_variant,+ argMaxIf(+ ${rowAsMap},+ current_brotli,+ is_legacy_variant+ ) AS largest_variant,+ map(+ 'variant', 'Median change',+ 'baseline_uncompressed', toString(median(baseline_uncompressed)),+ 'baseline_gzip', toString(median(baseline_gzip)),+ 'baseline_brotli', toString(median(baseline_brotli)),+ 'current_uncompressed', toString(median(current_uncompressed)),+ 'current_gzip', toString(median(current_gzip)),+ 'current_brotli', toString(median(current_brotli)),+ 'uncompressed_increase', toString(median(uncompressed_increase)),+ 'gzip_increase', toString(median(gzip_increase)),+ 'brotli_increase', toString(median(brotli_increase)),+ 'uncompressed_increase_percentage', toString(median(uncompressed_increase_percentage)),+ 'gzip_increase_percentage', toString(median(gzip_increase_percentage)),+ 'brotli_increase_percentage', toString(median(brotli_increase_percentage))+ ) AS median_result+ FROM data+`, fileData)++console.log(`Analyzed ${summary.total_variants} tracker script variants for size changes.`)+console.log(`The following tables summarize the results, with comparison with the baseline version in parentheses.\n`)++console.log("Main variants:")+console.log(createMarkdownTable(mainVariantResults))++console.log("\nImportant legacy variants:")+console.log(createMarkdownTable(legacyVariantResults))++console.log("\nSummary:")+console.log(createMarkdownTable([+ { ...summary.largest_variant, variant: `Largest variant (${summary.largest_variant.variant})`},+ { ...summary.max_increase_variant, variant: `Max change (${summary.max_increase_variant.variant})`},+ { ...summary.min_increase_variant, variant: `Min change (${summary.min_increase_variant.variant})`},+ summary.median_result+]))++console.log(`\nIn total, ${summary.brotli_increase_percentaged_variants} variants brotli size increased and ${summary.brotli_decreased_variants} variants brotli size decreased.`)++function createMarkdownTable(rows) {+ return markdownTable([HEADER].concat(rows.map(markdownRow)))+}++function markdownRow(row) {+ if (Array.isArray(row)) {+ return row+ }++ const isNew = row.baseline_uncompressed === null++ return [+ isNew ? `${row.variant} (new variant)` : row.variant,+ sizeColumn(row, 'brotli'),+ sizeColumn(row, 'gzip'),+ sizeColumn(row, 'uncompressed')+ ]+}+++function sizeColumn(row, key) {+ const currentSize = row[`current_${key}`]+ const previousIncrease = row[`${key}_increase`]+ const increasePercentage = row[`${key}_increase_percentage`]+ if (previousIncrease === null) {+ return `${currentSize}B`+ } else {+ return `${currentSize}B (${addSign(previousIncrease)}B / ${formatPercentage(increasePercentage)})`+ }+}++function formatPercentage(value) {+ const prefix = +value > 0 ? '+' : ''+ return `${prefix}${Math.round(+value * 10) / 10}%`+}++function addSign(value) {+ return +value >= 0 ? `+${value}` : +value+}++function readPlausibleScriptSizes() {+ const files = fs.readdirSync(TRACKER_FILES_DIR).filter((filename) =>+ !['.gitkeep', 'p.js'].includes(filename) && (filename.includes(currentSuffix) || filename.includes(baselineSuffix))+ )++ return files.map((filename) => {+ const filePath = path.join(TRACKER_FILES_DIR, filename)+ const [_, variant, suffix] = /(.*)[.]js(.*)/.exec(filename)+ return {+ variant: `${variant}.js`,+ suffix,+ uncompressed: fs.statSync(filePath).size,+ gzip: execSync(`gzip -c -9 "${filePath}"`).length,+ brotli: execSync(`brotli -c -q 11 "${filePath}"`).length+ }+ })+}++function clickhouseLocal(sql, inputLines = null) {+ const options = {}+ if (inputLines) {+ options.input = inputLines.map(JSON.stringify).join("\n")+ }++ const result = execSync(`clickhouse-local --query="${sql}" --format=JSON ${inputLines ? "--input-format=JSONLines" : ""}`, options)+ const json = JSON.parse(result.toString())++ return json.data+}diff --git a/tracker/compiler/index.js b/tracker/compiler/index.jsindex 2a7091a56f..b94ce8148a 100644--- a/tracker/compiler/index.js+++ b/tracker/compiler/index.js@@ -13,7 +13,6 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url))const DEFAULT_GLOBALS = {COMPILE_HASH: false,COMPILE_OUTBOUND_LINKS: false,- COMPILE_EXCLUSIONS: false,COMPILE_COMPAT: false,COMPILE_LOCAL: false,COMPILE_MANUAL: false,@@ -21,7 +20,9 @@ const DEFAULT_GLOBALS = {COMPILE_PAGEVIEW_PROPS: false,COMPILE_TAGGED_EVENTS: false,COMPILE_REVENUE: false,- COMPILE_TRACKER_SCRIPT_VERSION: packageJson.tracker_script_version+ COMPILE_EXCLUSIONS: false,+ COMPILE_TRACKER_SCRIPT_VERSION: packageJson.tracker_script_version,+ COMPILE_CONFIG: false}export async function compileAll(options = {}) {diff --git a/tracker/compiler/variants.json b/tracker/compiler/variants.jsonindex cc26f65475..699b3faff8 100644--- a/tracker/compiler/variants.json+++ b/tracker/compiler/variants.json@@ -1,6 +1,22 @@{"manualVariants": [-+ {+ "name": "plausible-main.js",+ "features": [+ "plausible-main"+ ],+ "globals": {+ "COMPILE_CONFIG": true,+ "COMPILE_HASH": true,+ "COMPILE_OUTBOUND_LINKS": true,+ "COMPILE_LOCAL": true,+ "COMPILE_MANUAL": true,+ "COMPILE_FILE_DOWNLOADS": true,+ "COMPILE_PAGEVIEW_PROPS": true,+ "COMPILE_TAGGED_EVENTS": true,+ "COMPILE_REVENUE": true+ }+ }],"legacyVariants": [{diff --git a/tracker/package-lock.json b/tracker/package-lock.jsonindex 31789c485a..26cea1e5b4 100644--- a/tracker/package-lock.json+++ b/tracker/package-lock.json@@ -17,7 +17,8 @@"@types/node": "^22.13.4","eslint": "^9.20.1","eslint-plugin-playwright": "^2.2.0",- "express": "^4.21.2"+ "express": "^4.21.2",+ "markdown-table": "^3.0.4"}},"node_modules/@aashutoshrathi/word-wrap": {@@ -1055,21 +1056,6 @@"node": ">= 0.6"}},- "node_modules/fsevents": {- "version": "2.3.2",- "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz",- "integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",- "dev": true,- "hasInstallScript": true,- "license": "MIT",- "optional": true,- "os": [- "darwin"- ],- "engines": {- "node": "^8.16.0 || ^10.6.0 || >=11.0.0"- }- },"node_modules/function-bind": {"version": "1.1.2","resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",@@ -1394,6 +1380,17 @@"integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==","dev": true},+ "node_modules/markdown-table": {+ "version": "3.0.4",+ "resolved": "https://registry.npmjs.org/markdown-table/-/markdown-table-3.0.4.tgz",+ "integrity": "sha512-wiYz4+JrLyb/DqW2hkFJxP7Vd7JuTDm77fvbM8VfEQdmSMqcImWeeRbHwZjBjIFki/VaMK2BhFi7oUUZeM5bqw==",+ "dev": true,+ "license": "MIT",+ "funding": {+ "type": "github",+ "url": "https://github.com/sponsors/wooorm"+ }+ },"node_modules/math-intrinsics": {"version": "1.1.0","resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",diff --git a/tracker/package.json b/tracker/package.jsonindex 0df0e8713a..c7294f3ca2 100644--- a/tracker/package.json+++ b/tracker/package.json@@ -1,11 +1,10 @@{- "tracker_script_version": 8,+ "tracker_script_version": 9,"type": "module","scripts": {"deploy": "node compile.js","test": "npx playwright test","test:local": "npx playwright test",- "report-sizes": "node report-sizes.js","start": "node test/support/server.js"},"license": "MIT",@@ -21,6 +20,7 @@"@types/node": "^22.13.4","eslint": "^9.20.1","eslint-plugin-playwright": "^2.2.0",- "express": "^4.21.2"+ "express": "^4.21.2",+ "markdown-table": "^3.0.4"}}diff --git a/tracker/report-sizes.js b/tracker/report-sizes.jsdeleted file mode 100644index 85a87b0099..0000000000--- a/tracker/report-sizes.js+++ /dev/null@@ -1,28 +0,0 @@-import fs from 'fs'-import path from 'path'-import { execSync } from 'child_process'--const PrivTrackerDir = '../priv/tracker/js/'--const toReport = [- 'plausible.js',- 'plausible.compat.js',- 'plausible.manual.js',- 'plausible.hash.js'-];--const results = [];--toReport.forEach((filename) => {- const filePath = path.join(PrivTrackerDir, filename);- if (fs.statSync(filePath).isFile()) {- results.push({- 'Filename': filename,- 'Real Size (Bytes)': fs.statSync(filePath).size,- 'Gzipped Size (Bytes)': execSync(`gzip -c -9 "${filePath}"`).length,- 'Brotli Size (Bytes)': execSync(`brotli -c -q 11 "${filePath}"`).length- });- }-});--console.table(results)diff --git a/tracker/src/plausible.js b/tracker/src/plausible.jsindex e20320736f..64c3f7e642 100644--- a/tracker/src/plausible.js+++ b/tracker/src/plausible.js@@ -8,8 +8,15 @@} else {var scriptEl = document.currentScript;}- var endpoint = scriptEl.getAttribute('data-api') || defaultEndpoint()- var dataDomain = scriptEl.getAttribute('data-domain')++ var config = {}++ if (COMPILE_CONFIG) {+ config = "<%= @config_json %>"+ }++ var endpoint = COMPILE_CONFIG ? config.endpoint : (scriptEl.getAttribute('data-api') || defaultEndpoint())+ var dataDomain = COMPILE_CONFIG ? config.domain : scriptEl.getAttribute('data-domain')function onIgnoredEvent(eventName, reason, options) {if (reason) console.warn('Ignoring Event: ' + reason);@@ -133,7 +140,7 @@runningEngagementStart = nullcurrentEngagementTime = 0- if (COMPILE_HASH) {+ if (COMPILE_HASH && (!COMPILE_CONFIG || config.hash)) {payload.h = 1}@@ -175,7 +182,7 @@maxScrollDepthPx = getCurrentScrollDepthPx()}- if (!COMPILE_LOCAL) {+ if (!(COMPILE_LOCAL && (!COMPILE_CONFIG || config.local))) {if (/^localhost$|^127(\.[0-9]+){0,2}\.[0-9]+$|^\[::1?\]$/.test(location.hostname) || location.protocol === 'file:') {return onIgnoredEvent(eventName, 'localhost', options)}@@ -190,7 +197,7 @@} catch (e) {}- if (COMPILE_EXCLUSIONS) {+ if (COMPILE_EXCLUSIONS && (!COMPILE_CONFIG || config.exclusions)) {var dataIncludeAttr = scriptEl && scriptEl.getAttribute('data-include')var dataExcludeAttr = scriptEl && scriptEl.getAttribute('data-exclude')@@ -204,7 +211,7 @@function pathMatches(wildcardPath) {var actualPath = location.pathname- if (COMPILE_HASH) {+ if (COMPILE_HASH && (!COMPILE_CONFIG || config.hash)) {actualPath += location.hash}@@ -216,7 +223,7 @@payload.n = eventNamepayload.v = COMPILE_TRACKER_SCRIPT_VERSION- if (COMPILE_MANUAL) {+ if (COMPILE_MANUAL && (!COMPILE_CONFIG || config.manual)) {var customURL = options && options.upayload.u = customURL ? customURL : location.href@@ -235,13 +242,13 @@if (options && options.interactive === false) {payload.i = false}- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {if (options && options.revenue) {payload.$ = options.revenue}}- if (COMPILE_PAGEVIEW_PROPS) {+ if (COMPILE_PAGEVIEW_PROPS && (!COMPILE_CONFIG || config.pageviewProps)) {var propAttributes = scriptEl.getAttributeNames().filter(function (name) {return name.substring(0, 6) === 'event-'})@@ -257,7 +264,7 @@payload.p = props}- if (COMPILE_HASH) {+ if (COMPILE_HASH && (!COMPILE_CONFIG || config.hash)) {payload.h = 1}@@ -309,11 +316,11 @@trigger.apply(this, queue[i])}- if (!COMPILE_MANUAL) {+ if (!(COMPILE_MANUAL && (!COMPILE_CONFIG || config.manual))) {var lastPage;function page(isSPANavigation) {- if (!COMPILE_HASH) {+ if (!(COMPILE_HASH && (!COMPILE_CONFIG || config.hash))) {if (isSPANavigation && lastPage === location.pathname) return;}@@ -323,7 +330,7 @@var onSPANavigation = function() {page(true)}- if (COMPILE_HASH) {+ if (COMPILE_HASH && (!COMPILE_CONFIG || config.hash)) {window.addEventListener('hashchange', onSPANavigation)} else {var his = window.history@@ -386,7 +393,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {var link = getLinkEl(event.target)var hrefWithoutQuery = link && link.href && link.href.split('?')[0]- if (COMPILE_TAGGED_EVENTS) {+ if (COMPILE_TAGGED_EVENTS && (!COMPILE_CONFIG || config.taggedEvents)) {if (isElementOrParentTagged(link, 0)) {// Return to prevent sending multiple events with the same action.// Clicks on tagged links are handled by another function.@@ -394,13 +401,13 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {}}- if (COMPILE_OUTBOUND_LINKS) {+ if (COMPILE_OUTBOUND_LINKS && (!COMPILE_CONFIG || config.outboundLinks)) {if (isOutboundLink(link)) {return sendLinkClickEvent(event, link, { name: 'Outbound Link: Click', props: { url: link.href } })}}- if (COMPILE_FILE_DOWNLOADS) {+ if (COMPILE_FILE_DOWNLOADS && (!COMPILE_CONFIG || config.fileDownloads)) {if (isDownloadToTrack(hrefWithoutQuery)) {return sendLinkClickEvent(event, link, { name: 'File Download', props: { url: hrefWithoutQuery } })}@@ -419,7 +426,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {if (shouldFollowLink(event, link)) {var attrs = { props: eventAttrs.props, callback: followLink }- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {attrs.revenue = eventAttrs.revenue}plausible(eventAttrs.name, attrs)@@ -427,7 +434,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {event.preventDefault()} else {var attrs = { props: eventAttrs.props }- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {attrs.revenue = eventAttrs.revenue}plausible(eventAttrs.name, attrs)@@ -437,13 +444,13 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {document.addEventListener('click', handleLinkClickEvent)document.addEventListener('auxclick', handleLinkClickEvent)- if (COMPILE_OUTBOUND_LINKS) {+ if (COMPILE_OUTBOUND_LINKS && (!COMPILE_CONFIG || config.outboundLinks)) {function isOutboundLink(link) {return link && link.href && link.host && link.host !== location.host}}- if (COMPILE_FILE_DOWNLOADS) {+ if (COMPILE_FILE_DOWNLOADS && (!COMPILE_CONFIG || config.fileDownloads)) {var defaultFileTypes = ['pdf', 'xlsx', 'docx', 'txt', 'rtf', 'csv', 'exe', 'key', 'pps', 'ppt', 'pptx', '7z', 'pkg', 'rar', 'gz', 'zip', 'avi', 'mov', 'mp4', 'mpeg', 'wmv', 'midi', 'mp3', 'wav', 'wma', 'dmg']var fileTypesAttr = scriptEl.getAttribute('file-types')var addFileTypesAttr = scriptEl.getAttribute('add-file-types')@@ -459,13 +466,13 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {}}- if (COMPILE_TAGGED_EVENTS) {+ if (COMPILE_TAGGED_EVENTS && (!COMPILE_CONFIG || config.taggedEvents)) {// Finds event attributes by iterating over the given element's (or its// parent's) classList. Returns an object with `name` and `props` keys.function getTaggedEventAttributes(htmlElement) {var taggedElement = isTagged(htmlElement) ? htmlElement : htmlElement && htmlElement.parentNodevar eventAttrs = { name: null, props: {} }- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {eventAttrs.revenue = {}}@@ -487,7 +494,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {}}- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {var revenueMatchList = className.match(/plausible-revenue-(.+)(=|--)(.+)/)if (revenueMatchList) {var key = revenueMatchList[1]@@ -518,7 +525,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {setTimeout(submitForm, 5000)var attrs = { props: eventAttrs.props, callback: submitForm }- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {attrs.revenue = eventAttrs.revenue}plausible(eventAttrs.name, attrs)@@ -560,7 +567,7 @@ if (COMPILE_OUTBOUND_LINKS || COMPILE_FILE_DOWNLOADS || COMPILE_TAGGED_EVENTS) {} else {var attrs = {}attrs.props = eventAttrs.props- if (COMPILE_REVENUE) {+ if (COMPILE_REVENUE && (!COMPILE_CONFIG || config.revenue)) {attrs.revenue = eventAttrs.revenue}plausible(eventAttrs.name, attrs)diff --git a/tracker/test/fixtures/plausible-main.html b/tracker/test/fixtures/plausible-main.htmlnew file mode 100644index 0000000000..635937942b--- /dev/null+++ b/tracker/test/fixtures/plausible-main.html@@ -0,0 +1,40 @@+<!DOCTYPE html>+<html lang="en">++<head>+ <meta charset="UTF-8">+ <meta name="viewport" content="width=device-width, initial-scale=1.0">+ <meta http-equiv="X-UA-Compatible" content="ie=edge">+ <title>Plausible Playwright tests</title>+</head>++<body>+ <a id="file-download" href="https://awesome.website.com/file.pdf">Download</a>++ <a id="outbound-link" href="https://example.com">Outbound link</a>++ <a id="manual-pageview" onclick="plausible('pageview', { u: '/:test-plausible-main' })">Manual pageview</a>++ <button id="tagged-event" class="plausible-event-name=Purchase plausible-event-foo=bar plausible-revenue-currency=EUR plausible-revenue-amount=13.32">+ Tagged event+ </button>++ <script>window.plausible = window.plausible || function() { (window.plausible.q = window.plausible.q || []).push(arguments) }</script>+ <script>+ const params = new URLSearchParams(window.location.search)++ if (params.has('before_script_loaded')) {+ const beforeScriptLoaded = params.get('before_script_loaded')+ eval(beforeScriptLoaded)+ }++ // Trick to load the script with the passed config+ const config = params.get('script_config')+ const script = document.createElement('script')+ script.src = `/tracker/js/plausible-main.js?script_config=${encodeURIComponent(config)}`+ script.setAttribute('event-some-prop', '456')++ document.body.appendChild(script)+ </script>+</body>+</html>diff --git a/tracker/test/plausible-main.spec.js b/tracker/test/plausible-main.spec.jsnew file mode 100644index 0000000000..b3aa3d47fc--- /dev/null+++ b/tracker/test/plausible-main.spec.js@@ -0,0 +1,175 @@+/*+Tests for plausible-main.js script variant++Unlike in production, we're manually interpolating the script config in this file to+better test the script in isolation of the plausible codebase.+*/++import {+ expectPlausibleInAction,+ hideAndShowCurrentTab,+ metaKey,+ mockRequest,+ e as expect+} from './support/test-utils'+import { test } from '@playwright/test'+import { LOCAL_SERVER_ADDR } from './support/server'++const DEFAULT_CONFIG = {+ domain: 'example.com',+ endpoint: `${LOCAL_SERVER_ADDR}/api/event`,+ local: true+}++async function openPage(page, config, options = {}) {+ const configJson = JSON.stringify({ ...DEFAULT_CONFIG, ...config })+ let path = `/plausible-main.html?script_config=${configJson}`+ if (options.beforeScriptLoaded) {+ path += `&before_script_loaded=${options.beforeScriptLoaded}`+ }+ await page.goto(path)+ await page.waitForFunction('window.plausible !== undefined')+}++test.describe('plausible-main.js', () => {+ test.beforeEach(({ page }) => {+ // Mock file download requests+ mockRequest(page, 'https://awesome.website.com/file.pdf')+ })++ test('triggers pageview and engagement automatically', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, {}),+ expectedRequests: [{ n: 'pageview', d: 'example.com', u: expect.stringContaining('plausible-main.html')}]+ })++ await expectPlausibleInAction(page, {+ action: () => hideAndShowCurrentTab(page, {delay: 2000}),+ expectedRequests: [{n: 'engagement', d: 'example.com', u: expect.stringContaining('plausible-main.html')}],+ })+ })++ test('does not trigger any events when `local` config is disabled', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, { local: false }),+ expectedRequests: [],+ refutedRequests: [{ n: 'pageview' }]+ })+ })++ test('supports overriding the endpoint with a custom proxy endpoint', async ({ page }) => {+ await expectPlausibleInAction(page, {+ pathToMock: 'http://proxy.io/endpoint',+ action: () => openPage(page, { endpoint: 'http://proxy.io/endpoint' }),+ expectedRequests: [{ n: 'pageview', d: 'example.com', u: expect.stringContaining('plausible-main.html')}]+ })+ })++ test('does not track pageview props, outbound links, file downloads or tagged events without features being enabled', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: async () => {+ await openPage(page, {})+ await page.click('#file-download', { modifiers: [metaKey()] })+ await page.click('#tagged-event')+ await page.click('#outbound-link')+ },+ expectedRequests: [{ n: 'pageview', p: expect.toBeUndefined() }],+ refutedRequests: [{ n: 'File Download' }, { n: 'Purchase' }, { n: 'Outbound Link: Click' }],+ // Webkit captures engagement events differently, so we ignore them in this test+ shouldIgnoreRequest: (payload) => payload.n === 'engagement'+ })+ })++ test('tracks outbound links (when feature enabled)', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: async () => {+ await openPage(page, { outboundLinks: true })+ await page.click('#outbound-link')+ },+ expectedRequests: [+ { n: 'pageview', d: 'example.com', u: expect.stringContaining('plausible-main.html') },+ { n: 'Outbound Link: Click', d: 'example.com', u: expect.stringContaining('plausible-main.html'), p: { url: 'https://example.com/' } },+ ]+ })+ })++ test('tracks file downloads (when feature enabled)', async ({ page }) => {+ await openPage(page, { fileDownloads: true })++ await expectPlausibleInAction(page, {+ action: () => page.click('#file-download'),+ expectedRequests: [{ n: 'File Download', p: { url: 'https://awesome.website.com/file.pdf' } }],+ })+ })++ test('tracks pageview props (when feature enabled)', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, { pageviewProps: true}),+ expectedRequests: [{ n: 'pageview', p: { "some-prop": "456" } }]+ })+ })++ test('manual mode does not track pageviews', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: async () => {+ await openPage(page, { manual: true })+ await hideAndShowCurrentTab(page, { delay: 200 })+ },+ expectedRequests: [],+ refutedRequests: [{ n: 'pageview' }, { n: 'engagement' }]+ })+ })++ test('manual mode after manual pageview continues tracking', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: async () => {+ await openPage(page, { manual: true })+ await page.click('#manual-pageview')+ await hideAndShowCurrentTab(page, { delay: 200 })+ },+ expectedRequests: [+ { n: 'pageview', u: '/:test-plausible-main', d: 'example.com' },+ { n: 'engagement', u: '/:test-plausible-main', d: 'example.com' },+ ],+ })+ })++ test('does not send `h` parameter when `hash` config is disabled', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, {}),+ expectedRequests: [{ n: 'pageview', h: expect.toBeUndefined() }]+ })+ })++ test('sends `h` parameter when `hash` config is enabled', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, { hash: true }),+ expectedRequests: [{ n: 'pageview', h: 1 }]+ })+ })++ test('tracking tagged events (when feature enabled)', async ({ page }) => {+ await openPage(page, { taggedEvents: true })++ await expectPlausibleInAction(page, {+ action: () => page.click('#tagged-event'),+ expectedRequests: [{ n: 'Purchase', p: { foo: 'bar' }, $: expect.toBeUndefined() }]+ })+ })++ test('tracking tagged events with revenue (when enabled)', async ({ page }) => {+ await openPage(page, { taggedEvents: true, revenue: true })++ await expectPlausibleInAction(page, {+ action: () => page.click('#tagged-event'),+ expectedRequests: [{ n: 'Purchase', p: { foo: 'bar' }, $: { currency: 'EUR', amount: '13.32' } }]+ })+ })++ test('with queue code included, respects `plausible` calls made before the script is loaded', async ({ page }) => {+ await expectPlausibleInAction(page, {+ action: () => openPage(page, {}, { beforeScriptLoaded: 'window.plausible("custom-event", { props: { foo: "bar" }, interactive: false })' }),+ expectedRequests: [{ n: 'custom-event', p: { foo: 'bar' }, i: false }, { n: 'pageview' }]+ })+ })+})diff --git a/tracker/test/support/server.js b/tracker/test/support/server.jsindex 37a8a4da99..d07a976d21 100644--- a/tracker/test/support/server.js+++ b/tracker/test/support/server.js@@ -21,7 +21,11 @@ export function runLocalFileServer() {const name = req.params.nameconst variant = VARIANTS.find((variant) => variant.name === name)- const code = compileFile(variant, { returnCode: true })+ let code = compileFile(variant, { returnCode: true })++ if (name === 'plausible-main.js') {+ code = code.replace('"<%= @config_json %>"', req.query.script_config)+ }res.send(code)});diff --git a/tracker/test/support/test-utils.js b/tracker/test/support/test-utils.jsindex 391a6e367c..c373cfcb9a 100644--- a/tracker/test/support/test-utils.js+++ b/tracker/test/support/test-utils.js@@ -79,6 +79,7 @@ export const expectPlausibleInAction = async function (page, {action,expectedRequests = [],refutedRequests = [],+ pathToMock = '/api/event',awaitedRequestCount,expectedRequestCount,responseDelay,@@ -90,7 +91,7 @@ export const expectPlausibleInAction = async function (page, {const plausibleRequestMockList = mockManyRequests({page,- path: '/api/event',+ path: pathToMock,responseDelay,shouldIgnoreRequest,numberOfRequests: requestsToAwait,@@ -187,12 +188,24 @@ export const blurAndFocusPage = async function(page, options = {}) {await focus(page)}+// Custom assertion methods for checking plausible request bodies+export const e = {+ stringContaining: (value) => ({+ expected: value,+ __expectation__: (actual) => actual.includes(value)+ }),+ toBeUndefined: () => ({+ expected: undefined,+ __expectation__: (actual) => actual === undefined+ })+}+function includesSubset(body, subset) {return Object.keys(subset).every((key) => {- if (typeof subset[key] === 'object') {+ if (typeof subset[key] === 'object' && !subset[key].__expectation__) {return typeof body[key] === 'object' && areFlatObjectsEqual(body[key], subset[key])} else {- return body[key] === subset[key]+ return checkEqual(body[key], subset[key])}})}@@ -205,7 +218,14 @@ function areFlatObjectsEqual(obj1, obj2) {if (keys1.length !== keys2.length) return false;- return keys1.every(key => obj2[key] === obj1[key])+ return keys1.every(key => checkEqual(obj2[key], obj1[key]))+}++function checkEqual(a, b) {+ if (typeof b === 'object' && b.__expectation__) {+ return b.__expectation__(a)+ }+ return a === b}function delay(ms) {