Skip to content

fix: include group context in $feature_flag_called dedupe key#153

Merged
gustavohstrassburger merged 4 commits into
mainfrom
posthog-code/fix-flag-called-dedupe-groups
May 27, 2026
Merged

fix: include group context in $feature_flag_called dedupe key#153
gustavohstrassburger merged 4 commits into
mainfrom
posthog-code/fix-flag-called-dedupe-groups

Conversation

@gustavohstrassburger
Copy link
Copy Markdown
Contributor

@gustavohstrassburger gustavohstrassburger commented May 25, 2026

💡 Motivation and Context

In _capture_feature_flag_called_if_needed (lib/posthog/client.rb), the per-distinct_id dedupe key only included the flag key and response. 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.

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 rspec examples covering the new behavior in spec/posthog/client_spec.rb:

  • $feature_flag_called fires per group context (group-scoped dedup) — same user, same flag, two different group contexts → two events.
  • $feature_flag_called dedupes across repeated calls under the same group context — repeated access under the same group → one event.
  • $feature_flag_called dedupes when same groups are passed in different key order — same groups passed with keys in a different order → one event (canonicalized via groups.sort.to_json).

All 78 existing client tests pass, and all 35 feature_flag_evaluations_spec tests pass.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

Created with PostHog Code

In `_capture_feature_flag_called_if_needed`, the per-`distinct_id` dedupe key only included the flag key and response. 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.

Include the sorted `groups` hash in the dedupe key so that the same `(distinct_id, flag, response)` combination fires once per distinct group context. Repeated calls under the same group context still dedupe, and calls that pass the same map in a different key order still dedupe (the groups are canonicalized via `groups.sort.to_json` before being mixed into the dedup 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.

Generated-By: PostHog Code
Task-Id: d94308d9-7655-4bac-8f15-c61478b5fca1
Generated-By: PostHog Code
Task-Id: d94308d9-7655-4bac-8f15-c61478b5fca1
@gustavohstrassburger gustavohstrassburger marked this pull request as ready for review May 25, 2026 21:03
@gustavohstrassburger gustavohstrassburger requested a review from a team as a code owner May 25, 2026 21:03
@greptile-apps
Copy link
Copy Markdown

greptile-apps Bot commented May 25, 2026

Prompt To Fix All With AI
Fix the following 1 code review issue. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 1
spec/posthog/client_spec.rb:619-682
**Duplicated test setup across all three new examples**

The `api_feature_flag_res` hash, `stub_request`, and `Client.new` lines are copy-pasted verbatim in each of the three new `it` blocks. Extracting them into `let`/`before` inside a shared `context` would satisfy the OnceAndOnlyOnce rule and align with the team preference for parameterised tests. The deduplication pair (same-context → one event, different-context → two events) are also natural candidates for a parameterised example to remove repetition between tests 2 and 3.

Reviews (1): Last reviewed commit: "chore: add changeset" | Re-trigger Greptile

Comment thread spec/posthog/client_spec.rb Outdated
Copy link
Copy Markdown

@ioannisj ioannisj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One bot comment but not a blocker I believe

@gustavohstrassburger gustavohstrassburger merged commit 669a361 into main May 27, 2026
14 checks passed
@gustavohstrassburger gustavohstrassburger deleted the posthog-code/fix-flag-called-dedupe-groups branch May 27, 2026 18:56
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.

$feature_flag_called dedupe does not include group context for group-scoped flags

2 participants