From 05932b3f1302806324d7ea5b13f3fc0e263484c6 Mon Sep 17 00:00:00 2001 From: Sibin M S Date: Sat, 4 Jan 2025 02:30:35 +0530 Subject: [PATCH] [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 --- CHANGELOG.md | 4 ++++ posthog/client.py | 8 ++++++- posthog/test/test_client.py | 48 +++++++++++++++++++++++++++++++++++++ posthog/version.py | 2 +- 4 files changed, 60 insertions(+), 2 deletions(-) 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/client.py b/posthog/client.py index b12764c..164c169 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 6bdb738..9f0dab5 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 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