Unique

Count distinct values for a field.

Unique returns counts of distinct values for a field. It answers questions like "how many unique visitors" or "how many countries are represented" without listing every value.

What unique returns

typescript
const countries = await vanta.unique({
  field: 'context.country',
  time: timeRange('2024-01-01T00:00:00Z', '2024-12-31T23:59:59Z'),
})

The result is a single number. Each distinct value in the range is counted once, no matter how many times it appears.

Unique users

The classic use is counting unique users. Unique over userId is the number of distinct users, which is different from the total number of events:

typescript
const activeUsers = await vanta.unique({
  field: 'userId',
  filters: filter('type', 'equals', 'user.active'),
  time: timeRange('2024-06-01T00:00:00Z', '2024-06-30T23:59:59Z'),
})

Unique values

Apply unique to any field to measure how many different values it takes, for example plans, sources, or feature flags:

typescript
const sources = await vanta.unique({
  field: 'source',
  filters: filter('type', 'equals', 'user.signup'),
  time: timeRange('2024-01-01T00:00:00Z', '2024-12-31T23:59:59Z'),
})

Distinct count semantics

Unique uses distinct counting: a user who sends five events counts once, not five times. This is why unique('userId') is often far smaller than a raw count of user events.