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..574540fc 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) 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")