Skip to content

Commit 3a6fd07

Browse files
liyiyneilkakkar
andauthored
Add get feature flag method (#67)
* get feature flag method * remove groups param from method * use personal api key * add groups * add capture * black reformat * black? * Update posthog/client.py Co-authored-by: Neil Kakkar <[email protected]> * add to init * formatting Co-authored-by: Neil Kakkar <[email protected]>
1 parent de0ccd2 commit 3a6fd07

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

posthog/__init__.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,20 @@ def feature_enabled(
254254
return _proxy("feature_enabled", key=key, distinct_id=distinct_id, default=default, groups=groups)
255255

256256

257+
def get_feature_flag(key, distinct_id, groups):
258+
"""
259+
Get feature flag variant for users. Used with experiments.
260+
Example:
261+
```python
262+
if posthog.get_feature_flag('beta-feature', 'distinct_id') == 'test-variant':
263+
# do test variant code
264+
if posthog.get_feature_flag('beta-feature', 'distinct_id') == 'control':
265+
# do control code
266+
```
267+
"""
268+
return _proxy("get_feature_flag", key=key, distinct_id=distinct_id, groups=groups)
269+
270+
257271
def page(*args, **kwargs):
258272
"""Send a page call."""
259273
_proxy("page", *args, **kwargs)

posthog/client.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,10 +412,21 @@ def feature_enabled(self, key, distinct_id, default=False, *, groups={}):
412412
self.log.exception(f"[FEATURE FLAGS] Unable to get feature variants: {e}")
413413
response = default
414414
else:
415-
response = feature_flags.get(key, default)
415+
response = True if feature_flags.get(key) else default
416416
self.capture(distinct_id, "$feature_flag_called", {"$feature_flag": key, "$feature_flag_response": response})
417417
return response
418418

419+
def get_feature_flag(self, key, distinct_id, groups={}):
420+
require("key", key, string_types)
421+
require("distinct_id", distinct_id, ID_TYPES)
422+
require("groups", groups, dict)
423+
424+
variants = self.get_feature_variants(distinct_id, groups=groups)
425+
self.capture(
426+
distinct_id, "$feature_flag_called", {"$feature_flag": key, "$feature_flag_response": variants.get(key)}
427+
)
428+
return variants.get(key)
429+
419430

420431
# This function takes a distinct_id and a feature flag key and returns a float between 0 and 1.
421432
# Given the same distinct_id and key, it'll always return the same float. These floats are

posthog/test/test_client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ def test_feature_enabled_request_multi_variate(self, patch_decide):
524524
client.feature_flags = [
525525
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
526526
]
527-
self.assertEqual(client.feature_enabled("beta-feature", "distinct_id"), "variant-1")
527+
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
528528

529529
@mock.patch("posthog.client.get")
530530
def test_feature_enabled_simple_without_rollout_percentage(self, patch_get):
@@ -536,10 +536,19 @@ def test_feature_enabled_simple_without_rollout_percentage(self, patch_get):
536536
def test_feature_enabled_simple_with_none_rollout_percentage(self, patch_get):
537537
client = Client(TEST_API_KEY)
538538
client.feature_flags = [
539-
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percantage": None}
539+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": None}
540540
]
541541
self.assertTrue(client.feature_enabled("beta-feature", "distinct_id"))
542542

543+
@mock.patch("posthog.client.decide")
544+
def test_get_feature_flag(self, patch_decide):
545+
patch_decide.return_value = {"featureFlags": {"beta-feature": "variant-1"}}
546+
client = Client(TEST_API_KEY, personal_api_key="test")
547+
client.feature_flags = [
548+
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
549+
]
550+
self.assertEqual(client.get_feature_flag("beta-feature", "distinct_id"), "variant-1")
551+
543552
@mock.patch("posthog.client.Poller")
544553
@mock.patch("posthog.client.decide")
545554
def test_feature_enabled_doesnt_exist(self, patch_decide, patch_poll):

0 commit comments

Comments
 (0)