MainSDK
Search⌘K
Integrations

Segment

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

0 interfaces0 driversWritten guide
Updated 2026-10-28

Segment is a customer data platform that collects events once and forwards them to the destinations you connect. Behind 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.

Interfaces it serves

Pick Segment in main.yaml for any of these. Your calls stay the same, and you can swap later.

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

main.yaml
analytics:
  driver: segment
  # [to be decided] write key as secret:SEGMENT_WRITE_KEY

Use it

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.

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

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

Was this page helpful?

↑↓ moveEnter openEsc close

Not decided yet