# Targeting your audience using Attributes

Target Attributes using the dashboard or API.

Text, Number, and Date Attributes are targeted directly. You specify the Attribute, an operator, and a value to evaluate against. For JSON Attributes, you target specific properties within the JSON schema, not the object as a whole.

> **Warning:** To target users based on a unique user ID or other external identifier, do not use Custom Attributes. Instead, target [Named User](https://www.airship.com/docs/reference/glossary/#named_user).

## Targeting in the dashboard

You can include Attributes when using the **Target by conditions** and **Target specific users** audience options for messages and experiments. You can also create [Segments](https://www.airship.com/docs/reference/glossary/#segment) instead of having to recreate your audience selections. Both use the same process.

Attributes can be combined with other segmentation data, such as events and Tags. For full instructions and usage locations, see [Segments](https://www.airship.com/docs/guides/audience/segmentation/segments/). For details about configuring each Attribute type, see the next sections.

When defining your audience, add each Attribute as a condition. In the image below, the Segment includes a Text Attribute targeting users whose favorite food is lasagna. The Attribute name is "Favorite Food", the operator is `Equals`, and the value is `lasagna`.

![Creating a Segment in the dashboard](https://www.airship.com/docs/images/segment-builder_hu_4e1acc92b6780a23.webp)

*Creating a Segment in the dashboard*

### Configure Text, Number, and Date Attributes

To configure Text and Number Attributes, select an operator, select **Add a value** and enter a value. Select outside the field or hit Enter on your keyboard to save the value. Repeat for multiple values. Multiple values are evaluated as a Boolean OR.

To configure Date attributes, select an operator, and then configure a date or range. Availability and requirements depend on the operator. For the *Between* operator, the end date is not inclusive. For example, `Between July 5 - July 17` includes dates July 5 to July 16.

Use these configuration steps for Specific, Relative, and Today:

| Option | Steps |
| --- | --- |
| **Specific** | Select a Year/Month/Day. With the _Equals_ and _Does not equal_ operators you can also use the formats Day, Month, Month/Day, and Year/Month. |
| **Relative** | Specify the number of years/months/days/hours/minutes from today's date. With the _Equals_ and _Does not equal_ operators, also select Month/Day or Year/Month/Day. |
| **Today** | With the _Equals_ operator, select Month/Day or Year/Month/Day. With the _After_ and _Before_ operators, Year/Month/Day is the only option and is preselected. |

### Configure JSON Attributes

For JSON Attributes, you target one or more properties in its schema for evaluation. For multiple properties, set Boolean logic to determine whether any or all evaluations must be true for the whole condition to be true.

> **Note:** When using the "is not equal to" (!=) operator, the system treats missing information differently based on the Attribute type. For JSON Attributes, if a user doesn't have a value for a specific property, that user is skipped from evaluation. For Text, Number, or Date Attributes, if a user is missing a value for the Attribute, that absence is considered "null", which is evaluated as whether it is not equal to the specified value, which is typically true.
> 
> For example, consider a project with 100 users:
> 
> * Targeting a segment using a JSON Attribute `flight.destination` != `"London"`: Suppose 10 users have values for the property, and one of those users has the value `"London"`. The system will only evaluate the 10 users who explicitly have the property. Of the 10, one is "London", so the condition is true for nine users. The 90 users who do not have the `flight.destination` property are skipped from this evaluation. Result: nine users are included.
> 
> * Targeting a segment using a Text Attribute `singleFlightDestination` != `"London"`: All 100 users are included in the evaluation, even if their `singleFlightDestination` value is missing, because the missing value is treated as `"null"`. If one user has `singleFlightDestination` as `"London"`, then 99 users will satisfy the condition: `"null"` != `"London"` is true, and any other value not equal to `"London"` is also true. Result: 99 users are included.


After selecting a JSON Attribute:

1. Select **Configure**.
   ![Configuring a JSON Attribute in a Segment](https://www.airship.com/docs/images/segment-attribute-json_hu_62babdb749d33e10.webp)
   
   *Configuring a JSON Attribute in a Segment*
1. Under **Evaluations**, choose from the **Select a source** menu:

   | Option | Description |
   | --- | --- |
   | **\<alias\>** | [An alternate name defined for a property.](https://www.airship.com/docs/guides/audience/attributes/adding/#add-json-attributes) The Attribute's schema will appear with the aliased property's line selected. |
   | **Select from schema** | The Attribute's schema will appear. Select a property's line, then its path will appear to the right of the menu. |
1. (For string and number properties) Select an operator and enter a value.
1. (For Boolean properties) Select **Is true** or **Is false**.
1. (For date properties) Select an operator and configure a date or range. Availability and requirements depend on the operator. For the *Between* operator, the end date is not inclusive. For example, `Between July 5 - July 17` includes dates July 5 to July 16.

   Use these configuration steps for Specific, Relative, and Today:

   | Option | Steps |
   | --- | --- |
   | **Specific** | Select a Year/Month/Day. With the _Equals_ and _Does not equal_ operators you can also use the format Year/Month. |
   | **Relative** | Specify the number of years/months/days/hours/minutes from today's date. With the _Equals_ and _Does not equal_ operators, also select Month/Day or Year/Month/Day. |
   | **Today** | With the _Equals_ operator, select Month/Day or Year/Month/Day. With the _After_ and _Before_ operators, Year/Month/Day is the only option and is preselected. |
1. (Optional for arrays in the path) Select **First** or **Last** to specify the first or last item in the array. Defaults to **Any**.
1. Select the check mark to add the property.
1. Select **Add another** for more properties.
1. (For multiple properties) Select **AND** or **OR** to determine how to evaluate multiple properties within the condition:
   * AND = all criteria must be met
   * OR = any criteria must be met
1. Select **Done**.

## Targeting using the API

Use the [Attributes audience object](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/) to target any Attribute type.

For Text, Number, and Date Attributes, use the Attribute ID as the value for `attribute` and specify a comparison `operator` and Attribute `value`. `precision` is also required for some operators for Date Attributes. For more information, see:

* [Text Attributes Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#textattribute)
* [Number Attributes Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#numberattribute)
* [Date Attributes Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#dateattribute)

**Example push targeting a Text Attribute**

```http
POST /api/push HTTP/1.1
Authorization: Basic <master authorization string>
Accept: application/vnd.urbanairship+json; version=3
Content-Type: application/json

{
    "audience": {
        "attribute": "item_purchased",
        "operator": "contains",
        "value": "jeans"
    },
    "device_types": [
        "ios",
        "android",
        "web"
    ],
    "notification": {
        "alert": "Hello from Airship"
    }
}
```


**Example push targeting a Date attribute**

```http
POST /api/push HTTP/1.1
Authorization: Basic <master authorization string>
Accept: application/vnd.urbanairship+json; version=3
Content-Type: application/json

{
    "audience": {
        "attribute": "account_creation",
        "operator": "equals",
        "value": "2023-05-24",
        "precision": "year_month_day"
    },
    "device_types": [
        "web"
    ],
    "notification": {
        "alert": "Hello from Airship"
    }
}
```


Use Boolean `NOT` or `AND` operators to create compound audience expressions. In the following example, the `NOT` operator gives you the same result as if you had used the "Does not contain" operator building a Segment in the dashboard. For more about Boolean audience operators, see [Audience Selection: Compound Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/audience-selection/#compoundselector) in the Data Formats section of our API reference.

**NOT operator**

```json
{
    "NOT": {
        "attribute": "item_purchased",
        "operator": "contains",
        "value": "jeans"
    }
}
```


**AND operator**

```json
{
    "AND": [
        {
            "attribute": "size",
            "operator": "greater",
            "value": 12
        },
        {
            "attribute": "size",
            "operator": "less",
            "value": 15
        }
    ]
}
```


For JSON Attributes, the [Attributes audience object](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/) must contain the Attribute ID as the value for `attribute` and a `where` object. The `where` object defines a `property` as a JsonPath expression, the property `value`, a comparison `operator`, and the property type as a `compare_as` value. See [JSON Attributes Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#jsonattribute) for more information.

**Audience object for a JSON Attribute**

```json
{
    "audience":{
        "attribute": "books_on_books",
        "where":{
            "property": "$.x.store.book[*].title",
            "operator": "equals",
            "value": "Dracula",
            "compare_as": "text"
        }
    }
}
```


For details and more example requests, see [Attributes](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/) in the Data Formats section of our API reference.

---

As in the dashboard, you can create reusable Segments instead of having to recreate your audience selections. See [Segments](https://www.airship.com/docs/developer/rest-api/ua/operations/segments/) in our API reference.

## Operators

Operators per Attribute type:

| Attribute type | Operators | API reference |
| --- | --- | --- |
| **Text** | equals, contains, less, greater, is_empty | [Text Attribute Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#textattribute) |
| **Number** | equals, contains, less, greater, is_empty | [Number Attribute Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#numberattribute) |
| **Date** | equals, before, after, is_empty, range | [Date Attribute Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#dateattribute) |
| **JSON** | equals, contains, not_contains, less, greater, is_empty, not_empty<sup>1</sup> | [JSON Attribute Selector](https://www.airship.com/docs/developer/rest-api/ua/schemas/attributes/#jsonattribute) |

<sup>1. If the JsonPath expression maps to an array, the only available operators are is_empty, not_empty, contains, or not_contains.</sup>


The same operators are available when creating a Segment in the dashboard, but some have slightly different labels, such as "Does not contain" instead of `not_contains`.
