diff --git a/CHANGELOG.md b/CHANGELOG.md index 741010d3..89521f5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 3.7.5 - 2025-01-03 + +1. Add `distinct_id` to group_identify + ## 3.7.4 - 2024-11-25 1. Fix bug where this SDK incorrectly sent feature flag events with null values when calling `get_feature_flag_payload`. diff --git a/posthog/client.py b/posthog/client.py index b12764c8..164c1695 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -304,6 +304,7 @@ def group_identify( timestamp=None, uuid=None, disable_geoip=None, + distinct_id=None, ): properties = properties or {} context = context or {} @@ -311,6 +312,11 @@ def group_identify( require("group_key", group_key, ID_TYPES) require("properties", properties, dict) + if distinct_id: + require("distinct_id", distinct_id, ID_TYPES) + else: + distinct_id = "${}_{}".format(group_type, group_key) + msg = { "event": "$groupidentify", "properties": { @@ -318,7 +324,7 @@ def group_identify( "$group_key": group_key, "$group_set": properties, }, - "distinct_id": "${}_{}".format(group_type, group_key), + "distinct_id": distinct_id, "timestamp": timestamp, "context": context, "uuid": uuid, diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 6bdb7388..9f0dab55 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -715,6 +715,25 @@ def test_basic_group_identify(self): self.assertTrue(isinstance(msg["timestamp"], str)) self.assertIsNone(msg.get("uuid")) + def test_basic_group_identify_with_distinct_id(self): + success, msg = self.client.group_identify("organization", "id:5", distinct_id="distinct_id") + self.assertTrue(success) + self.assertEqual(msg["event"], "$groupidentify") + self.assertEqual(msg["distinct_id"], "distinct_id") + self.assertEqual( + msg["properties"], + { + "$group_type": "organization", + "$group_key": "id:5", + "$group_set": {}, + "$lib": "posthog-python", + "$lib_version": VERSION, + "$geoip_disable": True, + }, + ) + self.assertTrue(isinstance(msg["timestamp"], str)) + self.assertIsNone(msg.get("uuid")) + def test_advanced_group_identify(self): success, msg = self.client.group_identify( "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): self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00") self.assertEqual(msg["context"]["ip"], "192.168.0.1") + def test_advanced_group_identify_with_distinct_id(self): + success, msg = self.client.group_identify( + "organization", + "id:5", + {"trait": "value"}, + {"ip": "192.168.0.1"}, + datetime(2014, 9, 3), + "new-uuid", + distinct_id="distinct_id", + ) + + self.assertTrue(success) + self.assertEqual(msg["event"], "$groupidentify") + self.assertEqual(msg["distinct_id"], "distinct_id") + + self.assertEqual( + msg["properties"], + { + "$group_type": "organization", + "$group_key": "id:5", + "$group_set": {"trait": "value"}, + "$lib": "posthog-python", + "$lib_version": VERSION, + "$geoip_disable": True, + }, + ) + self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00") + self.assertEqual(msg["context"]["ip"], "192.168.0.1") + def test_basic_alias(self): client = self.client success, msg = client.alias("previousId", "distinct_id") diff --git a/posthog/version.py b/posthog/version.py index 14899e15..fd1751ee 100644 --- a/posthog/version.py +++ b/posthog/version.py @@ -1,4 +1,4 @@ -VERSION = "3.7.4" +VERSION = "3.7.5" if __name__ == "__main__": print(VERSION, end="") # noqa: T201