Skip to content

Commit a181ba7

Browse files
authored
Groups: Feature flags support (#45)
* Fix a documentation typo * Feature flags & groups support * Update examples
1 parent b996f3a commit a181ba7

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

example.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
posthog.capture("distinct_id", "event", {"property1": "value", "property2": "value"})
1818

1919
print(posthog.feature_enabled("beta-feature", "distinct_id"))
20+
print(posthog.feature_enabled("beta-feature", "distinct_id", groups={"company": "id:5"}))
2021

2122
print("sleeping")
2223
time.sleep(5)
@@ -28,10 +29,16 @@
2829
posthog.alias("distinct_id", "new_distinct_id")
2930

3031
posthog.capture("new_distinct_id", "event2", {"property1": "value", "property2": "value"})
32+
posthog.capture(
33+
"new_distinct_id", "event-with-groups", {"property1": "value", "property2": "value"}, groups={"company": "id:5"}
34+
)
3135

3236
# # Add properties to the person
3337
posthog.identify("new_distinct_id", {"email": "[email protected]"})
3438

39+
# Add properties to a group
40+
posthog.group_identify("company", "id:5", {"employees": 11})
41+
3542
# properties set only once to the person
3643
posthog.set_once("new_distinct_id", {"self_serve_signup": True})
3744

posthog/__init__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def group_identify(
172172
"""
173173
Set properties on a group
174174
175-
A `set_once` call requires
175+
A `group_identify` call requires
176176
- `group_type` type of your group
177177
- `group_key` unique identifier of the group
178178
- `properties` with a dict with any key: value pairs
@@ -233,6 +233,7 @@ def feature_enabled(
233233
key, # type: str,
234234
distinct_id, # type: str,
235235
default=False, # type: bool
236+
groups={}, # type: dict
236237
):
237238
# type: (...) -> bool
238239
"""
@@ -242,11 +243,13 @@ def feature_enabled(
242243
```python
243244
if posthog.feature_enabled('beta feature', 'distinct id'):
244245
# do something
246+
if posthog.feature_enabled('groups feature', 'distinct id', groups={"organization": "id:5"}):
247+
# do something
245248
```
246249
247250
You can call `posthog.load_feature_flags()` before to make sure you're not doing unexpected requests.
248251
"""
249-
return _proxy("feature_enabled", key=key, distinct_id=distinct_id, default=default)
252+
return _proxy("feature_enabled", key=key, distinct_id=distinct_id, default=default, groups=groups)
250253

251254

252255
def page(*args, **kwargs):

posthog/client.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,10 @@ def load_feature_flags(self):
348348
self.poller = Poller(interval=timedelta(seconds=self.poll_interval), execute=self._load_feature_flags)
349349
self.poller.start()
350350

351-
def feature_enabled(self, key, distinct_id, default=False):
351+
def feature_enabled(self, key, distinct_id, default=False, *, groups={}):
352352
require("key", key, string_types)
353353
require("distinct_id", distinct_id, ID_TYPES)
354+
require("groups", groups, dict)
354355

355356
if not self.personal_api_key:
356357
self.log.warning("[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.")
@@ -375,6 +376,7 @@ def feature_enabled(self, key, distinct_id, default=False):
375376
request_data = {
376377
"distinct_id": distinct_id,
377378
"personal_api_key": self.personal_api_key,
379+
"groups": groups,
378380
}
379381
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
380382
response = key in resp_data["featureFlags"]

0 commit comments

Comments
 (0)