Segment
Use Segment for MainSDK product analytics - write key, main.yaml, consent before sending, local testing and swapping.
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
- Create a source in your Segment workspace for your app and copy its write key.
- Connect the destinations you want events forwarded to.
- Store the write key in your secrets manager.
Configure
analytics:
driver: segment
# [to be decided] write key as secret:SEGMENT_WRITE_KEYUse it
"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;
analyticshas 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
analytics:
driver: segment # was posthogYour 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?