---
title: "Segment"
description: "Use Segment for MainSDK product analytics - write key, main.yaml, consent before sending, local testing and swapping."
section: "integrations"
canonical: "https://mainsdk.dev/docs/integrations/segment"
updated: "2026-10-28"
---

# Segment

> Use Segment for MainSDK product analytics - write key, main.yaml, consent before sending, local testing and swapping.

Canonical: https://mainsdk.dev/docs/integrations/segment · Updated: 2026-10-28

> **Note:** Not planned. ADR-0031 removed Segment from `analytics`, because it links people across sites. Use the built-in store, PostHog or OpenTelemetry instead, see [analytics](https://mainsdk.dev/docs/interfaces/analytics).


Segment is a customer data platform that collects events once and forwards them to the destinations you connect. Behind [`analytics`](https://mainsdk.dev/docs/interfaces/analytics), it lets your app track through MainSDK while your data team routes events onward. Pick it when Segment already feeds your warehouse and tools.

> **Note:** `analytics` has no spec yet. The options on this page are provisional and may change when the spec lands.

## Interfaces it serves

## Before you start

1. Create a source in your Segment workspace for your app and copy its write key.
2. Connect the destinations you want events forwarded to.
3. Store the write key in your secrets manager.

## Configure

```yaml title="main.yaml"
analytics:
  driver: segment
  # [TBD] write key as secret:SEGMENT_WRITE_KEY
```

## Use it

```ts title="app/checkout/actions.ts"
"use server";
import { analytics, auth } from "mainsdk";

export async function startCheckout(req: Request, plan: string) {
  const session = await auth.require(req);
  analytics.track("checkout.started", { plan }, { for: session.user });
}
```

## Local development and testing

Use a separate Segment source for development. In tests, `createTestApp()` from `@mainsdk/testing` runs `analytics` on an in-memory fake, and `chaos.on("analytics", { outage: true })` simulates Segment being down.

## What MainSDK adds on top

- Nothing is sent without consent where it is required.
- Only pseudonymised user IDs leave your app, so they reach every Segment destination pseudonymised too.

See [Product analytics](https://mainsdk.dev/docs/interfaces/analytics).

## Limits and notes

- Planned driver from the product requirements; `analytics` has no spec yet.
- Consent is decided before an event reaches Segment; destinations you connect in Segment then receive only what was allowed.

## Swap to or from it

```yaml title="main.yaml"
analytics:
  driver: segment   # was posthog
```

Your calls stay the same. Past events stay in the old tool.

## FAQ

**Can Segment forward to PostHog or Google Analytics?** Segment routes to its destinations as usual. Or send to those tools directly with their own MainSDK drivers.

## Next

- [Product analytics](https://mainsdk.dev/docs/interfaces/analytics)
- [PostHog](https://mainsdk.dev/docs/integrations/posthog) and [Google Analytics](https://mainsdk.dev/docs/integrations/google-analytics)
