Flow Analytics Events · FAE
A Flow Analytics Event, or FAE, is a JSON record of activity that your application sends to an analytics consumer. It answers a product question such as which steps were selected or how many recorded sessions reached an end. The contract gives the sender and receiver a shared shape; your application decides which real action each record represents and when that action is confirmed.
FlowAnalyticsEvent is the PHP data object for that contract. AnalyticsEmitterInterface is the small delivery interface your application implements. Creating the object does not emit it: the engine does not produce FAE automatically, and the object does not confirm a save, anonymize data or guarantee delivery.
Decide what the event will mean
Section titled “Decide what the event will mean”Choose the event type according to an action your application actually knows happened. A graph option being available in a payload is different from your interface displaying it, and an attempted choice is different from an accepted and saved transition.
eventType |
Meaning your application can record | edgeId |
|---|---|---|
flow_start |
A confirmed start of a flow. | null or omitted in JSON. |
node_view |
A node your application considers presented to the person using it. | null or omitted. |
edge_visible |
An option your application considers displayed. | Required integer. |
edge_selected |
A selected edge whose resulting transition is confirmed under your application’s policy. | Required integer. |
flow_end |
A confirmed end of the flow. | null or omitted. |
flow_abandon |
An abandonment decision made by an explicit application policy, with the last known node. | null or omitted. |
The contract supports all six names, but it does not implement these application policies. If you emit flow_abandon, define the evidence and time window that establish it. Silence, a missing terminal event or one observed session expiration is not enough to reconstruct what a person did.
The remaining fields identify that activity:
| Field | Contract and purpose |
|---|---|
sessionId |
Nonempty string identifying the analytics session. Supply the pseudonym or identifier your analytics policy permits. |
flowId |
Required integer from the numeric graph identity, represented as flow_id in runtime events. |
nodeId |
Required integer for every type. In the transition projection below, it is the node the selected edge leaves. |
timestamp |
RFC 3339 observation time. The PHP object accepts DateTimeImmutable and preserves representable fractional seconds. |
context |
Optional JSON object in the schema; explicitly selected application facts. The example leaves it empty. |
metadata |
Optional JSON object; graphCode and label, if supplied, must be strings. Other application metadata is permitted. |
The PHP constructor takes all its arguments, including edgeId, context and metadata; toArray() includes them, with the two maps serialized as JSON objects even when empty. It validates the event type, edge requirement and declarative JSON data, and detaches the data from mutable references. Those structural checks cannot verify that an identifier is pseudonymous or that a business action occurred.
graph_code remains the canonical runtime graph identity. FAE also requires a numeric flowId, so a graph with only a code needs an explicit compatibility identity or mapping before this projection can emit it. Do not turn an arbitrary graph code into an integer by casting it. Carry the graph code in metadata.graphCode so an analytics record remains understandable alongside the definition.
Project a confirmed transition
Section titled “Project a confirmed transition”A safe place to create a transition FAE is after your application knows the requested state was saved. This applies the result boundary explained in Events and listeners: calculate with deferred runtime events, confirm persistence, then release the results and perform the application projection.
We will reuse the learning-choice graph from Anatomy of a graph. Node 10 offers guide edge 101, leading to terminal 20, and practice edge 102, leading to terminal 30 when practiceEnabled is true. For FAE’s numeric flowId, the example uses the HTTP-compatible definition reintroduced in HTTP API. It adds graph_id: 7006 to that learning graph.
The downloadable service loads the saved state as $before, then performs the choice:
$result = $engine->choose( $graph, $before, $edgeId, 'en', sessionId: $sessionId, timestamp: $timestamp, deferEvents: true, operationId: $operationId, ); faeConfirm($sessions, $sessionId, $result);faeConfirm() checks that the store returned persisted: true and the requested state before publishing the runtime batch. A failed save discards the batch and throws. A rejected choice never reaches this helper or the following FAE construction.
After confirmation, the application constructs the analytics record and calls its emitter:
$event = new FlowAnalyticsEvent( sessionId: $pseudonym, flowId: $graph->graphId, eventType: 'edge_selected', nodeId: $before->currentNodeId, edgeId: $edgeId, context: [], metadata: [ 'graphCode' => $graph->graphCode, 'eventKey' => $operationId . ':edge-selected', ], timestamp: new DateTimeImmutable('@' . $result->state->updatedAt), ); // The state is already saved. A delivery failure must not retry the choice. $emitter->emit($event);The projection uses the departure node from the stored state, the selected edge, the graph’s numeric ID and the confirmed state’s update time. It does not copy the full runtime context or the raw session ID. $pseudonym is supplied by the application, which knows the association between the runtime session and the permitted analytics identity.
This recipe also adds metadata.eventKey, derived from the application’s operation identity. That is an application convention for recognizing repeated deliveries, not a required FAE field. The readable operation labels in this example belong to one run. In your application, make each confirmed action’s key unique across sessions and executions. In a durable delivery system, retain that key with the confirmed action so a retry sends the same event identity.
Read the resulting JSON
Section titled “Read the resulting JSON”Here is the first event from the complete example’s recorded output:
{ "sessionId": "5395eaf1431030ae6bce2f5a91f073a3", "flowId": 7006, "eventType": "edge_selected", "nodeId": 10, "edgeId": 101, "context": {}, "metadata": { "graphCode": "learning-path", "eventKey": "a-guide-confirmed:edge-selected" }, "timestamp": "2023-11-14T22:13:30+00:00"}nodeId: 10 and edgeId: 101 identify the confirmed selection of the guide. The terminal node is 20, but this record’s nodeId deliberately describes where the selected edge started. A separate flow_end projection would describe the terminal node. The fixed timestamp makes the demonstration repeatable; the opaque session pseudonym is generated for that run.
The example runs three choices across two sessions:
| Choice | Runtime result | FAE produced by this projection |
|---|---|---|
| Practice for a session with practice disabled | Rejected with EK-SEC-NAV-002; saved node stays 10. |
None. |
| Guide for that session | Saved at terminal 20. |
edge_selected, edge 101. |
| Practice for a session with practice enabled | Saved at terminal 30. |
edge_selected, edge 102. |
That is the distinction that keeps analytics meaningful: the blocked attempt remains available to diagnostic logging, while this analytics stream counts confirmed selections. If you also need attempts, define a separate application event stream; do not label an unconfirmed attempt as a completed selection.
Count completed steps in the receiving application
Section titled “Count completed steps in the receiving application”The receiving application can group validated edge_selected events by flow and edge. For this recipe, it also requires metadata.eventKey and ignores another delivery of a key it has already seen:
function faeCompletedSteps(array $deliveries): array{ $seen = []; $counts = []; foreach ($deliveries as $event) { if ($event['eventType'] !== 'edge_selected') { continue; } $eventKey = $event['metadata']['eventKey'] ?? null; if (!is_string($eventKey) || $eventKey === '') { throw new InvalidArgumentException('This consumer requires metadata.eventKey.'); } $identity = $event['flowId'] . ':' . $eventKey; if (isset($seen[$identity])) { continue; } $seen[$identity] = true; $step = $event['flowId'] . ':' . $event['edgeId']; $counts[$step] = ($counts[$step] ?? 0) + 1; } ksort($counts); return $counts;}The complete example feeds this function the two emitted events plus one repeated delivery of the guide event. The result is:
| Aggregation key | Confirmed selections counted |
|---|---|
7006:101 |
1 guide step |
7006:102 |
1 practice step |
Three deliveries therefore describe two recorded selections. This small consumer keeps its seen keys only in memory. A production receiver needs persistent deduplication if the same event can arrive after a restart. Validate the FAE envelope and your additional metadata requirements at the ingestion boundary before passing data to this function.
These counts answer how many selections were recorded under the chosen event policy. They do not establish unique people, time spent reading, all attempts or a conversion percentage without an explicitly recorded denominator. The graph’s terminal result also does not prove that an external business action, such as opening a link, subsequently happened.
Own privacy and delivery separately
Section titled “Own privacy and delivery separately”Pseudonymization is part of the application projection. Use a session-to-analytics mapping with a defined lifetime and access policy; an opaque string alone does not guarantee anonymity. The complete example creates a random pseudonym per runtime session and retains the mapping for its one run. A service handling later requests needs its own way to recover the permitted analytics identity.
Select context and metadata deliberately. A label, graph code or operation key can reveal business information even when the session ID is pseudonymous. Keep credentials, raw request bodies and unrelated personal data out of the event. The DTO accepts declarative JSON; that does not make all declarative data appropriate for your analytics destination.
The emitter is also an application responsibility. The sample’s FaeJsonLinesEmitter writes to a local stream so the consumer can inspect real JSON. If delivery fails after the state is saved, the transition is still confirmed: retry delivery through your own delivery record or outbox, not by repeating the engine choice. The sample does not provide an atomic transaction between the session store and analytics output.
Complete example and data
Section titled “Complete example and data”fae-confirmed-activity.php includes confirmed saving, the application emitter, both sessions and the aggregation function. It uses the HTTP-compatible learning graph:
php fae-confirmed-activity.php http-learning-path.json ./var/analytics-sessionsThe recorded events preserve the actual JSON object shape. The FAE schema describes the shared envelope; metadata.eventKey remains an additional requirement of this particular receiving application.