DocsGuides
Sessions and outcomes
View as MarkdownA session is one run of a flow for one person. Every decision in it shares the outcomes you report, by session or by customer, within the flow's attribution window.
Flows, subjects and sessions
- A flow is one place in your product where Zigbongo chooses for a customer, with one goal:
checkout,paywall,onboarding-quiz. Its key is 1 to 128 characters of letters, digits,-,_and.. A flow exists the first time your code asks about it. - A subject is your id for the person: any non-empty string except
.and.., or an integer, which the SDKs send as its text. Keep it stable. It decides whether the person is in the holdout, and it's how you report outcomes and erase a customer later. An id is personal data whatever it looks like, so prefer your internal user id to an e-mail address. - A session is one run of a flow for one subject, such as one visit to checkout or one onboarding. It starts with its first decision.
- A decision is one choice at a decision point (a node) in the session: which plan to lead with, which upsell, which question next.
Decisions in a session
The SDK's session helper keeps the session id between calls and attaches the previous decision's id to the result you report:
import { Zigbongo } from "@zigbongo/sdk";
const zb = new Zigbongo({ apiKey: process.env.ZIGBONGO_KEY, baseUrl: "https://zigbongo.com" });
const s = zb.session("checkout", user.id);
const plan = await s.decide("plan", ["monthly", "annual"], "monthly", { inputs: { country: "LT" } });
// show plan.option, then at the next decision point:
const upsell = await s.decide("upsell", ["none", "insurance"], "none", { result: { answer: "annual" } });
- The first call starts the session. Each later call in it adds a decision.
inputsadd up over the session; a later value replaces an earlier one. See Context inputs.resultsays what happened at the previous decision, such as the answer given. Reporting the same result again changes nothing; a different result for the same decision is refused with409 decision_already_reported. Results are stored with the decision, but they aren't outcomes: only outcomes are learned from.- To put a list in order, ask at the same decision point again in the session, with the items still left as the options. Zigbongo decides the first pick at the node and each later position on its own, so it learns which item works best in each position.
When each step is its own request, as in PHP, keep the session's ids between requests and carry on from them:
use Zigbongo\Zigbongo;
$zb = new Zigbongo(apiKey: getenv('ZIGBONGO_KEY'), baseUrl: 'https://zigbongo.com');
$s = $zb->session('checkout', $user->id,
sessionId: $_SESSION['zb_session'] ?? null, lastDecisionId: $_SESSION['zb_last'] ?? null);
$upsell = $s->decide('upsell', ['none', 'insurance'], 'none', result: ['answer' => $_POST['plan']]);
$_SESSION['zb_session'] = $s->sessionId;
$_SESSION['zb_last'] = $s->lastDecisionId;
Every decide call without a session id starts a new session. If you call zb.decide() directly rather than through zb.session(), pass the sessionId the first call returned, or each step becomes a session of its own.
Outcomes
An outcome is a number you report when something happens: revenue, margin, a renewal (1), a cancellation (1, with lower set as better). Report it by session or by subject, never both.
// By session: you still have the session at hand.
await s.outcome(order.total, order.id);
// By subject: from a payment webhook or a renewal job, minutes or weeks later.
await zb.outcome({
outcomeId: invoice.id,
flowKey: "checkout",
subjectId: user.id,
value: invoice.total,
at: invoice.paidAt, // a Date, or an ISO 8601 string with its time zone
});
$zb->outcome($invoice->id, 'checkout', $invoice->total, subjectId: $user->id, at: $invoice->paidAt);
| Report by | Use it when | It counts for |
|---|---|---|
Session (sessionId) |
The outcome belongs to that run, such as the purchase at the end of a checkout | That session |
Subject (subjectId) |
You know the person but not the session, such as a renewal webhook | Each of the subject's sessions in that flow whose window covers the outcome's time |
How a flow turns outcomes into a session's value:
- Adding up. A session's outcomes are summed by default. The flow's
objective.aggregatecan instead take the largest (max) or the latest by time (last). - Pending, then zero. A session with no outcome by the end of its window counts as 0. Until then it's pending, not zero.
- Large values don't swamp the rest. Zigbongo learns from a session's value ranked among the flow's last 2,000 positive values, so one very large order doesn't outweigh many ordinary ones. Reports show the values as you sent them. For learning, a session whose value is zero or below counts as no outcome.
- Direction. Higher is better unless you set
objective.directiontominimize("Better outcome is: Lower" on the flow page), for churn or cancellations.
Idempotency by outcomeId
outcomeId is your id for the event, such as the order or invoice id. Reporting the same id again changes nothing, even with a different value, so retries are always safe. The repeat usually answers duplicate, but may answer accepted when it reaches another of Zigbongo's servers or comes more than 10 minutes later; either way it counts once. The SDKs rely on this when they retry (Retries and rate limits).
- An
outcomeIdis unique across every flow of an environment. If one order matters to two flows, give each report its own id, such asorder_1001:checkoutandorder_1001:upsell. - To correct a value, report the difference under a new id: a refund as a negative value, timed within the window, cancels a purchase in a flow that sums its outcomes.
The attribution window
The window is how long after a session starts its outcomes still count. It's 1 day (P1D) unless you change it.
- An outcome counts for a session when its time (
at) falls between 60 seconds before the session started and the end of its window. - While the window is open, decision points without inputs learn from what's known so far: an outcome counts as soon as it arrives, and the final value replaces it when the window closes. Decision points that use inputs learn when the window closes.
- Set it on the flow page's settings (1 minute to 90 days), with
objective.windowonPATCH /v1/flows/{flowKey}, or in a registered flow's definition. The API takes an ISO 8601 duration in weeks, days, hours, minutes and seconds (PT30M,P7D,P30D); years and months aren't accepted. - A new window applies to the sessions already open, not only to new ones. With a shorter one, those whose new window has passed close within seconds, and outcomes after their new end stop counting; with a longer one, they stay open longer. A session whose window had closed stays closed.
curl -X PATCH https://zigbongo.com/v1/flows/checkout \
-H "Authorization: Bearer $ZIGBONGO_KEY" \
-H "Content-Type: application/json" \
-d '{"objective": {"window": "P30D"}}'
Choose a window longer than the flow takes and long enough for the outcome to arrive. A short window learns sooner but misses later outcomes: a 5-minute window on a 12-minute quiz counts the purchase at its end as nothing. For churn, think in weeks, such as 30 days (P30D).
Outcomes reported late
- Send
at. Without it, an outcome is timed when Zigbongo receives it. An outcome reported a week late withoutatfalls outside a 1-day window.atcan be up to 400 days back, and up to a day ahead for clocks that differ; beyond that the outcome is refused with400 invalid_input. - Report before the window closes. Once a session's window has closed, Zigbongo stops learning from it: an outcome that arrives afterwards isn't learned from, even if its
atfalls inside the window. The lift report fixes each session's numbers about an hour after its window closes. - An outcome by subject needs a session to land in. One timed before the person's session, or for a subject with no session in that flow, is accepted and stored but counts for nothing.
- Outcomes aren't checked as they arrive. One for a flow or session Zigbongo doesn't know, such as one with a typo in its flow key, is accepted and stored too, and counts for nothing.
- Records don't last for ever. Sessions, decisions and outcomes are deleted 180 days after their last activity. An outcome for such a session counts for nothing.
When outcomes stop arriving
If far fewer sessions than usual end with an outcome, as when a purchase webhook starts failing, Zigbongo puts the flow's learning on hold: the sessions that end meanwhile aren't learned from, so the missing outcomes don't count against the options people saw, and Zigbongo keeps choosing from what it had learned. The flow's page says so, and the organization's owners get an e-mail for a live flow. Learning resumes on its own once outcomes arrive again. If your outcome really became rarer for good, such as a change from sign-ups to purchases, learning resumes once Zigbongo's idea of the usual share has caught up, within a few weeks; a new flow starts afresh at once.
Batches
To send many outcomes at once, such as a nightly import, use outcomes():
const results = await zb.outcomes([
{ outcomeId: "inv_1001", flowKey: "checkout", subjectId: "u_81723", value: 49, at: "2026-10-10T14:03:00Z" },
{ outcomeId: "inv_1002", flowKey: "checkout", sessionId: savedSessionId, value: 19 },
]);
for (const r of results) {
if (r.error) console.warn(r.outcomeId, r.error.code);
}
- Each item succeeds or fails on its own:
{ outcomeId, status }, wherestatusisacceptedorduplicate, or{ outcomeId, error: { code, message } }. One invalid item doesn't stop the others. - A request body is at most 2 MiB, about 25,000 outcomes. A larger one is refused with
413 body_too_large. - Every outcome in a batch counts against your outcomes rate limit, so send big imports in batches at your limit's pace (Retries and rate limits).
- The SDK retries a batch like a single outcome. An item an earlier attempt stored reads
duplicate, or may readacceptedwhen the retry reaches another of Zigbongo's servers; either way it counts once.
What counts as a billed session
A session is one run of a flow for one person, however many decision points it has. A five-step quiz is one session, not five.
- A session counts when Zigbongo starts it: at a
decidecall without a session id, or a registered flow'sstart(). A call that falls back before it reaches Zigbongo starts nothing. - Sessions in the holdout and in a paused flow count too.
- A session counts on the day (UTC) Zigbongo receives it. A call sent twice, as a retry can be, starts one session.
- Outcomes, fallback reports and the other calls aren't sessions.
- Only live environments are billed. Test environments are free.
- The first 10,000 live sessions each month are free, then each 1,000 costs €4. Prices exclude VAT. After a failed payment, days on which Zigbongo didn't choose for you at all aren't billed (Pausing flows).
See Pricing for the rest.
For AI assistants and agents: every docs page is plain Markdown at its address with .md (this one is /docs/sessions-and-outcomes.md), and llms.txt lists them all.