Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 2 additions & 3 deletions src/matomo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import type { MatomoConfig, MatomoPayload } from './types.js';
import {
formatMatomoDateTime,
getContentLength,
isUserAgentAllowed,
toSeconds
isUserAgentAllowed
} from './utils.js';

export function buildMatomoPayload(
Expand Down Expand Up @@ -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)) {
Expand Down
2 changes: 0 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tests/matomo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
Expand All @@ -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');
});

Expand Down