From 8aa1dc815a2e615f8bf515cc953c0ebc613268f1 Mon Sep 17 00:00:00 2001 From: Marc Neudert Date: Tue, 3 Feb 2026 10:47:11 +0100 Subject: [PATCH] Send pf_srv as milliseconds instead of fractional seconds --- README.md | 2 +- src/matomo.ts | 5 ++--- src/utils.ts | 2 -- tests/matomo.test.ts | 4 ++-- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index cc8c5ed..2095336 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ The Worker simply calls `fetch(request)` to reach your origin and separately pos ## Runtime Behavior - Receives each incoming request, proxies to origin with `fetch`, and returns the origin response. If configuration is invalid, logs an error and just proxies (no tracking). -- Measures server time (`pf_srv` in seconds), status, and response bytes from `Content-Length` when present. +- Measures server time (`pf_srv` in milliseconds), status, and response bytes from `Content-Length` when present. - Builds a Matomo payload with `idsite`, `rec:1`, `recMode:1`, `url`, `source:'Cloudflare'`, `cdt` (UTC `YYYY-MM-DD HH:mm:ss`), and `ua`. - Skips tracking when `URL_EXCLUDE_REGEX` matches; detects downloads via `DOCUMENT_REGEX`; disallowed UAs are skipped by `USER_AGENT_ALLOWLIST_REGEX`; skips tracking when request method not in `HTTP_METHOD_ALLOWLIST`. - Sends a single Matomo hit asynchronously via `waitUntil` to `/matomo.php` (standard tracking API) with timeout. diff --git a/src/matomo.ts b/src/matomo.ts index ea93f5e..b0fe3d7 100644 --- a/src/matomo.ts +++ b/src/matomo.ts @@ -2,8 +2,7 @@ import type { MatomoConfig, MatomoPayload } from './types.js'; import { formatMatomoDateTime, getContentLength, - isUserAgentAllowed, - toSeconds + isUserAgentAllowed } from './utils.js'; export function buildMatomoPayload( @@ -55,7 +54,7 @@ export function buildMatomoPayload( } if (durationMs >= 0) { - payload.pf_srv = toSeconds(durationMs); + payload.pf_srv = Math.round(durationMs); } if (config.documentRegex && config.documentRegex.test(url)) { diff --git a/src/utils.ts b/src/utils.ts index 36b282c..041056f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -18,8 +18,6 @@ export const formatMatomoDateTime = (date: Date): string => { return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`; }; -export const toSeconds = (ms: number): number => Number((ms / 1000).toFixed(3)); - export const getContentLength = (response: Response): number | undefined => { const header = response.headers.get('content-length'); if (!header) return undefined; diff --git a/tests/matomo.test.ts b/tests/matomo.test.ts index a0de7fa..3a73811 100644 --- a/tests/matomo.test.ts +++ b/tests/matomo.test.ts @@ -36,7 +36,7 @@ describe('buildMatomoPayload (Worker)', () => { ua: 'AgentX', http_status: 201, bw_bytes: 512, - pf_srv: 1.234, + pf_srv: 1234, download: request.url }); }); @@ -55,7 +55,7 @@ describe('buildMatomoPayload (Worker)', () => { expect(payload).not.toHaveProperty('bw_bytes'); expect(payload).not.toHaveProperty('download'); - expect(payload?.pf_srv).toBe(0.005); + expect(payload?.pf_srv).toBe(5); expect(payload?.cdt).toBe('2025-02-18 12:00:01'); });