Skip to content

Commit 0bf9db0

Browse files
committed
add functionality+tests for set and set_once
1 parent e830836 commit 0bf9db0

File tree

5 files changed

+127
-10
lines changed

5 files changed

+127
-10
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,3 @@ Specifically, the [Python integration](https://posthog.com/docs/integrations/pyt
1414
2. Run `source env/bin/activate` (activates the virtual environment)
1515
3. Run `python3 -m pip install -e ".[test]"` (installs the package in develop mode, along with test dependencies)
1616
4. Run `make test`
17-
18-
To run tests specific to a file, use

example.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import posthog
77

88
# You can find this key on the /setup page in PostHog
9-
posthog.api_key = ""
9+
posthog.api_key = "LXP6nQXvo-2TCqGVrWvPah8uJIyVykoMmhnEkEBi5PA"
1010
posthog.personal_api_key = ""
1111

1212
# Where you host PostHog, with no trailing /.
@@ -19,19 +19,29 @@
1919
print(posthog.feature_enabled("beta-feature", "distinct_id"))
2020

2121
print("sleeping")
22-
time.sleep(45)
22+
time.sleep(5)
2323

2424
print(posthog.feature_enabled("beta-feature", "distinct_id"))
2525

2626
# # Alias a previous distinct id with a new one
27+
2728
posthog.alias("distinct_id", "new_distinct_id")
2829

30+
posthog.capture("new_distinct_id", "event2", {"property1": "value", "property2": "value"})
31+
2932
# # Add properties to the person
30-
posthog.identify("distinct_id", {"email": "[email protected]"})
33+
posthog.identify("new_distinct_id", {"email": "[email protected]"})
34+
35+
# properties set only once to the person
36+
posthog.set_once("new_distinct_id", {"self_serve_signup": True})
3137

38+
time.sleep(3)
3239

33-
# properties only once to the person
34-
posthog.set_once("distinct_id", {"self_serve_signup": True})
3540
posthog.set_once(
36-
"distinct_id", {"self_serve_signup": False}
41+
"new_distinct_id", {"self_serve_signup": False}
3742
) # this will not change the property (because it was already set)
43+
44+
posthog.set("new_distinct_id", {"current_browser": "Chrome"})
45+
posthog.set("new_distinct_id", {"current_browser": "Firefox"})
46+
47+
posthog.shutdown()

posthog/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,38 @@ def identify(
8787
)
8888

8989

90+
def set(
91+
distinct_id, # type: str,
92+
properties=None, # type: Optional[Dict]
93+
context=None, # type: Optional[Dict]
94+
timestamp=None, # type: Optional[datetime.datetime]
95+
message_id=None, # type: Optional[str]
96+
):
97+
# type: (...) -> None
98+
"""
99+
Set properties on a user record.
100+
This will overwrite previous people property values, just like `identify`.
101+
102+
A `set` call requires
103+
- `distinct id` which uniquely identifies your user
104+
- `properties` with a dict with any key: value pairs
105+
106+
For example:
107+
```python
108+
posthog.set('distinct id', {
109+
'current_browser': 'Chrome',
110+
})
111+
```
112+
"""
113+
_proxy(
114+
"set",
115+
distinct_id=distinct_id,
116+
properties=properties,
117+
context=context,
118+
timestamp=timestamp,
119+
message_id=message_id,
120+
)
121+
90122
def set_once(
91123
distinct_id, # type: str,
92124
properties=None, # type: Optional[Dict]

posthog/client.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def capture(self, distinct_id=None, event=None, properties=None, context=None, t
138138

139139
return self._enqueue(msg)
140140

141-
def set_once(self, distinct_id=None, properties=None, context=None, timestamp=None, message_id=None):
141+
def set(self, distinct_id=None, properties=None, context=None, timestamp=None, message_id=None):
142142
properties = properties or {}
143143
context = context or {}
144144
require("distinct_id", distinct_id, ID_TYPES)
@@ -149,6 +149,23 @@ def set_once(self, distinct_id=None, properties=None, context=None, timestamp=No
149149
"context": context,
150150
"distinct_id": distinct_id,
151151
"$set": properties,
152+
"event": "$set",
153+
"messageId": message_id,
154+
}
155+
156+
return self._enqueue(msg)
157+
158+
def set_once(self, distinct_id=None, properties=None, context=None, timestamp=None, message_id=None):
159+
properties = properties or {}
160+
context = context or {}
161+
require("distinct_id", distinct_id, ID_TYPES)
162+
require("properties", properties, dict)
163+
164+
msg = {
165+
"timestamp": timestamp,
166+
"context": context,
167+
"distinct_id": distinct_id,
168+
"$set_once": properties,
152169
"event": "$set_once",
153170
"messageId": message_id,
154171
}

posthog/test/test_client.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,66 @@ def test_advanced_identify(self):
105105
self.assertEqual(msg["messageId"], "messageId")
106106
self.assertEqual(msg["distinct_id"], "distinct_id")
107107

108+
109+
def test_basic_set(self):
110+
client = self.client
111+
success, msg = client.set("distinct_id", {"trait": "value"})
112+
client.flush()
113+
self.assertTrue(success)
114+
self.assertFalse(self.failed)
115+
116+
self.assertEqual(msg["$set"]["trait"], "value")
117+
self.assertTrue(isinstance(msg["timestamp"], str))
118+
self.assertTrue(isinstance(msg["messageId"], str))
119+
self.assertEqual(msg["distinct_id"], "distinct_id")
120+
121+
def test_advanced_set(self):
122+
client = self.client
123+
success, msg = client.set(
124+
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "messageId"
125+
)
126+
127+
self.assertTrue(success)
128+
129+
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
130+
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
131+
self.assertEqual(msg["$set"]["trait"], "value")
132+
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
133+
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
134+
self.assertTrue(isinstance(msg["timestamp"], str))
135+
self.assertEqual(msg["messageId"], "messageId")
136+
self.assertEqual(msg["distinct_id"], "distinct_id")
137+
138+
139+
def test_basic_set_once(self):
140+
client = self.client
141+
success, msg = client.set_once("distinct_id", {"trait": "value"})
142+
client.flush()
143+
self.assertTrue(success)
144+
self.assertFalse(self.failed)
145+
146+
self.assertEqual(msg["$set_once"]["trait"], "value")
147+
self.assertTrue(isinstance(msg["timestamp"], str))
148+
self.assertTrue(isinstance(msg["messageId"], str))
149+
self.assertEqual(msg["distinct_id"], "distinct_id")
150+
151+
def test_advanced_set_once(self):
152+
client = self.client
153+
success, msg = client.set_once(
154+
"distinct_id", {"trait": "value"}, {"ip": "192.168.0.1"}, datetime(2014, 9, 3), "messageId"
155+
)
156+
157+
self.assertTrue(success)
158+
159+
self.assertEqual(msg["timestamp"], "2014-09-03T00:00:00+00:00")
160+
self.assertEqual(msg["context"]["ip"], "192.168.0.1")
161+
self.assertEqual(msg["$set_once"]["trait"], "value")
162+
self.assertEqual(msg["properties"]["$lib"], "posthog-python")
163+
self.assertEqual(msg["properties"]["$lib_version"], VERSION)
164+
self.assertTrue(isinstance(msg["timestamp"], str))
165+
self.assertEqual(msg["messageId"], "messageId")
166+
self.assertEqual(msg["distinct_id"], "distinct_id")
167+
108168
def test_basic_alias(self):
109169
client = self.client
110170
success, msg = client.alias("previousId", "distinct_id")
@@ -125,7 +185,7 @@ def test_basic_page(self):
125185

126186
def test_basic_page_distinct_uuid(self):
127187
client = self.client
128-
distinct_id = uuid4()
188+
distinct_id = str(uuid4())
129189
success, msg = client.page(distinct_id, url="https://posthog.com/contact")
130190
self.assertFalse(self.failed)
131191
client.flush()

0 commit comments

Comments
 (0)