---
title: Targeting Rules and Filters
description: >-
  Learn how targeting rules, filters, and rollout types control which variants
  your application serves.
breadcrumbs: Docs > Feature Flags > Concepts > Targeting Rules and Filters
---

# Targeting Rules and Filters

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Overview{% #overview %}

**Targeting rules** define which variant to serve to which subjects. Each rule can include a **filter**, one or more variants, and an optional percentage rollout. Rules are evaluated in order until a match is found.

## Targeting rule types{% #targeting-rule-types %}

Datadog supports different targeting rule types depending on your rollout strategy:

| Type                    | Description                                                                               |
| ----------------------- | ----------------------------------------------------------------------------------------- |
| **Feature gate**        | Roll out immediately to a percentage of subjects matching your filter (randomized or not) |
| **Progressive rollout** | Randomized rollout over a schedule with multiple steps                                    |
| **Experiment**          | Randomized allocation associated with an experiment                                       |

## Configure targeting rules{% #configure-targeting-rules %}

To configure targeting rules for a flag:

1. Navigate to **Feature Flags** and select your flag.
1. Select the environment whose rules you want to modify.
1. Click **Add Targeting Rule** (or click the targeting rule you want to modify).

{% image
   source="https://docs.dd-static.net/images/getting_started/feature_flags/ff-targeting-rules-and-rollouts.a4d47482def5e8e4ebb239333e8709d1.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/getting_started/feature_flags/ff-targeting-rules-and-rollouts.a4d47482def5e8e4ebb239333e8709d1.png?auto=format&fit=max&w=850&dpr=2 2x"
   alt="Targeting Rules and Rollouts section on a feature flag." /%}

For each targeting rule, configure the following:

- **Name your targeting rule**: Give your targeting rule a name to describe the group it targets.
- **Define a filter** (optional): If you do not define a filter, the rule matches all subjects in that environment.
- **Select variants**: Choose which variants to serve to matching subjects. Click **Split Traffic** to randomize across multiple variants (see [Traffic Splitting and Randomization](https://docs.datadoghq.com/feature_flags/concepts/traffic_splitting.md)).
- **Set the traffic exposure** (optional): Serve the variant to a percentage of matching subjects (see [Traffic Splitting and Randomization](https://docs.datadoghq.com/feature_flags/concepts/traffic_splitting.md)).

{% image
   source="https://docs.dd-static.net/images/getting_started/feature_flags/configure-targeting-rule.5feb2c768daf6a3921a357cdd8c019d0.png?auto=format&fit=max&w=850 1x, https://docs.dd-static.net/images/getting_started/feature_flags/configure-targeting-rule.5feb2c768daf6a3921a357cdd8c019d0.png?auto=format&fit=max&w=850&dpr=2 2x"
   alt="Targeting Rule editor side panel on a feature flag." /%}

After configuring your targeting rules, click **Save Changes**, then enable the flag in the environment so SDKs can evaluate targeting rules.

{% alert level="info" %}
SDKs do not evaluate targeting rules when the flag is disabled or overridden in an environment. If the flag is overridden with a fixed variant, the SDK returns that variant instead. If the flag is disabled, the SDK returns the coded default variant.
{% /alert %}

## Filters and evaluation context{% #filters-and-evaluation-context %}

Filters use attributes from your SDK's **evaluation context**. Define attributes when you set the evaluation context before evaluating flags. Attributes must be flat primitive values (strings, numbers, Booleans). Nested objects and arrays are not supported.

### Example evaluation contexts and filters{% #example-evaluation-contexts-and-filters %}

With the following evaluation context, you can build filters with different operators, such as equality, **is one of**, **is not**, or numeric comparisons:

**Evaluation context:**

{% tab title="JavaScript" %}

```javascript
await OpenFeature.setContext({
  targetingKey: 'user-123',
  user_id: 'user-123',
  user_role: 'admin',
  email: 'user@example.com',
  country: 'US',
  tier: 'premium',
  account_age_days: 120,
});
```

{% /tab %}

{% tab title="Python" %}

```python
from openfeature.evaluation_context import EvaluationContext

eval_ctx = EvaluationContext(
    targeting_key="user-123",
    attributes={
        "user_id": "user-123",
        "user_role": "admin",
        "email": "user@example.com",
        "country": "US",
        "tier": "premium",
        "account_age_days": 120,
    },
)
```

{% /tab %}

{% tab title="Go" %}

```go
evalCtx := openfeature.NewEvaluationContext(
    "user-123",
    map[string]interface{}{
        "user_id":          "user-123",
        "user_role":        "admin",
        "email":            "user@example.com",
        "country":          "US",
        "tier":             "premium",
        "account_age_days": 120,
    },
)
```

{% /tab %}

#### Example filters{% #example-filters %}

- `country` **is one of** `US`, `CA`
- `tier` **equals** `premium`
- `user_role` **is not** `guest`
- `account_age_days` **greater than** `90`

**Note**: Other SDKs follow the same pattern. See your platform's [client](https://docs.datadoghq.com/feature_flags/client.md) or [server](https://docs.datadoghq.com/feature_flags/server.md) SDK documentation for evaluation context setup.

## Rule hierarchy{% #rule-hierarchy %}

Targeting rules are evaluated **in order** from top to bottom:

1. The SDK evaluates the first rule. If the subject matches the filter (or no filter is defined), the rule may serve a variant.
1. If the subject does not match, evaluation passes through to the next rule.
1. If no rule matches, the SDK serves the **default variant** for that environment.

## Further reading{% #further-reading %}

- [Traffic Splitting and Randomization](https://docs.datadoghq.com/feature_flags/concepts/traffic_splitting.md)
- [Environments](https://docs.datadoghq.com/feature_flags/concepts/environments.md)
- [Client-Side SDKs](https://docs.datadoghq.com/feature_flags/client.md)
