Skip to content

Have an option to send feature variants with the .capture(...) calls. #64

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

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 2 additions & 0 deletions posthog/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def capture(
timestamp=None, # type: Optional[datetime.datetime]
uuid=None, # type: Optional[str]
groups=None, # type: Optional[Dict]
send_feature_flags=False,
):
# type: (...) -> None
"""
Expand Down Expand Up @@ -58,6 +59,7 @@ def capture(
timestamp=timestamp,
uuid=uuid,
groups=groups,
send_feature_flags=send_feature_flags,
)


Expand Down
51 changes: 39 additions & 12 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,33 @@ def identify(self, distinct_id=None, properties=None, context=None, timestamp=No

return self._enqueue(msg)

def get_feature_variants(self, distinct_id, groups=None):
assert self.personal_api_key, "You have to specify a personal_api_key to use feature flags."
require("distinct_id", distinct_id, ID_TYPES)

if groups:
require("groups", groups, dict)
else:
groups = {}

request_data = {
"distinct_id": distinct_id,
"personal_api_key": self.personal_api_key,
"groups": groups,
}
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
return resp_data["featureFlags"]

def capture(
self, distinct_id=None, event=None, properties=None, context=None, timestamp=None, uuid=None, groups=None
self,
distinct_id=None,
event=None,
properties=None,
context=None,
timestamp=None,
uuid=None,
groups=None,
send_feature_flags=False,
):
properties = properties or {}
context = context or {}
Expand All @@ -142,6 +167,16 @@ def capture(
require("groups", groups, dict)
msg["properties"]["$groups"] = groups

if send_feature_flags:
try:
feature_variants = self.get_feature_variants(distinct_id, groups)
except Exception:
self.log.exception("[FEATURE FLAGS] Unable to get feature variants")
else:
for feature, variant in feature_variants.items():
msg["properties"]["$feature/{}".format(feature)] = variant
msg["properties"]["$active_feature_flags"] = list(feature_variants.keys())

return self._enqueue(msg)

def set(self, distinct_id=None, properties=None, context=None, timestamp=None, uuid=None):
Expand Down Expand Up @@ -367,20 +402,12 @@ def feature_enabled(self, key, distinct_id, default=False, *, groups={}):
response = _hash(key, distinct_id) <= (feature_flag.get("rollout_percentage", 100) / 100)
if response == None:
try:
request_data = {
"distinct_id": distinct_id,
"personal_api_key": self.personal_api_key,
"groups": groups,
}
resp_data = decide(self.api_key, self.host, timeout=10, **request_data)
feature_flags = self.get_feature_variants(distinct_id, groups=groups)
except Exception as e:
self.log.exception("[FEATURE FLAGS] Unable to get feature variants")
response = default
self.log.warning(
"[FEATURE FLAGS] Unable to get data for flag %s, because of the following error:" % key
)
self.log.warning(e)
else:
response = resp_data["featureFlags"].get(key, default)
response = feature_flags.get(key, default)
self.capture(distinct_id, "$feature_flag_called", {"$feature_flag": key, "$feature_flag_response": response})
return response

Expand Down
8 changes: 4 additions & 4 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def test_load_feature_flags_wrong_key(self):
@mock.patch("posthog.client.decide")
@mock.patch("posthog.client.get")
def test_feature_enabled_simple(self, patch_get, patch_decide):
client = Client(TEST_API_KEY)
client = Client(TEST_API_KEY, personal_api_key="test")
client.feature_flags = [
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": True, "rollout_percentage": 100}
]
Expand Down Expand Up @@ -435,7 +435,7 @@ def test_feature_enabled_simple_with_project_api_key(self, patch_get):
@mock.patch("posthog.client.decide")
def test_feature_enabled_request(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": True}}
client = Client(TEST_API_KEY)
client = Client(TEST_API_KEY, personal_api_key="test")
client.feature_flags = [
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
]
Expand All @@ -444,7 +444,7 @@ def test_feature_enabled_request(self, patch_decide):
@mock.patch("posthog.client.decide")
def test_feature_enabled_request_multi_variate(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": "variant-1"}}
client = Client(TEST_API_KEY)
client = Client(TEST_API_KEY, personal_api_key="test")
client.feature_flags = [
{"id": 1, "name": "Beta Feature", "key": "beta-feature", "is_simple_flag": False, "rollout_percentage": 100}
]
Expand Down Expand Up @@ -477,7 +477,7 @@ def test_feature_enabled_doesnt_exist(self, patch_decide, patch_poll):
@mock.patch("posthog.client.Poller")
@mock.patch("posthog.client.decide")
def test_personal_api_key_doesnt_exist(self, patch_decide, patch_poll):
client = Client(TEST_API_KEY)
client = Client(TEST_API_KEY, personal_api_key="test")
client.feature_flags = []

patch_decide.return_value = {"featureFlags": {"feature-flag": True}}
Expand Down