fix: include group context in $feature_flag_called dedupe key#150
Merged
gustavohstrassburger merged 3 commits intoMay 27, 2026
Merged
Conversation
In `Client::captureFlagCalledIfNeeded`, the per-`distinct_id` dedupe element only included the distinct ID under the flag key. For group-scoped flags this meant that when the same user was evaluated under a different group, no new `$feature_flag_called` event was fired — causing per-group exposure undercount for experiments scoped to a group key. Append a canonical JSON of the sorted `groups` map to the dedup element (`$distinctId . canonicalGroupsRepr($groups)`) so the same `(distinct_id, flag)` fires once per distinct group context. Repeated calls under the same group context still dedupe; calls that pass the same array with keys inserted in a different order also dedupe (the array is canonicalized via `ksort` before being JSON-encoded). Mirrors the posthog-node fix in PostHog/posthog-js#3658 (which closes PostHog/posthog-js#3651). Both SDKs share the same dedupe shape, so backend evaluation needs the same change. Generated-By: PostHog Code Task-Id: d94308d9-7655-4bac-8f15-c61478b5fca1
Generated-By: PostHog Code Task-Id: d94308d9-7655-4bac-8f15-c61478b5fca1
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
lib/Client.php:984-985
`json_encode` returns `false` on encoding failure (e.g., non-UTF-8 bytes in a group value). PHP silently casts `false` to `''`, so every failing encoding produces the suffix `'_'` — all such groups collapse into the same dedup bucket and subsequent `$feature_flag_called` events are silently dropped. Adding a `JSON_THROW_ON_ERROR` flag makes the failure explicit.
```suggestion
ksort($groups);
return '_' . json_encode($groups, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
```
### Issue 2 of 2
test/FeatureFlagEvaluationsTest.php:609-655
**Prefer parameterised tests for the two dedup-count cases**
`testFeatureFlagCalledDedupesAcrossRepeatedCallsUnderSameGroup` and `testFeatureFlagCalledDedupesAcrossGroupKeyOrder` both set up a different call pattern, flush, count matching events, and assert exactly 1. Per the team's preference, these share enough structure to be combined under a single `@dataProvider` — each data set would supply the group arrays to pass on each call and the expected event count, keeping the assertion logic in one place.
Reviews (1): Last reviewed commit: "chore: add changeset" | Re-trigger Greptile |
ioannisj
approved these changes
May 26, 2026
…dedup tests under dataProvider
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 Motivation and Context
In
Client::captureFlagCalledIfNeeded(lib/Client.php), the per-distinct_iddedupe element only included the distinct ID under the flag key. For group-scoped flags this meant that when the same user was evaluated under a different group, no new$feature_flag_calledevent was fired — causing per-group exposure undercount for experiments scoped to a group key.Mirrors the posthog-node fix in PostHog/posthog-js#3658 (which closes PostHog/posthog-js#3651). Both SDKs share the same dedupe shape, so backend evaluation needs the same change.
💚 How did you test it?
Added three PHPUnit tests in
test/FeatureFlagEvaluationsTest.php:testFeatureFlagCalledFiresPerGroupContext— same user, same flag, two different group contexts → two events fired with the expected$groupspayloads.testFeatureFlagCalledDedupesAcrossRepeatedCallsUnderSameGroup— repeated access under the same group → one event.testFeatureFlagCalledDedupesAcrossGroupKeyOrder— same groups passed with different array insertion order → still one event (canonicalized viaksort+json_encode).vendor/bin/phpunitis green (311 tests pass).📝 Checklist
If releasing new changes
pnpm changesetto generate a changeset fileCreated with PostHog Code