---
agent:
  instructions: "Use this search API to find relevant content across this site with keywords or natural-language queries, instead of crawling individual pages. Results are returned in Markdown."
  endpoint: "https://segmentstream.com/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Web Tracker SDK

Send browser page views and server request events to a workspace event stream.

## What you can build

- [Collect browser page views](https://segmentstream.com/docs/sdk/web-tracker#send-a-browser-page-view)

  Track client-side navigation and flush a batch to the workspace collector.

- [Capture a server request](https://segmentstream.com/docs/sdk/web-tracker#send-a-server-request-event)

  Send request events from the server and keep the stream write key there.

- [Verify delivery](https://segmentstream.com/docs/sdk/web-tracker#check-the-event-reached-the-collector)

  Inspect the POST in browser developer tools and check the stream origin.

## Connect a site to an event stream

Install the SDK in the site that emits events. Create a stream that accepts the site’s origin and batched JSON. Copy its collector endpoint from the workspace. Use the browser entry point for page views and the server entry point for requests signed with a write key.

Terminal · site

```
npm install @segmentstream/web-tracker
```

## Send a browser page view

Replace the endpoint placeholders with your workspace UUID and stream key. Call pageView after client-side navigation and flush the batch. Repeated calls for the same URL are deduplicated. Keep the stream’s server write key in server code.

Browser · TypeScript

```
import { createBrowserTracker } from "@segmentstream/web-tracker/browser";

const tracker = createBrowserTracker({
  endpoint: "https://collect.segmentstream.com/v1/e/WORKSPACE_UUID/STREAM_KEY",
  sdk: { name: "support-site", version: "1" },
});

tracker.pageView();
await tracker.flush();
```

## Send a server request event

In a server request handler, use the document-request predicate to skip asset requests and prefetches. Pass the collector endpoint and stream write key from server configuration. Where the host offers a background-task hook, give it the promise returned by sendEvents.

Server · TypeScript

```
import { isDocumentRequest, requestAnonymousId, requestEvent, sendEvents } from "@segmentstream/web-tracker/server";

export async function trackRequest(request: Request, endpoint: string, writeKey: string) {
  if (!isDocumentRequest(request)) return;
  const event = requestEvent(request, {
    eventType: "http_request", anonymousId: requestAnonymousId(request), page: true,
    sdk: { name: "support-site", version: "1" },
    fields: { request: { method: request.method, path: new URL(request.url).pathname } },
  });
  await sendEvents([event], { endpoint, writeKey });
}
```

## Check the event reached the collector

Open the browser network panel and navigate to a new page. Inspect the batched POST to the collector for a page_view and the expected page URL. If the browser blocks the request, check that its origin exactly matches the stream declaration. Configure the tracker’s consent callback when collection depends on the visitor’s choice.

## Selected API methods and types

### Create the browser client · `createBrowserTracker`

One bounded, memory-only queue. Failed and unacknowledged events are dropped, never retried. Call \`pageView()\` after each navigation and \`flush()\` periodically and with \`hidden = true\` when the page is hidden, which queues a beacon. Options' callbacks that throw skip the view.

```
function createBrowserTracker(options: BrowserTrackerOptions): {
    pageView: () => void;
    flush: (hidden?: boolean) => Promise<number>;
    consentChanged: () => void;
    anonymousId: () => string | null;
};
```

### Build a server request event · `requestEvent`

A server event about the visitor's request, carrying its \`payload.visitor\` snapshot.

```
function requestEvent(request: Request, options: RequestEventOptions): WebEvent;
```

### Send server-side events · `sendEvents`

Best effort, never throws and never retries. Callers keep this work off the response path.

```
function sendEvents(events: WebEvent[], options: SendOptions): Promise<void>;
```

[segmentstream events](https://segmentstream.com/docs/cli/events)

[Read as Markdown ↗](https://segmentstream.com/docs/sdk/web-tracker.md)
