Audiences
`audiences->list`, `get`, `create`, `update`, `delete`, `empty`, `growth`, `listContacts`, `addContact`, `addContacts`, `importContacts`, `removeContact` and `removeContacts`.
Every method
$everyone = null; foreach ($client->audiences->listAll() as $audience) { if ($audience['builtin'] === 'default') { $everyone = $audience; }} $list = $client->audiences->create([ 'name' => 'Product updates', 'description' => 'Customers who asked to hear about releases',]); $client->contacts->create(['email' => '[email protected]', 'name' => 'Grace Hopper']);$client->audiences->addContact($list['id'], ['email' => '[email protected]']); $bulk = $client->audiences->addContacts($list['id'], ['emails' => ['[email protected]', '[email protected]']]); $imported = $client->audiences->importContacts($list['id'], [ 'contacts' => [['email' => '[email protected]', 'name' => 'Katherine Johnson']],]); $members = $client->audiences->listAllContacts($list['id'], q: 'grace', sort: 'added-newest', limit: 200); $growth = $client->audiences->growth(audienceIds: [$list['id']], days: 30); $client->audiences->update($list['id'], ['name' => 'Release notes']);$client->audiences->removeContact($list['id'], '[email protected]');$client->audiences->removeContacts($list['id'], ['emails' => ['[email protected]']]);$client->audiences->empty($list['id']);$client->audiences->delete($list['id']); echo $everyone['contactCount'] ?? 0, ' contacts in all', PHP_EOL;echo implode(', ', $bulk['missing']), ' ', $imported['created'], ' ', count($members), ' ', $growth['totals']['added'], PHP_EOL;An audience is a named list of contacts in this workspace. Every contact is in the built-in default audience from the moment it exists, and builtin is what names that row. The rest are yours to make, fill and delete. Branch on builtin rather than on the name, which anybody can change.
A call on one audience takes its id as the first argument, and removeContact takes the address as the second. Filters and options are named arguments in camelCase (audienceIds:, offsetMinutes:), while a request body is one array whose keys keep the API’s names (emails, contacts). A response is an array keyed in the API’s camelCase, so $audience['contactCount'] reads the count.
Send to one or more audiences with $client->broadcasts->send, on the Broadcasts page. Putting a contact in an audience is a write to the audience rather than to the contact, so audiences:write is the only scope checked. importContacts is the exception. It creates contacts, so it needs contacts:write as well.
addContact takes an address that is already a contact and refuses one that is not, with 422 contact_not_found, thrown as a ValidationException. Save it with $client->contacts->create first. Adding somebody twice answers with the membership that is already there, carrying its original addedAt, so the call is safe to retry, and the client retries it after a network failure.
The default audience can be renamed and described like any other, but it cannot be deleted and it cannot be thinned. Both are refused with 409 audience_immutable, thrown as a ConflictException with isConflict() true. Delete the contact when you mean the contact to go.
Response: an audience
list returns one page of these as an OpenEmail\Result\Page, with items, hasMore and nextCursor, the default audience first and the rest newest first. A page holds 25 unless limit: asks for up to 100. listAll returns every audience in one array, and iterate returns a Generator that yields one audience at a time. get, create and update each return one audience. listContacts returns a page of contacts instead, the contacts themselves with the date each one joined rather than membership records, with listAllContacts and iterateContacts beside it.
idstring- The durable handle, `aud_` followed by 24 hex characters. Names are not unique, so this is what belongs in stored configuration.
namestring- Trimmed on write, 1 to 120 characters. Two audiences may share a name, because an audience is addressed by its id.
descriptionstring or null- Free text for whoever reads the list later. It is null when nobody wrote any, and `'description' => null` on `update` clears it.
builtinstring or null- `default` on exactly one row per workspace, the audience that holds every contact, and null on every audience somebody created. Compare it with `'default'` rather than testing it for null, so that a built-in added later is not mistaken for the default audience.
contactCountint- How many contacts are in the audience, counted at the moment of the read rather than cached. Two reads either side of a `contacts->create` disagree by one.
lastContactAtstring or null- ISO 8601 UTC, when the contact who joined most recently joined this audience. It is null while the audience is empty.
createdAtstring- ISO 8601 UTC, when the audience was made. It fixes the list order after the default one.
updatedAtstring- ISO 8601 UTC, bumped by a rename or a description change. Membership changes do not touch it.
Parameters: audiences->listContacts
limitint- How many contacts per page, a whole number from 1 to 200, defaulting to 50.
cursorstring- The `nextCursor` from the previous page, sent with the same `q:`, `source:`, `sort:` and `statuses:`. A cursor naming a contact that is not in this audience is a 400 `invalid_cursor`, thrown as an `InvalidRequestException`.
qstring- Searches the name and the address, up to 200 characters. When nothing matches exactly on the first page, close spellings are returned instead, and the pages that follow keep matching the same way.
sourcestring- `manual` for the contacts somebody saved on purpose, `auto` for the ones the app composer recorded. Leave it out for everyone in the audience.
sortstring- `last-heard-newest` (the default) and `last-heard-oldest` go by `lastSeenAt`, and contacts that have never been mailed come last in the first and first in the second. `added-newest` and `added-oldest` go by when each contact joined this audience, and `name` ignores case and sorts a contact with no name by its address.
statusesstring or array- `['subscribed']` keeps the members who have not unsubscribed and `['unsubscribed']` the ones who have. Leave it out, pass an empty array, or name both, for everyone in the audience. `OpenEmail\Constants\AudienceMemberStatuses` holds the values, and the client sends them joined with commas as the `status` query parameter.
Response: a contact in an audience
listContacts returns an OpenEmail\Result\Page of contact arrays, and listAllContacts and iterateContacts walk every page with the same named arguments. Each row is a contact in the shape contacts->list returns, whose fields are on the Contacts page, with two more. Walking every page is how to export an audience.
addedAtstring- ISO 8601 UTC, when the contact joined this audience. Taking a contact out and adding it again starts it afresh.
unsubscribedAtstring or null- ISO 8601 UTC, when the contact unsubscribed from a broadcast sent to this audience, or null while it is subscribed. An unsubscribed contact stays in the audience, and broadcasts to it skip it. Taking it out and adding it again makes it subscribed afresh.
Adding and removing in bulk
addContacts and removeContacts take an array whose emails is a list of 1 to 200 addresses, and change one audience in one request. addContacts never creates a contact. An address that is not one comes back in missing, and importContacts is the call that creates them. Both are safe to repeat, so the client retries them after a network failure, and a retry reports the same people as already done rather than failing.
Adding to the default audience answers added as 0, because every contact is in it already, and removeContacts on it is refused with 409 audience_immutable. Taking somebody out of an audience leaves them in the address book, in the default audience and in their other audiences.
audienceIdstring- The audience the call changed, on both results.
addedint- On the `addContacts` result: the new memberships this call made.
unchangedint- On the `addContacts` result: contacts that were in the audience already. Nothing was written for them.
removedint- On the `removeContacts` result: the memberships this call took away.
notInAudiencearray- On the `removeContacts` result: contacts that were not in the audience, so nothing happened to them.
missingarray- On both: the addresses that are not contacts in this workspace, lowercased and without repeats.
Importing
importContacts is the CSV import on the audience page. It takes an array whose contacts is a list of 1 to 500 arrays, each with an email and an optional name. Each well-formed address becomes a contact if it is not one yet, and every one lands in the audience. Send a longer list in several calls. It needs audiences:write and contacts:write, and a key missing either is refused with 403 insufficient_scope, where isScopeMissing() is true on the exception.
An address that is already a contact is reused and keeps its name, and a name here only fills one that was empty. A new contact is saved as manual and joins the default audience too, and an address that was deleted from the book comes back. Replaying the same rows creates nothing twice, so the client retries the call after a network failure.
audienceIdstring- The audience the rows went into.
createdint- New contacts this call saved.
addedint- New memberships in this audience, counting contacts that existed already and were not in it yet.
skippedint- Rows that were not imported because the address was malformed.
invalidarray- The malformed addresses, exactly as they were sent.
Emptying
empty($id) takes every contact out of one audience in one request and returns the audience as it now stands, with contactCount at 0, plus removed, the number of memberships taken away. The audience keeps its id, name and description, and every contact stays in the address book and in its other audiences.
It cannot be undone and nothing records who was in the list, so walk listAllContacts first if you may want it back. The default audience cannot be emptied, and the call is refused with 409 audience_immutable. The client does not retry empty after a network failure, because a second call succeeds with removed at 0. If a response was lost, read the audience with get.
Growth
growth reads how many contacts joined each audience over a window that ends now, and how many unsubscribed inside it, by day, hour or minute. It is the chart on the audiences page. It takes named arguments, needs audiences:read and returns one array.
$growth = $client->audiences->growth( audienceIds: ['aud_9f2c4b7e1a0d63d84c5f2e7b'], days: 90, grain: 'day', offsetMinutes: intdiv((int) date('Z'), 60),); echo $growth['totals']['added'], ' joins since ', $growth['since'], PHP_EOL; foreach ($growth['series'] as $series) { echo $series['name'], ': ', $series['before'], ' before the window, ', $series['total'], ' now', PHP_EOL;}An audience records when somebody joined and never when they left, so every figure counts the people still in the list today by the date they joined, and a line never falls. A contact who joined and later left is in none of the numbers.
Parameters
audienceIdsstring or array- Up to 50 audience ids, as a list or one comma-separated string, sent joined with commas. Leave it out, or pass an empty array, for every audience. An id that is not an audience in this workspace is a 404 `audience_not_found`, and more than 50 is a 422.
daysint- How far back the window reaches, 1 to 1095. It is 30 when neither `days:` nor `minutes:` is given.
minutesint- The window in minutes, 1 to 1576800, for a window shorter than a day. It wins over `days:` when both are given.
grainstring- The size of each bucket: `day` (the default), `hour` or `minute`.
offsetMinutesint- The viewer’s offset from UTC in minutes, -840 to 840, so day and hour buckets start at their local boundary. 0 by default. `intdiv((int) date('Z'), 60)` is the offset of the zone PHP is set to.
Response
sincestring- ISO 8601 UTC, the start of the first bucket.
untilstring- ISO 8601 UTC, the moment of the read.
totalsarray- `contacts` counts each person once however many lists they are in, and `memberships` adds the lists up, so a person counts once for every list read that holds them. `added` sums the joins in the window, `lists` is how many audiences were read, and `busiest` is the bucket with the most joins, or null. `subscribed` counts each person still subscribed to at least one of the audiences read, and `unsubscribed` adds up the unsubscribes inside the window.
seriesarray- One entry per audience, largest first and then by name: `id`, `name`, `builtin`, `total` members now, `subscribed` (those still subscribed), `before` (those who joined before `since`), `added` (those who joined inside the window), `unsubscribed` (those who unsubscribed inside it) and `buckets`, oldest first, each an array with `bucket`, `added` and `unsubscribed`. Here `builtin` is `true` on the default audience and `false` on the rest, not the string an audience array carries. Only buckets with a join or an unsubscribe are listed, keyed `YYYY-MM-DD`, `YYYY-MM-DDTHH` or `YYYY-MM-DDTHH:MM` in the offset’s local time.