Skip to content

Commit 05932b3

Browse files
authored
[FEATURE]Add distinct_id to group_identify (#155)
* [FEATURE]Add distinct_id to group_identify * [TESTS]Updated test cases for adding distinct_id to group_identify * [LINT-FIX]client.py and test_client.py * [CHORE]Verion bump and changelog update
1 parent 50c1356 commit 05932b3

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 3.7.5 - 2025-01-03
2+
3+
1. Add `distinct_id` to group_identify
4+
15
## 3.7.4 - 2024-11-25
26

37
1. Fix bug where this SDK incorrectly sent feature flag events with null values when calling `get_feature_flag_payload`.

posthog/client.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,21 +304,27 @@ def group_identify(
304304
timestamp=None,
305305
uuid=None,
306306
disable_geoip=None,
307+
distinct_id=None,
307308
):
308309
properties = properties or {}
309310
context = context or {}
310311
require("group_type", group_type, ID_TYPES)
311312
require("group_key", group_key, ID_TYPES)
312313
require("properties", properties, dict)
313314

315+
if distinct_id:
316+
require("distinct_id", distinct_id, ID_TYPES)
317+
else:
318+
distinct_id = "${}_{}".format(group_type, group_key)
319+
314320
msg = {
315321
"event": "$groupidentify",
316322
"properties": {
317323
"$group_type": group_type,
318324
"$group_key": group_key,
319325
"$group_set": properties,
320326
},
321-
"distinct_id": "${}_{}".format(group_type, group_key),
327+
"distinct_id": distinct_id,
322328
"timestamp": timestamp,
323329
"context": context,
324330
"uuid": uuid,

posthog/test/test_client.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,25 @@ def test_basic_group_identify(self):
715715
self.assertTrue(isinstance(msg["timestamp"], str))
716716
self.assertIsNone(msg.get("uuid"))
717717

718+
def test_basic_group_identify_with_distinct_id(self):
719+
success, msg = self.client.group_identify("organization", "id:5", distinct_id="distinct_id")
720+
self.assertTrue(success)
721+
self.assertEqual(msg["event"], "$groupidentify")
722+
self.assertEqual(msg["distinct_id"], "distinct_id")
723+
self.assertEqual(
724+
msg["properties"],
725+
{
726+
"$group_type": "organization",
727+
"$group_key": "id:5",
728+
"$group_set": {},
729+
"$lib": "posthog-python",
730+
"$lib_version": VERSION,
731+
"$geoip_disable": True,
732+
},
733+
)
734+
self.assertTrue(isinstance(msg["timestamp"], str))
735+
self.assertIsNone(msg.get("uuid"))
736+
718737
def test_advanced_group_identify(self):
719738
success, msg = self.client.group_identify(
720739
"organization", "id:5", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "new-uuid"
@@ -737,6 +756,35 @@ def test_advanced_group_identify(self):
737756
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
738757
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
739758

759+
def test_advanced_group_identify_with_distinct_id(self):
760+
success, msg = self.client.group_identify(
761+
"organization",
762+
"id:5",
763+
{"trait": "value"},
764+
{"ip": "192.168.0.1"},
765+
datetime(2014, 9, 3),
766+
"new-uuid",
767+
distinct_id="distinct_id",
768+
)
769+
770+
self.assertTrue(success)
771+
self.assertEqual(msg["event"], "$groupidentify")
772+
self.assertEqual(msg["distinct_id"], "distinct_id")
773+
774+
self.assertEqual(
775+
msg["properties"],
776+
{
777+
"$group_type": "organization",
778+
"$group_key": "id:5",
779+
"$group_set": {"trait": "value"},
780+
"$lib": "posthog-python",
781+
"$lib_version": VERSION,
782+
"$geoip_disable": True,
783+
},
784+
)
785+
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
786+
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
787+
740788
def test_basic_alias(self):
741789
client = self.client
742790
success, msg = client.alias("previousId", "distinct_id")

posthog/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = "3.7.4"
1+
VERSION = "3.7.5"
22

33
if __name__ == "__main__":
44
print(VERSION, end="") # noqa: T201

0 commit comments

Comments
 (0)