From 33ccc37d4aef7b0641ecff857556dfd3dc6d3ab8 Mon Sep 17 00:00:00 2001 From: Luke Harries Date: Tue, 24 Jan 2023 17:03:27 +0000 Subject: [PATCH 1/3] fix alias --- example.py | 4 ++-- posthog/__init__.py | 19 +++++++++++++------ posthog/client.py | 8 ++++---- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/example.py b/example.py index d2a23720..b3d3a323 100644 --- a/example.py +++ b/example.py @@ -41,9 +41,9 @@ print(posthog.feature_enabled("beta-feature", "distinct_id")) -# # Alias a previous distinct id with a new one +# Add an alias_id to a distinct_id -posthog.alias("distinct_id", "new_distinct_id") +posthog.alias("distinct_id", "alias_id") posthog.capture("new_distinct_id", "event2", {"property1": "value", "property2": "value"}) posthog.capture( diff --git a/posthog/__init__.py b/posthog/__init__.py index d7aaa984..5f556689 100644 --- a/posthog/__init__.py +++ b/posthog/__init__.py @@ -199,8 +199,8 @@ def group_identify( def alias( - previous_id, # type: str, - distinct_id, # type: str, + distinct_id, # type: str + previous_id, # type: str context=None, # type: Optional[Dict] timestamp=None, # type: Optional[datetime.datetime] uuid=None, # type: Optional[str] @@ -214,18 +214,25 @@ def alias( The same concept applies for when a user logs in. An `alias` call requires - - `previous distinct id` the unique ID of the user before - - `distinct id` the current unique id + - `distinct id` the current unique id of the user (normally the id in your database) + - `alias distinct id` the alias id you want to attach to the user, such as the anonymous session id or another ID like their email + For example: ```python - posthog.alias('anonymous session id', 'distinct id') + posthog.alias('distinct id', 'anonymous session id') + ``` + + or + + ```python + posthog.alias('distinct id', 'users-email@posthog.com') ``` """ _proxy( "alias", - previous_id=previous_id, distinct_id=distinct_id, + previous_id=previous_id, context=context, timestamp=timestamp, uuid=uuid, diff --git a/posthog/client.py b/posthog/client.py index 2b2e62e0..1308cf15 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -241,7 +241,7 @@ def group_identify(self, group_type=None, group_key=None, properties=None, conte return self._enqueue(msg) - def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None, uuid=None): + def alias(self, distinct_id=None, previous_id=None, context=None, timestamp=None, uuid=None): context = context or {} require("previous_id", previous_id, ID_TYPES) @@ -249,13 +249,13 @@ def alias(self, previous_id=None, distinct_id=None, context=None, timestamp=None msg = { "properties": { - "distinct_id": previous_id, - "alias": distinct_id, + "distinct_id": distinct_id, + "alias": previous_id, }, "timestamp": timestamp, "context": context, "event": "$create_alias", - "distinct_id": previous_id, + "distinct_id": distinct_id, } return self._enqueue(msg) From 364760b83d8b195a87419331870d2a3a29353409 Mon Sep 17 00:00:00 2001 From: Luke Harries Date: Tue, 24 Jan 2023 17:11:24 +0000 Subject: [PATCH 2/3] added regression tests --- posthog/test/test_client.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/posthog/test/test_client.py b/posthog/test/test_client.py index 5da7d95e..5b8de77c 100644 --- a/posthog/test/test_client.py +++ b/posthog/test/test_client.py @@ -447,3 +447,21 @@ def raise_effect(): client.feature_flags = [{"key": "example", "is_simple_flag": False}] self.assertFalse(client.feature_enabled("example", "distinct_id")) + + def test_alias_doesnt_regress_positional_args(self): + client = self.client + success, msg = client.alias("distinct_id", "alias_id") + client.flush() + self.assertTrue(success) + self.assertFalse(self.failed) + self.assertEqual(msg["properties"]["distinct_id"], "distinct_id") + self.assertEqual(msg["properties"]["alias"], "alias_id") + + def test_alias_doesnt_regress_named_args(self): + client = self.client + success, msg = client.alias(distinct_id="distinct_id", previous_id="alias_id") + client.flush() + self.assertTrue(success) + self.assertFalse(self.failed) + self.assertEqual(msg["properties"]["distinct_id"], "distinct_id") + self.assertEqual(msg["properties"]["alias"], "alias_id") From d747bcea20c1aea14a7f6c4755381b5c87522c63 Mon Sep 17 00:00:00 2001 From: Luke Harries Date: Tue, 24 Jan 2023 17:59:54 +0000 Subject: [PATCH 3/3] reformat with black --- posthog/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/posthog/__init__.py b/posthog/__init__.py index 5f556689..574540fc 100644 --- a/posthog/__init__.py +++ b/posthog/__init__.py @@ -223,7 +223,7 @@ def alias( posthog.alias('distinct id', 'anonymous session id') ``` - or + or ```python posthog.alias('distinct id', 'users-email@posthog.com')