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
4 changes: 3 additions & 1 deletion src/track.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { classifyAgent, isAiBot } from './bots.js'
import { classifyAgent, isAiBot, isHttpClient } from './bots.js'
import { hashId } from './hash.js'
import type { TrackVisitOptions } from './types.js'

Expand All @@ -18,7 +18,9 @@ export async function trackVisit(
const userAgent = req.headers.get('user-agent') || ''

const onlyBots = opts.onlyBots ?? false
const skipBrowsers = opts.skipBrowsers ?? false
if (onlyBots && !isAiBot(userAgent)) return
if (skipBrowsers && !isAiBot(userAgent) && !isHttpClient(userAgent)) return

let pathname = '/'
let originFromUrl = ''
Expand Down
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface TrackVisitOptions {
*/
source?: string
/**
* Event name. Defaults to `'doc_view'`.
* Event name. Defaults to `'agent_visit'`.
*/
eventName?: string
/**
Expand All @@ -27,6 +27,12 @@ export interface TrackVisitOptions {
* coding-agent traffic that uses HTTP-library UAs like axios or curl).
*/
onlyBots?: boolean
/**
* When `true`, capture AI bots and coding agents (HTTP clients like axios,
* curl, node-fetch) but skip regular browsers. Use this when client-side
* analytics already handles browser traffic. Defaults to `false`.
*/
skipBrowsers?: boolean
/**
* Extra properties merged into the captured event. Useful for tagging the
* site (`{ site: 'docs' }`) or any other dimension.
Expand Down
31 changes: 31 additions & 0 deletions test/track.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,37 @@ describe('trackVisit', () => {
expect(event.properties.is_ai_bot).toBe(false)
})

it('skipBrowsers captures AI bots', async () => {
const spy = vi.fn()
await trackVisit(
makeRequest('https://example.com/page', { 'user-agent': 'ClaudeBot/1.0' }),
{ analytics: customAnalytics(spy), skipBrowsers: true }
)
expect(spy).toHaveBeenCalledOnce()
})

it('skipBrowsers captures coding agents (HTTP clients)', async () => {
const spy = vi.fn()
await trackVisit(
makeRequest('https://example.com/page', { 'user-agent': 'axios/1.6.0' }),
{ analytics: customAnalytics(spy), skipBrowsers: true }
)
expect(spy).toHaveBeenCalledOnce()
const event = spy.mock.calls[0]![0] as CaptureEvent
expect(event.properties.coding_agent_hint).toBe(true)
})

it('skipBrowsers skips regular browsers', async () => {
const spy = vi.fn()
await trackVisit(
makeRequest('https://example.com/page', {
'user-agent': 'Mozilla/5.0 (Macintosh) Chrome/120'
}),
{ analytics: customAnalytics(spy), skipBrowsers: true }
)
expect(spy).not.toHaveBeenCalled()
})

it('honours a custom event name', async () => {
const spy = vi.fn()
await trackVisit(
Expand Down
Loading