Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(cdp): add optional page-routes-only filter to Vercel log drain (#68798)

Lucas Ricoy8f5d20fef289
diff --git a/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.test.ts b/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.test.ts
index 1394ba8c8e83..521da73a2caf 100644
--- a/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.test.ts
+++ b/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.test.ts
@@ -730,6 +730,93 @@ describe('vercel log drain template', () => {
}
)
})
+
+ describe('page_routes_only', () => {
+ const logWithPath = (path: string) => ({
+ ...vercelLogDrain,
+ proxy: { ...vercelLogDrain.proxy, path },
+ })
+
+ it('is off by default: a sub-resource request is still captured', async () => {
+ const response = await tester.invoke(
+ {},
+ { request: createVercelRequest(logWithPath('/static/app.abc123.js')) }
+ )
+
+ expect(response.error).toBeUndefined()
+ expect(response.capturedPostHogEvents).toHaveLength(1)
+ })
+
+ it.each([
+ ['root', '/'],
+ ['extension-less route', '/pricing'],
+ ['nested extension-less route', '/docs/getting-started'],
+ ['trailing slash', '/docs/'],
+ ['html document', '/index.html'],
+ ['htm document', '/legacy.htm'],
+ ['uppercase HTML extension', '/INDEX.HTML'],
+ ['extension-less api route', '/api/users'],
+ ])('captures %s when enabled', async (_name, path) => {
+ const response = await tester.invoke(
+ { page_routes_only: true },
+ { request: createVercelRequest(logWithPath(path)) }
+ )
+
+ expect(response.error).toBeUndefined()
+ expect(response.capturedPostHogEvents).toHaveLength(1)
+ })
+
+ it.each([
+ ['script bundle', '/static/app.abc123.js'],
+ ['source map', '/static/app.abc123.js.map'],
+ ['stylesheet', '/styles/main.css'],
+ ['gatsby page-data', '/page-data/index/page-data.json'],
+ ['image', '/images/logo.svg'],
+ ['font', '/fonts/inter.woff2'],
+ ])('skips %s when enabled and acknowledges with 200', async (_name, path) => {
+ const response = await tester.invoke(
+ { page_routes_only: true },
+ { request: createVercelRequest(logWithPath(path)) }
+ )
+
+ expect(response.error).toBeUndefined()
+ expect(response.finished).toEqual(true)
+ expect(response.capturedPostHogEvents).toHaveLength(0)
+ expect(response.execResult).toMatchObject({
+ httpResponse: { status: 200, body: 'OK' },
+ })
+ })
+
+ it('selects the first page-route log from a batch even when an asset comes first', async () => {
+ const batch = [
+ { ...logWithPath('/static/app.abc123.js'), id: 'asset1' },
+ { ...logWithPath('/pricing'), id: 'page1' },
+ { ...logWithPath('/styles/main.css'), id: 'asset2' },
+ ]
+
+ const response = await tester.invoke({ page_routes_only: true }, { request: createVercelRequest(batch) })
+
+ expect(response.error).toBeUndefined()
+ expect(response.capturedPostHogEvents).toHaveLength(1)
+ expect(response.capturedPostHogEvents[0].properties.vercel_log_id).toBe('page1')
+ expect(response.capturedPostHogEvents[0].properties.$pathname).toBe('/pricing')
+ })
+
+ it('skips a batch with no page-route log and acknowledges with 200', async () => {
+ const batch = [
+ { ...logWithPath('/static/app.abc123.js'), id: 'asset1' },
+ { ...logWithPath('/styles/main.css'), id: 'asset2' },
+ ]
+
+ const response = await tester.invoke({ page_routes_only: true }, { request: createVercelRequest(batch) })
+
+ expect(response.error).toBeUndefined()
+ expect(response.capturedPostHogEvents).toHaveLength(0)
+ expect(response.execResult).toMatchObject({
+ httpResponse: { status: 200, body: 'OK' },
+ })
+ })
+ })
})
const createVercelRequest = (body: Record<string, any> | Record<string, any>[]) => {
diff --git a/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.ts b/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.ts
index 8e03361e731e..16d614be7a85 100644
--- a/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.ts
+++ b/nodejs/src/cdp/templates/_sources/vercel/vercel_log_drain.template.ts
@@ -95,7 +95,8 @@ if (empty(logs)) {
}
// Technical limitation: Hog functions can only call postHogCapture once per invocation.
-// If Vercel batches multiple logs in one request, we capture only the first one.
+// If Vercel batches multiple logs in one request, we emit only one (the first, or the
+// first page-route log when page_routes_only is enabled — see the selection below).
// Configure Vercel to send logs individually for complete coverage.
let droppedCount := length(logs) - 1
if (droppedCount > 0) {
@@ -103,7 +104,6 @@ if (droppedCount > 0) {
}
let log := logs[1]
-let proxy := log.proxy ?? {}
let limit := toInt(inputs.max_message_len ?? 262144)
@@ -155,6 +155,59 @@ fun extractPathname(url) {
return url
}
+// Extension of the last path segment, lowercased; '' when there is none.
+fun pathExtension(p) {
+ if (empty(p) or typeof(p) != 'string') {
+ return ''
+ }
+ let segments := splitByString('/', p)
+ let lastSegment := segments[length(segments)]
+ if (empty(lastSegment)) {
+ return ''
+ }
+ let parts := splitByString('.', lastSegment)
+ if (length(parts) <= 1) {
+ return ''
+ }
+ return lower(parts[length(parts)])
+}
+
+// Top-level document request: a path with no file extension (e.g. /pricing,
+// /docs/x) or an HTML document. Everything else is a sub-resource (JS, CSS,
+// image, font, JSON, source map).
+fun isPageRoute(p) {
+ let ext := pathExtension(p)
+ return ext == '' or ext == 'html' or ext == 'htm'
+}
+
+fun logPathname(l) {
+ let p := l.proxy ?? {}
+ return extractPathname(p.path ?? l.path ?? '')
+}
+
+// With page_routes_only enabled, emit the first page-route log in the batch, so a page
+// view is not lost when an asset request happens to come first in the same batch. Vercel
+// can send several logs per request but only one event can be emitted per invocation.
+if (inputs.page_routes_only) {
+ let selected := null
+ for (let _, candidate in logs) {
+ if (selected == null and isPageRoute(logPathname(candidate))) {
+ selected := candidate
+ }
+ }
+ if (selected == null) {
+ // No page-route request in this batch — ack with 200 so Vercel does not retry.
+ return {
+ 'httpResponse': {
+ 'status': 200,
+ 'body': 'OK'
+ }
+ }
+ }
+ log := selected
+}
+let proxy := log.proxy ?? {}
+
// Distinct ID: configurable strategy. Default is a fixed salted hash of (ip, host, ua) —
// one stable ID per client. The active strategy is recorded as $distinct_id_strategy
// on the event for diagnostics — it is not an analytical breakdown dimension.
@@ -351,6 +404,16 @@ return {
required: false,
default: false,
},
+ {
+ key: 'page_routes_only',
+ type: 'boolean',
+ label: 'Only capture page routes',
+ description:
+ 'When enabled, only top-level document requests are captured — paths with no file extension (e.g. /pricing, /docs/x) or ending in .html/.htm. Sub-resource requests (JS, CSS, images, fonts, JSON, source maps) are skipped and acknowledged with 200 so Vercel does not retry them. A single page view fans out into many sub-resource requests, so this keeps $http_log close to a document/pageview stream and cuts ingested volume substantially. Off by default (the full HTTP access log is captured). Note: extension-less API routes (e.g. /api/x) are also kept.',
+ secret: false,
+ required: false,
+ default: false,
+ },
{
key: 'salt_secret',
type: 'string',