feat: add groups parameter to capture() for event-level group association#248
feat: add groups parameter to capture() for event-level group association#248omarbadran wants to merge 2 commits intoPostHog:mainfrom
Conversation
This adds a `groups` parameter to `Posthog().capture()` that allows associating an event with specific groups without persisting them for future events (unlike `group()`). Use case: You have "sticky" groups set at login (e.g. company, team), but also need to associate certain events with a third group (e.g. project, supplier) that shouldn't persist to other events. Implementation: - Added `groups` parameter to platform interface, IO, and web layers - iOS/Android bridges pass `groups` to the native SDK's `capture()` method, which properly merges with any sticky groups from `group()` - Web implementation embeds groups into `$groups` property (matching the JS SDK behavior) - Added tests and example app buttons for manual verification Closes #XXX
| Future<void> capture({ | ||
| required String eventName, | ||
| Map<String, Object>? properties, | ||
| Map<String, Object>? groups, |
There was a problem hiding this comment.
on Android and iOS: groups: Map<String, String>, so lets make it the same instead of String, Object, double check the rest of the PR
| ...Map<String, Object>.from( | ||
| existingGroups.map( | ||
| (key, value) => MapEntry(key.toString(), value as Object), |
There was a problem hiding this comment.
you dont need this if we make <String, String>
| return _posthog.capture( | ||
| eventName: eventName, | ||
| properties: props, | ||
| groups: groups, | ||
| ); |
There was a problem hiding this comment.
not needed since it will return below anyway
| // Convert groups to Map<String, String> for native SDK compatibility | ||
| Map<String, String>? normalizedGroups; | ||
| if (groups != null && groups.isNotEmpty) { | ||
| normalizedGroups = groups.map( | ||
| (key, value) => MapEntry(key, value.toString()), | ||
| ); | ||
| } |
There was a problem hiding this comment.
| }); | ||
|
|
||
| group('PosthogFlutterIO onFeatureFlags via setup', () { | ||
| final bool isUnsupportedPlatform = Platform.isLinux || Platform.isWindows; |
There was a problem hiding this comment.
this is not needed, the tests should run on any machine
posthog-flutter/.github/workflows/ci.yml
Line 39 in c95d00e
those tests wont run on CI, so we should mock results if needed
| expect(groups, equals({'company': 'c_123', 'project': 42})); | ||
| }); | ||
|
|
||
| test('capture passes groups separately from properties', () async { |
There was a problem hiding this comment.
this test is the same as the above, the only difference is that you pass properties, i dont follow why we need it
|
Thanks @omarbadran |
Will do today once I'm done with some tasks. |
@omarbadran any chances to wrap up this work so we can get this in? |
💡 Motivation and Context
The native iOS/Android SDKs support a
groupsparameter oncapture()for event-level group association (see group analytics docs). This is already exposed in Python, Go, Node.js, and the native mobile SDKs, but was missing from the Flutter SDK.Use case: When you have session-level groups (e.g.
company) set viagroup(), but need to associate specific events with a different group (e.g.project) that shouldn't persist to other events.💚 How did you test it?
$groupsproperty appears correctly in PostHog events📝 Checklist
Note: Docs update for posthog.com may be needed after merge (adding Flutter to the "Link events to groups" section, similar to iOS/Android examples).