Creating Segments
Build audience segments using conditions and event data.
Via API
Create segments programmatically using the REST API. Define conditions with event names, traits, and time windows.
index.js
import { Segmentr } from 'segmentr';
const segmentr = new Segmentr(process.env.SEGMENTR_API_KEY);
const segment = await segmentr.segments.create({
name: 'Churn Risk',
conditions: {
event: 'payment.failed',
timeWindow: 86400, // last 24 hours
AND: {
event: 'page.viewed',
properties: { page: '/pricing' },
},
},
});
// Segment is now live. Webhooks fire on join/leave.