Segments
Saved audience filters. Segments are PERSONAL to the API key owner — a team member never sees the site owner’s segments, and vice versa.
5 endpoints, all relative to https://analytics.appfor.you. Shared rules live in Conventions, Rate limits and Error codes.
List segments
/api/v2/sites/{siteId}/segmentsscope: readSegments are per-user in the data model, so this list never contains another user’s segments even for the site owner.
Path parameters
| siteId | string | The Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere. |
Query parameters
| Name | Type | Description |
|---|---|---|
| limit | integer | Rows to return. Default 50, minimum 1, maximum 500. |
| offset | integer | Rows to skip, for paging. Default 0. |
Example request
curl -H "Authorization: Bearer $ANALYTICS_API_KEY" \ "https://analytics.appfor.you/api/v2/sites/6507f1f77bcf86cd799439011/segments"
Example response
{
"segments": [
{
"segmentId": "6512cc03bcf86cd7994390c3",
"siteId": "6507f1f77bcf86cd799439011",
"name": "UK mobile",
"filters": { "country": "GB", "device": "mobile" },
"createdAt": "2025-02-14T08:00:00.000Z",
"updatedAt": "2025-02-14T08:00:00.000Z"
}
],
"total": 1,
"limit": 50,
"offset": 0
}Create a segment
/api/v2/sites/{siteId}/segmentsscope: writeAll filter keys are optional strings and unknown keys are stripped. Supplied filters are ANDed together. There is no viewer restriction here — segments are personal, matching the dashboard route which only checks site access.
Path parameters
| siteId | string | The Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere. |
Request body
| Field | Type | Required | Description |
|---|---|---|---|
| name | string | Yes | Segment display name. |
| filters | object | Yes | Any of: dateRange, country, city, browser, os, device, referrer, utm_source, utm_medium, utm_campaign, pathname. All optional strings. |
Example request
curl -X POST "https://analytics.appfor.you/api/v2/sites/6507f1f77bcf86cd799439011/segments" \
-H "Authorization: Bearer $ANALYTICS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "name": "UK mobile", "filters": { "country": "GB", "device": "mobile" } }'Example response (201)
{
"segmentId": "6512cc03bcf86cd7994390c3",
"siteId": "6507f1f77bcf86cd799439011",
"name": "UK mobile",
"filters": { "country": "GB", "device": "mobile" },
"createdAt": "2025-03-21T09:00:00.000Z",
"updatedAt": "2025-03-21T09:00:00.000Z"
}Get a segment
/api/v2/sites/{siteId}/segments/{segmentId}scope: readScoped to the key owner’s own segments and to this site; anything else returns 404 "Segment not found".
Path parameters
| siteId | string | The Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere. |
| segmentId | string | Identifier of the segment. |
Example request
curl -H "Authorization: Bearer $ANALYTICS_API_KEY" \ "https://analytics.appfor.you/api/v2/sites/6507f1f77bcf86cd799439011/segments/6512cc03bcf86cd7994390c3"
Example response
{
"segmentId": "6512cc03bcf86cd7994390c3",
"siteId": "6507f1f77bcf86cd799439011",
"name": "UK mobile",
"filters": { "country": "GB", "device": "mobile" },
"createdAt": "2025-02-14T08:00:00.000Z",
"updatedAt": "2025-02-14T08:00:00.000Z"
}Update a segment
/api/v2/sites/{siteId}/segments/{segmentId}scope: writeAt least one field is required. `filters` REPLACES the whole filter object rather than merging — send every filter you want to keep. Only the key owner’s own segments can be updated.
Path parameters
| siteId | string | The Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere. |
| segmentId | string | Identifier of the segment. |
Request body
Every field is optional, but at least one must be supplied.
| Field | Type | Required | Description |
|---|---|---|---|
| name | string | No | New segment name. |
| filters | object | No | Full replacement filter object. |
Example request
curl -X PATCH "https://analytics.appfor.you/api/v2/sites/6507f1f77bcf86cd799439011/segments/6512cc03bcf86cd7994390c3" \
-H "Authorization: Bearer $ANALYTICS_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "filters": { "country": "GB", "device": "mobile", "browser": "Safari" } }'Example response
{
"segmentId": "6512cc03bcf86cd7994390c3",
"siteId": "6507f1f77bcf86cd799439011",
"name": "UK mobile",
"filters": { "country": "GB", "device": "mobile", "browser": "Safari" },
"updatedAt": "2025-03-21T09:30:00.000Z"
}Delete a segment
/api/v2/sites/{siteId}/segments/{segmentId}scope: adminDestructivePass `confirm=<segmentId>` as a query parameter or as a `confirm` field in the JSON body — it must equal the 24-character segment id from the URL, not the segment name. Only the key owner’s own segments are visible or deletable; an unknown id returns 404.
Irreversible. Requires the admin scope and confirm={segmentId} — see Destructive operations.
- Segments are per-user, so there is no separate viewer check here — the delete only ever matches your own segments.
- The confirmation is checked BEFORE the id is validated, so a wrong `confirm` with a non-existent id returns 400, never a 404.
Path parameters
| siteId | string | The Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere. |
| segmentId | string | Identifier of the segment. |
Query parameters
| Name | Type | Description |
|---|---|---|
| confirm | string | Destructive-operation confirmation. Must exactly equal the `segmentId` in the URL. May be sent as a `confirm` field in the JSON body instead. |
Request body
Optional — send `{ "confirm": "<segmentId>" }` instead of the query parameter if you prefer.
| Field | Type | Required | Description |
|---|---|---|---|
| confirm | string | No | Must equal the segmentId in the URL. |
Example request
curl -X DELETE "https://analytics.appfor.you/api/v2/sites/6507f1f77bcf86cd799439011/segments/6512cc03bcf86cd7994390c3?confirm=6512cc03bcf86cd7994390c3" \ -H "Authorization: Bearer $ANALYTICS_API_KEY"
Example response
{
"deleted": true,
"segmentId": "6512cc03bcf86cd7994390c3"
}