> ## Documentation Index
> Fetch the complete documentation index at: https://confidence.spotify.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Target with Criteria

> Learn how to use targeting criteria to filter users in segments.

export const HowToSchema = ({name, description, steps = []}) => {
  const schema = {
    "@context": "https://schema.org",
    "@type": "HowTo",
    name,
    description
  };
  if (steps.length > 0) {
    schema.step = steps.map((s, i) => ({
      "@type": "HowToStep",
      position: i + 1,
      name: typeof s === "string" ? s : s.name,
      text: typeof s === "string" ? s : s.text || s.name
    }));
  }
  return <script type="application/ld+json" dangerouslySetInnerHTML={{
    __html: JSON.stringify(schema)
  }} />;
};

<HowToSchema
  name="Target with Criteria"
  description="Learn how to use targeting criteria to filter users in segments."
  steps={[
{
  name: "Use Equality Matching",
  text: "Match users where a field equals a specific value:",
},
{
  name: "Use Set Matching",
  text: "Match users where a field equals any value from a set:",
},
{
  name: "Use Range Matching",
  text: "Match users where a field falls within a range:",
},
{
  name: "Match Against Another Segment",
  text: "Check if a user is in another segment:",
},
{
  name: "Combine Multiple Criteria",
  text: "Use AND, OR, and NOT to create complex logic:",
},
{
  name: "Use Nested Fields",
  text: "Reference nested fields with dot notation: This matches against evaluation context like:",
},
]}
/>

Add targeting criteria to your segments to filter users based on attributes from the evaluation context.

<Tip>
  See [Targeting Criteria](../../flags/segments-reference#targeting-criteria) in the reference for details on operators, value types, and how criteria work.
</Tip>

## Use Equality Matching

Match users where a field equals a specific value:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=sweden-users" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "Users from Sweden",
    "targeting": {
      "criteria": {
        "sweden": {
          "attribute": {
            "attributeName": "country",
            "eqRule": {
              "value": {"stringValue": "SE"}
            }
          }
        }
      },
      "expression": {"ref": "sweden"}
    },
    "allocation": {"proportion": {"value": "1.0"}}
  }'
```

## Use Set Matching

Match users where a field equals any value from a set:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=nordic-users" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "Nordic users",
    "targeting": {
      "criteria": {
        "nordics": {
          "attribute": {
            "attributeName": "country",
            "setRule": {
              "values": [
                {"stringValue": "SE"},
                {"stringValue": "DK"},
                {"stringValue": "NO"},
                {"stringValue": "FI"},
                {"stringValue": "IS"}
              ]
            }
          }
        }
      },
      "expression": {"ref": "nordics"}
    },
    "allocation": {"proportion": {"value": "1.0"}}
  }'
```

## Use Range Matching

Match users where a field falls within a range:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=age-40-49" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "Users aged 40-49",
    "targeting": {
      "criteria": {
        "age-range": {
          "attribute": {
            "attributeName": "age",
            "rangeRule": {
              "startInclusive": {"numberValue": 40},
              "endExclusive": {"numberValue": 50}
            }
          }
        }
      },
      "expression": {"ref": "age-range"}
    },
    "allocation": {"proportion": {"value": "1.0"}}
  }'
```

## Match Against Another Segment

Check if a user is in another segment:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=premium-sweden" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "Premium users from Sweden",
    "targeting": {
      "criteria": {
        "sweden": {
          "attribute": {
            "attributeName": "country",
            "eqRule": {"value": {"stringValue": "SE"}}
          }
        },
        "premium": {
          "segment": {"segment": "segments/premium-users"}
        }
      },
      "expression": {
        "and": {
          "operands": [
            {"ref": "sweden"},
            {"ref": "premium"}
          ]
        }
      }
    },
    "allocation": {"proportion": {"value": "0.1"}}
  }'
```

## Combine Multiple Criteria

Use AND, OR, and NOT to create complex logic:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=nordic-android" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "Nordic Android users",
    "targeting": {
      "criteria": {
        "nordics": {
          "attribute": {
            "attributeName": "country",
            "setRule": {
              "values": [
                {"stringValue": "SE"},
                {"stringValue": "DK"},
                {"stringValue": "NO"},
                {"stringValue": "FI"}
              ]
            }
          }
        },
        "ios": {
          "attribute": {
            "attributeName": "device.os",
            "eqRule": {"value": {"stringValue": "ios"}}
          }
        }
      },
      "expression": {
        "and": {
          "operands": [
            {"ref": "nordics"},
            {"not": {"ref": "ios"}}
          ]
        }
      }
    },
    "allocation": {"proportion": {"value": "1.0"}}
  }'
```

## Use Nested Fields

Reference nested fields with dot notation:

```bash theme={null}
curl -X POST "https://flags.confidence.dev/v1/segments?segmentId=iphone-users" \
  -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{
    "displayName": "iPhone users",
    "targeting": {
      "criteria": {
        "iphone": {
          "attribute": {
            "attributeName": "device.model",
            "eqRule": {"value": {"stringValue": "iPhone14"}}
          }
        }
      },
      "expression": {"ref": "iphone"}
    },
    "allocation": {"proportion": {"value": "1.0"}}
  }'
```

This matches against evaluation context like:

```json theme={null}
{
  "user_id": "123",
  "device": {
    "model": "iPhone14",
    "os": "ios"
  }
}
```

## Next Steps

After setting up targeting criteria:

1. [Coordinate segments](./coordinate-segments) to make them mutually exclusive
2. [Create flag rules](./create-flag-rule) to assign variants
3. [Resolve flags](./resolve-flags) with the appropriate evaluation context
