Skip to content

Powered by Grav CMS + Helios Theme

Newsletter segments (add-on)

Saved conditions that pick people out of a list, the vocabulary they are written in, and their counts.

A segment is a condition document saved under a name. Read the vocabulary first: it names every field a condition may use, with its type and the operators it accepts. See Segments.

List segments

GET /api/v1/kahunacart/newsletters/segments Permission kahunacart.newsletters.view

Every saved segment, with the count of people it is about, when that count was taken, and how many campaigns are pointed at it. The count is cached and refreshed by the worker every fifteen minutes or so, so read counted_at beside it before quoting the number — and call count_segment when the answer has to be true right now. A segment with a list_id is about the people subscribed to that list who also match its conditions; a segment with none is about everybody the store may mail. Start here when somebody asks who a campaign would reach.

Get one segment

GET /api/v1/kahunacart/newsletters/segments/{id} Permission kahunacart.newsletters.view

One segment with its full condition document, its cached count and the list it is narrowed to. The document has three containers — all, any and none — and each holds conditions of the form {field, op, value}. Read get_segment_vocabulary to find out what the fields mean before writing one.

Parameters

Name Type Description
id required integer At least 1.

What a segment can ask about

GET /api/v1/kahunacart/newsletters/segments/vocabulary Permission kahunacart.newsletters.view

Every field a segment condition may name, with its type, the operators it accepts, the group it belongs to and — for the fields that are chosen from a list — the options themselves. This is the reference for writing a condition document: a field this does not name, or an operator a field does not declare, is refused both when the segment is saved and when it runs. Some fields come from other add-ons and are only here when those add-ons are installed, so read this rather than assuming a fixed list.

Try a segment without saving it

POST /api/v1/kahunacart/newsletters/segments/preview Permission kahunacart.newsletters.view

Answer a condition document straight away: how many people match it, ten of them, and how many matched but cannot be mailed — suppressed, gone from the list, or never confirmed. Nothing is stored. This is the right tool for "how many people would that reach?", and the right thing to run before creating a segment, because a document that matches nobody is much easier to fix before a campaign is pointed at it. Pass list_id to require that people are also subscribed to that list.

Parameters

Name Type Description
list_id optional integer or null Require a subscribed membership on this list as well
definition optional object The condition document: all, any and none, each a list of {field, op, value}. Keys: `all`, `any`, `none`

Save a segment

POST /api/v1/kahunacart/newsletters/segments Permission kahunacart.newsletters.manage

Save a condition document under a name. Run preview_segment first and say what the count came to — a segment saved without anybody looking at what it matches is a campaign audience nobody has checked. The definition is validated against the vocabulary as it is saved and again every time it runs, so a field that goes away with an add-on is a refusal rather than a quietly smaller audience. The count is left empty until the worker takes it, which is within about fifteen minutes.

Parameters

Name Type Description
name required string What it is called on the screens
description optional string or null What this segment is for, for whoever reads it in a year
list_id optional integer or null Narrow it to people subscribed to this list
definition optional object The condition document: all, any and none, each a list of {field, op, value}. Keys: `all`, `any`, `none`

Change a segment

PATCH /api/v1/kahunacart/newsletters/segments/{id} Permission kahunacart.newsletters.manage

Edit a segment's name, description, list or conditions. Changing the conditions or the list clears the cached count, because the old number is the answer to a different question — the screens say "not counted yet" until the worker takes a new one. Every draft and scheduled campaign pointed at this segment will use the new definition when it starts, so check what is using it first.

Parameters

Name Type Description
id required integer At least 1.
name optional string 190 characters or fewer.
description optional string or null
list_id optional integer or null
definition optional object Keys: `all`, `any`, `none`

Delete a segment

DELETE /api/v1/kahunacart/newsletters/segments/{id} Permission kahunacart.newsletters.manage

Remove a segment. Refused, with the campaigns named, while any draft or scheduled campaign is still pointed at it — those campaigns have to be given a different audience first. A campaign that has already sent keeps its record of who received it in the sends table, so deleting the segment it used takes nothing away from the history.

Parameters

Name Type Description
id required integer At least 1.

Recount a segment now

POST /api/v1/kahunacart/newsletters/segments/{id}/count Permission kahunacart.newsletters.manage

Work out how many people a saved segment is about, right now, and store the answer with the moment it was taken. Use this when the cached count is too old to quote — before a campaign goes out, or when somebody asks how big an audience is today. It reads the whole subscriber table, so it is not something to run in a loop over every segment; the worker already does that on its own schedule.

Parameters

Name Type Description
id required integer At least 1.