# Audiences

An audience is a set of Telegram users owned by the account. Its members are never shown, listed or exported; only its counts are.

## Sources

The source is chosen when an audience is created and never changes:

- `file`: a .txt or .csv file with one Telegram user id per line. Replacing the file rebuilds the audience from the new file alone.
- `campaigns`: the users who clicked the account's ads, or only those who converted, across all its campaigns including future ones, or across picked campaigns. A rolling window of 30, 90, 180 days moves with time and is refreshed every hour; fixed dates are collected once the window has ended. Changing the settings rebuilds the audience.
- `server`: the advertiser's server adds users. Users are only ever added, never removed.

The number of audiences, the users per audience and per account, the file size and how often an audience can be rebuilt are limited.

## Uploading a file

1. Initiate the upload and PUT the file to the URL it returns before the URL expires.
2. Read the upload's status until it is `ready` or `failed`. A ready upload shows its number of ids; a failed one shows why. Every line that is not an id is invalid, a CSV header included. A file with no ids, with too many invalid lines, or with more ids than the account can still add fails.
3. Create the audience, or replace a file audience's file, with the ready upload. An upload is used once; one left unused expires.

## Server updates

The server sends `POST https://ad.api.imem.me/audience/users?aud=<audience label>` with the advertiser's API key in the `x-ads-api-key` header, the same key as in https://ad.api.imem.me/docs/postback.md | docs_postback, and up to 4096 ids per request. Requests come from servers only; a request from a browser is refused.

Sending an id again is harmless. Added users are counted within 15 minutes. A refusal is a plain-text message saying why. When requests come too fast, or too many updates are still being processed, retry after a minute.

## Status and counts

An audience is `processing` until its first build, then `ready`, or `failed` with the reason. A rebuild that fails keeps the previous contents.

- **People**: distinct users added to the audience.
- **Too few**: too few people for a campaign to show its ads only to this audience.

## In campaigns

A campaign with manual targeting can use audiences; switching it to automatic targeting removes them. Each audience is used in one of two ways:

- `exclude`: its users never see the campaign's ads.
- `include`: the ads are shown only to users in at least one such audience.

A user in both is excluded. Changing a campaign's audiences does not send it to review.

Each audience in a campaign is `active` or `inactive`. It is `inactive` while the audience is `processing` or `failed`, and when a show-only-to audience is too few. An `inactive` show-only-to audience matches nobody, so a campaign whose show-only-to audiences are all `inactive` shows no ads; while an `exclude` audience is `inactive`, the campaign shows no ads at all. A `failed` audience cannot be added to a campaign.

Users who started seeing ads recently can take a while to be matched by a large audience. An audience used by a campaign that is not deleted cannot be deleted.
