Documentation menu

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

GET/api/v2/sites/{siteId}/segmentsscope: read

Segments are per-user in the data model, so this list never contains another user’s segments even for the site owner.

Path parameters

siteIdstringThe 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

NameTypeDescription
limitintegerRows to return. Default 50, minimum 1, maximum 500.
offsetintegerRows 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

POST/api/v2/sites/{siteId}/segmentsscope: write

All 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

siteIdstringThe 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

FieldTypeRequiredDescription
namestringYesSegment display name.
filtersobjectYesAny 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

GET/api/v2/sites/{siteId}/segments/{segmentId}scope: read

Scoped to the key owner’s own segments and to this site; anything else returns 404 "Segment not found".

Path parameters

siteIdstringThe Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere.
segmentIdstringIdentifier 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

PATCH/api/v2/sites/{siteId}/segments/{segmentId}scope: write

At 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

siteIdstringThe Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere.
segmentIdstringIdentifier of the segment.

Request body

Every field is optional, but at least one must be supplied.

FieldTypeRequiredDescription
namestringNoNew segment name.
filtersobjectNoFull 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

DELETE/api/v2/sites/{siteId}/segments/{segmentId}scope: adminDestructive

Pass `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

siteIdstringThe Mongo _id returned as `siteId` by GET /api/v2/sites, or the public `trackingId` embedded in the tracking snippet. Both are accepted everywhere.
segmentIdstringIdentifier of the segment.

Query parameters

NameTypeDescription
confirmstringDestructive-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.

FieldTypeRequiredDescription
confirmstringNoMust 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"
}