Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update alias docs #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not clear to me what alias_id restrictions are.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we are removing the alias restrictions with the new updates of persons on events enabling person merging?


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(
Expand Down
19 changes: 13 additions & 6 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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', '[email protected]')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks like a bad example. The [email protected] doesn't look like an anonymous session ID to me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

People use aliasing is several ways, not just for anonymous session ids. E.g. if you want to be able to identify users by their email as well as their distinct ID. But I guess we don't need this example in the library doc and instead can just have it in the posthog.com docs

```
"""
_proxy(
"alias",
previous_id=previous_id,
distinct_id=distinct_id,
previous_id=previous_id,
context=context,
timestamp=timestamp,
uuid=uuid,
Expand Down
8 changes: 4 additions & 4 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,21 +241,21 @@ 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)
require("distinct_id", distinct_id, ID_TYPES)

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,
Comment on lines -252 to +258

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we swapping the order here?

Copy link
Contributor Author

@lharries lharries Jan 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In our docs we tell people to use the ordering posthog.alias(distinct_id, alias). However, the parameter names are the wrong way round for this so I've updated the parameter names. Even in the current version we're actually sending it the right way (but in a fairly hacky way where we send the distinct_id as the alias because the user actually gives the alias in place of the distinct_id). I've put in a regression test to confirm that it doesn't change user functionality (apart from named args, as the named args actually refer to the opposite thing)

}

return self._enqueue(msg)
Expand Down
18 changes: 18 additions & 0 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")