Skip to content

Let the client send a PreparedEvent, not only an Event (2.0) - #16

Merged
loevgaard merged 2 commits into
2.xfrom
send-prepared-payload
Sep 21, 2026
Merged

loevgaard merged 2 commits into
2.xfrom
send-prepared-payload

Conversation

@loevgaard

@loevgaard loevgaard commented Sep 14, 2026 •

Copy link
Copy Markdown
Member

Summary

First change of the 2.0 line (base: 2.x). Closes #15.

A consumer that hashes the personal data at capture time and sends later (e.g. through a queue) could neither queue the Event — User holds the raw PII until the payload is built — nor hand a finished payload back to the client, which only accepted an Event. The bundle works around that today with a PreparedEvent subclass (MetaConversionsApiBundle#49).

  • Event::prepare(): PreparedEvent — the payload (already normalized and hashed) together with the delivery information: eventName, eventId, payload, pixels, testEventCode. Scalars, arrays and Pixel objects only, so it round-trips through serialize() (tested) and the Symfony serializer without tricks.
  • ClientInterface::sendPreparedEvent(PreparedEvent) sends it; Client::sendEvent() is now a one-line delegation, so both paths produce byte-identical requests (tested).
  • PreparedEvent::withoutAccessTokens() / withAccessTokens(array $accessTokensByPixelId) — immutable; keep the access tokens out of the queue (and any failure storage behind it) and restore them by pixel id before sending. Pixels missing from the list are left as they are, and numeric pixel ids work as array keys (tested).
  • prepare() clones the pixels, so the prepared event is a true snapshot: changing a pixel on the event or on the prepared event no longer leaks into the other.
  • README section "Sending events later, e.g. through a queue" built around prepare()->withoutAccessTokens() and withAccessTokens().

Naming, relative to the proposal in #15

The issue proposed Payload / toPayload() / sendPayload(). That left Event with both getPayload() and toPayload(), and the object called "payload" also carried pixels, access tokens and the test event code, which are delivery information rather than payload. So "payload" keeps its 1.x meaning (the array from getPayload(), matching the PAYLOAD_CONTEXT_* constants), and the new object is named for what it is: the payload plus where to send it. Its array property is accordingly PreparedEvent::$payload.

Backwards-compatibility break (see UPGRADE-2.0.md)

ClientInterface gains sendPreparedEvent() — custom implementations must add it. That is the only break; getPayload() is untouched. The Roave BC job on this PR is red by design, since it compares against 2.x, which is still 1.x code.

Not in this PR (candidates for further 2.0 PRs)

Fbp::$randomNumber being public/mutable, native union types for User::$fbc/$fbp and Fb::withCreationTime(), a PayloadContext enum, and the haven'n typo in the client's log message.

Test plan

  • EventTest::it_prepares — hashed payload, pixels, test event code; eventName/eventId match the payload; the raw email never appears in the serialized prepared event
  • ClientTest::it_sends_prepared_event, it_sends_the_same_request_for_an_event_and_its_prepared_event, it_does_not_send_prepared_event_when_it_has_no_pixels
  • PreparedEventTest — serialize()/unserialize() round trip incl. a token-less pixel; withoutAccessTokens() and withAccessTokens() (immutability, numeric pixel ids, untouched and replaced tokens)
  • EventTest::it_prepares_a_snapshot — pixels are cloned in both directions
  • ClientTest::it_sends_a_prepared_event_that_was_queued_without_its_access_tokens — strip, serialize, unserialize, restore, send: byte-identical to sendEvent(), and the serialized form contains no token
  • 109 tests, 100 % line coverage; PHPStan (level max), ECS, dependency analyser green; Infection MSI 95 % / covered 95 %, no escaped mutants in the new code
  • Roave BC check run locally: exactly the one break listed above

After this is released

The bundle deletes its PreparedEvent subclass; its SendEvent message carries $event->prepare()->withoutAccessTokens(), and the handler resolves the tokens and calls sendPreparedEvent($preparedEvent->withAccessTokens($tokens)).

@loevgaard
loevgaard changed the base branch from master to 2.x September 21, 2026 07:37
@loevgaard loevgaard changed the title Let the client send a prepared Payload instead of only an Event Send a prepared Payload and rename getPayload() to toArray() (2.0) Sep 21, 2026
@loevgaard
loevgaard force-pushed the send-prepared-payload branch from 7c33bf6 to 7d7f1e1 Compare September 21, 2026 07:39
@loevgaard loevgaard changed the title Send a prepared Payload and rename getPayload() to toArray() (2.0) Let the client send a PreparedEvent, not only an Event (2.0) Sep 21, 2026
@loevgaard
loevgaard force-pushed the send-prepared-payload branch from 7d7f1e1 to 9eaf315 Compare September 21, 2026 07:52
A consumer that hashes the personal data at capture time and sends later,
e.g. through a queue, could neither queue the Event (User holds the raw
PII until the payload is built) nor hand the finished payload back to
the client, which only accepted an Event.

Event::prepare() now returns a PreparedEvent: the payload, already
normalized and hashed, together with the delivery information (event
name and id, the pixels and the test event code). It is made of scalars,
arrays and Pixel objects only, so it serializes without any tricks.
ClientInterface gains sendPreparedEvent() to send it, and
Client::sendEvent() is a one-line delegation to it.

The pixels are cloned, so the prepared event is a snapshot, and
PreparedEvent::withoutAccessTokens()/withAccessTokens() keep the access
tokens out of the queue and restore them by pixel id before sending.

"Payload" keeps its 1.x meaning, the array returned by getPayload(); the
new object is named for what it is, the payload plus where to send it.
The added interface method is the only BC break. See UPGRADE-2.0.md.

Closes #15
@loevgaard
loevgaard force-pushed the send-prepared-payload branch from 9eaf315 to fbfe481 Compare September 21, 2026 08:00
@loevgaard
loevgaard merged commit faff03e into 2.x Sep 21, 2026
27 of 28 checks passed
@loevgaard
loevgaard deleted the send-prepared-payload branch September 21, 2026 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Let the client send a prepared payload instead of only an Event

1 participant