From 41ec8ff7c4b82027e6e61a8b233a311c65c51519 Mon Sep 17 00:00:00 2001 From: sibinms Date: Thu, 2 Jan 2025 08:39:51 +0530 Subject: [PATCH 1/4] [FEATURE]Add distinct_id to group_identify --- posthog/client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/posthog/client.py b/posthog/client.py index b12764c..2820aec 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, From ff4991498d49a7557613298676e7826075a69c34 Mon Sep 17 00:00:00 2001 From: sibinms Date: Thu, 2 Jan 2025 09:07:27 +0530 Subject: [PATCH 2/4] [TESTS]Updated test cases for adding distinct_id to group_identify --- posthog/test/test_client.py | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 6bdb738..6c62deb 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,28 @@ 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") From a8b9c16ab8b3df05e1ce8c8301d99a902274a30e Mon Sep 17 00:00:00 2001 From: sibinms Date: Thu, 2 Jan 2025 09:28:19 +0530 Subject: [PATCH 3/4] [LINT-FIX]client.py and test_client.py --- posthog/client.py | 2 +- posthog/test/test_client.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/posthog/client.py b/posthog/client.py index 2820aec..164c169 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -304,7 +304,7 @@ def group_identify( timestamp=None, uuid=None, disable_geoip=None, - distinct_id=None + distinct_id=None, ): properties = properties or {} context = context or {} diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 6c62deb..9f0dab5 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -758,12 +758,19 @@ def test_advanced_group_identify(self): 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" + "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"], { From 4090b97acfe05c9b173c8eec7b84a7a8f73d40df Mon Sep 17 00:00:00 2001 From: sibinms Date: Fri, 3 Jan 2025 08:10:27 +0530 Subject: [PATCH 4/4] [CHORE]Verion bump and changelog update --- CHANGELOG.md | 4 ++++ posthog/version.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 741010d..89521f5 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/version.py b/posthog/version.py index 14899e1..fd1751e 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