# Group shoppers into segments for marketing

Create the named group, add the shoppers who belong, and recompute a rule-based segment so it always matches its rule.

Customers · a play in 3 steps.

## The steps

1. Create the segment — membership starts empty. — [createSegment](/reference/segment/createSegment/)
2. Add the shoppers who belong — up to 48 per call. — [addSegmentMembers](/reference/pricing/addSegmentMembers/)
3. Recompute a rule-based segment from its rule — joiners join, leavers leave. — [refreshSegment](/reference/pricing/refreshSegment/)

## The calls

Every operation this use case runs, in the order it first appears, with the request and the response its reference page shows.

### createSegment

[createSegment](/reference/segment/createSegment/) — mutation · Create a customer segment for marketing — a named group of shoppers you add members to.

**Example request**

```graphql
mutation ExampleCreateSegment($input: NewSegmentInput!) {
  createSegment(input: $input) {
    id
    sysId
    type
    caption
    status
    parentId
    rootId
    createdAt
    updatedAt
    revisionNum
    revision
    segmentType
    memberCount
    excludedConsumerIds
    evaluatedAt
    refreshProblem
  }
}
```

Variables:

```json
{
  "input": {
    "caption": "Blue jeans",
    "segmentType": "static"
  }
}
```

Send it with the envelope naming the version: `"extensions": {"at": {"version": {"name":"genesis","number":0}}}`.

**Example response**

```json
{
  "data": {
    "createSegment": {
      "id": "01900000-0000-7000-8000-37386ae00000",
      "sysId": "SE-EXMP-0000-000F",
      "type": "Segment",
      "caption": "Blue jeans",
      "status": "active",
      "parentId": "01900000-0000-7000-8000-065235280000",
      "rootId": "01900000-0000-7000-8000-a093dd800000",
      "createdAt": "2027-01-31T00:00:00.000Z",
      "updatedAt": "2027-01-31T00:00:00.000Z",
      "revisionNum": 1,
      "revision": "01900000-0000-7000-8000-b7960e180000",
      "segmentType": "static",
      "memberCount": 1,
      "excludedConsumerIds": [
        "01900000-0000-7000-8000-2fe46bb30000"
      ],
      "evaluatedAt": "2027-01-31T00:00:00.000Z",
      "refreshProblem": "<refresh problem>"
    }
  },
  "extensions": {
    "at": {
      "callId": "01EXAMPLE-CALL-ID",
      "version": {
        "requested": {
          "name": "genesis",
          "number": 0
        },
        "serviced": {
          "name": "genesis",
          "number": 0
        }
      }
    }
  }
}
```

### addSegmentMembers

[addSegmentMembers](/reference/pricing/addSegmentMembers/) — mutation · Add up to 48 shoppers to a segment in one call — already-present shoppers are skipped, not errors.

**Example request**

```graphql
mutation ExampleAddSegmentMembers($segmentId: ID!, $consumerIds: [ID!]!) {
  addSegmentMembers(segmentId: $segmentId, consumerIds: $consumerIds) {
    added
    skippedExisting
    memberCount
  }
}
```

Variables:

```json
{
  "segmentId": "01900000-0000-7000-8000-243b225b0000",
  "consumerIds": [
    "01900000-0000-7000-8000-a64ad04d0000"
  ]
}
```

Send it with the envelope naming the version: `"extensions": {"at": {"version": {"name":"genesis","number":0}}}`.

**Example response**

```json
{
  "data": {
    "addSegmentMembers": {
      "added": 1,
      "skippedExisting": 1,
      "memberCount": 1
    }
  },
  "extensions": {
    "at": {
      "callId": "01EXAMPLE-CALL-ID",
      "version": {
        "requested": {
          "name": "genesis",
          "number": 0
        },
        "serviced": {
          "name": "genesis",
          "number": 0
        }
      }
    }
  }
}
```

### refreshSegment

[refreshSegment](/reference/pricing/refreshSegment/) — mutation · Recompute a rule-based segment’s membership from its rule — shoppers who now match join, shoppers who no longer match leave.

**Example request**

```graphql
mutation ExampleRefreshSegment($id: ID!) {
  refreshSegment(id: $id) {
    queued
    matched
    added
    removed
  }
}
```

Variables:

```json
{
  "id": "01900000-0000-7000-8000-37386ae00000"
}
```

Send it with the envelope naming the version: `"extensions": {"at": {"version": {"name":"genesis","number":0}}}`.

**Example response**

```json
{
  "data": {
    "refreshSegment": {
      "queued": true,
      "matched": 1,
      "added": 1,
      "removed": 1
    }
  },
  "extensions": {
    "at": {
      "callId": "01EXAMPLE-CALL-ID",
      "version": {
        "requested": {
          "name": "genesis",
          "number": 0
        },
        "serviced": {
          "name": "genesis",
          "number": 0
        }
      }
    }
  }
}
```
