# Customer segments in Limitr Cloud

> Group customers by plan, type, metadata, activity, or usage with rules or a Stof condition, see how many match as you write it, filter every page by a segment, and use segments in notifications and contracts.

# Segments

A segment is a group of customers by a condition: Pro customers in Europe, orgs added this week, anyone past 80% of
their limit. Segments belong to the workspace, so your whole team shares them, and they're checked against the
customers in the current mode.

Use a segment to:

- **Filter the app.** Pick it in the top bar, and Customers, dashboard panels about customers, and what the Analyst
  looks at show only its customers. **Show customers** on the Segments page does the same.
- **Scope a [notification](./notifications)**, so an alert fires only for those customers.
- **Give them a rule** (Scale and Enterprise): a rule's **Who has it** on the Policy page's Rules tab names the
  segment, and every customer it matches has the rule, as they come and go. Ex. a [coupon](./coupons).
- **Give a group a contract's terms** (Scale and Enterprise), Ex. a startup program: a [contract](./contracts) with the
  segment as its audience reaches every customer it matches, as they come and go.

Segments start on [Pay as you go](./plans-and-billing). Making and changing them needs the **Change customers**
permission.

## Making a segment

**New segment**, then describe who's in it, with rules or in Stof. As you edit, the editor shows how many customers
match and a few of their IDs.

### Rules

Combine rules with **Match every rule** or **Match any rule**:

| Rule | Ex. |
|---|---|
| Plan is / is not | Plan is `pro` |
| Customer type is / is not | Type is `org` |
| Metadata | `region` == `eu`; `beta` is set; `company` contains `Inc` |
| Added in the last N days | New this week |
| Active in the last N days / Not active in N days | Active this month; quiet for 30 days |
| Near a limit | `api` at 80% or more |
| Usage this period | `chat` at least 1,000,000 |

**The Stof it makes** shows the condition the rules become.

### Stof

For anything the rules can't say, write the condition in [Stof](/docs/how-to/write-stof-policies): the body of a
function that returns true for the customers to include. **Start from a template** offers a few: added this week,
active this month, inactive, past a soft limit, and near a hard limit. The condition sees:

- `customer`: the customer, with `id`, `label`, `type`, `plan`, `meters`, and `cloud_created` and `cloud_updated`
  (in milliseconds).
- `customer_id`: its ID.
- `data`: its metadata.
- `policy`: the mode's published policy, with the customer and the customers its refs lead to loaded, to ask
  about its plan, limits, and usage.

A customer that uses its refs' plan (Ex. a user on its org's plan) has no plan of its own, so ask the policy for the
plan it's on: `policy.plan_for(customer_id, false)?.name` (the **Plan** rule does). The same goes for limits and
usage that come through refs: `policy.limit`, `policy.value`.

```stof
// EU customers on Pro who are past 80% of their chat limit
const max = policy.limit(customer_id, 'chat', false);
const used = policy.value(customer_id, 'chat', false, false);
const plan = policy.plan_for(customer_id, false)?.name ?? '';
plan == 'pro' && data.region == 'eu' && max != null && max > 0 && used != null && used >= max * 0.8
```

To make a segment of an [account](./customers#accounts) (a customer and everyone whose refs lead to it), walk the
customer's first refs to the top. It stays current as customers join or leave:

```stof
// Acme's account: Acme and everyone whose refs lead to it, at any depth
let c = customer;
let depth = 0;
while (c.id != 'acme' && c.refs.any() && depth < 16) {
    const next = policy.customer(c.refs.front());
    if (next == null) break;
    c = next;
    depth += 1;
}
c.id == 'acme'
```

The condition has to parse before it saves. The Analyst can write one and propose it for you to apply: "make a segment of Pro customers
who haven't been active in two weeks".

## Changing and deleting

Changing a segment's condition takes effect everywhere it's used right away, including notifications in your
connected engines, and who a contract with it reaches. A segment a notification, a published rule, or a contract uses can't be
deleted until the notification changes, the rule no longer names it, or the contract ends.
