# Segmentation for the Web SDK

You can define your audience based on tags, named users, tag groups, and attributes.
## Addressing Devices

To help target specific devices or users for a notification, we have Tags,
Named Users, Tag Groups, and Attributes.

### Tags {#tags-about}

Tags are an easy way to group channels. Many tags can be applied to one or
more devices. Then, a message sent to a tag will be sent out to all devices that
are associated with that tag.

**Modifying tags**


```js
const sdk = await UA

await sdk.channel.editTags()
  // Add a tag
  .add(group, tag)
  // Remove a tag
  .remove(group, tag)
  // Replaces all tags for the given tag group with `tags`
  .set(group, tags)
  // applies all previously queued mutations
  .apply()
```


Tags are applied asynchronously and will be retried if the application fails
due to a network failure.

### Named Users

Named Users allow you to associate multiple devices to a single
user or profile that may be associated with more than one device, e.g., an
end-user's Chrome browser on their laptop and their iPhone. A device can have
only one Named User, and a single Named User should not be associated with more than 100 devices. Named Users provide the ability to directly set tags on them. See [Tag Groups](#tag-groups).

[Client-side Named User association](https://www.airship.com/docs/guides/audience/named-users/#client-side-named-user-association) is enabled by default.

> **Note:** * Associating the channel with a Named User ID will implicitly disassociate the
> channel from the previously associated Named User ID, if it existed.
> 
> * A Named User that has not been identified yet is referred to as an "anonymous
> contact." You may set tags and attributes on a contact even if it has not yet
> been identified.


**Setting the Named User**


```js
const sdk = await UA

// Set the Named User to a String
await sdk.contact.identify(name) // associates this channel with a Named User

// Remove the currently associated Named User
await sdk.contact.reset() // Disassociates the channel from the Named User
```


### Tag Groups

<p>A Tag Group is an array of tags that you can associate with both channels and Named Users. In addition to the Airship default tag groups, you can create custom tag groups in the dashboard. See the <a href="https://www.airship.com/docs/guides/audience/tags/#tag-groups">Tag Groups</a> documentation for more details, including security information.</p>

**Contact Tag Group Examples**


```js
const sdk = await UA

await sdk.contact.editTags()
  // Add channel tag to a group
  .add('group-name', 'tag-name')
  // …or add multiple at a time
  .add('other-group-name', ['tag-name-1', 'tag-name-2'])
  // Remove channel tag from a group
  .remove('group-name', 'tag-name')
  // Set channel tags on a group named `loyalty`
  .set('loyalty', ['silver-member', 'gold-member'])
  // apply the above mutations
  .apply()
```


**Channel Tag Group Examples**


```js
const sdk = await UA

await sdk.channel.editTags()
  // Add channel tag to a group
  .add('group-name', 'tag-name')
  // …or add multiple at a time
  .add('other-group-name', ['tag-name-1', 'tag-name-2'])
  // Remove channel tag from a group
  .remove('group-name', 'tag-name')
  // Set channel tags on a group named `loyalty`
  .set('loyalty', ['silver-member', 'gold-member'])
  // apply the above mutations
  .apply()
```


### Attributes

Attributes are metadata used for audience segmentation and personalization. They extend the concept of Tags by adding comparison operators and values to determine whether or not to target a user, helping you better evaluate your audience.

**Contact Attributes Example**


```js
const sdk = await UA

await sdk.contact.editAttributes()
  // set attribute `firstName`
  .set('firstName', 'John')
  // set attribute `lastName`
  .set('lastName', 'Doe')
  // apply the above mutations
  .apply()
```


**Channel Attributes Example**


```js
const sdk = await UA

await sdk.channel.editAttributes()
  // set attribute `firstName`
  .set('firstName', 'John')
  // set attribute `lastName`
  .set('lastName', 'Doe')
  // apply the above mutations
  .apply()
```


### JSON Attributes
JSON Attributes are data objects containing one or more string, number, date, or boolean key-value pairs. The pairs can be added individually or within objects or arrays.

**Contact JSON Attributes Example**


```js
const contact = await sdk.contact
const editor = await contact.editAttributes()
const nowInSeconds = Math.floor(Date.now() / 1000)

await editor
  .set("attribute_name#instance_id", {
     key: "value", 
     another_key: "another_value", 
     exp: nowInSeconds 
    }
  )
  .apply()
```


**Channel JSON Attributes Example**


```js
const channel = await sdk.channel
const editor = await channel.editAttributes()
const nowInSeconds = Math.floor(Date.now() / 1000)

await editor
  .set("attribute_name#instance_id", {
     key: "value", 
     another_key: "another_value", 
     exp: nowInSeconds 
    }
  )
  .apply()
```


### Subscription Lists

A *Subscription list* is an audience list of users who are opted in to messaging about a specific topic. Users can manage their opt-in status per list using a Preference Center.

**Contact Subscription List Example**


```js
const sdk = await UA

await sdk.contact.subscriptions.edit()
  // unsubscribe the contact's `web` channels from the `product_updates` list
  .unsubscribe("product_updates", "web")
  // subscribe the contact's `app` channels to the `product_updates` list
  .subscribe("product_updates", "app")
  // apply the above mutations
  .apply()
```

