Skip to content

Commit cfdf1d2

Browse files
committed
fix: Deprecate send_feature_flag_events in get_feature_flag_payload
1 parent 9b4938e commit cfdf1d2

File tree

2 files changed

+20
-57
lines changed

2 files changed

+20
-57
lines changed

posthog/client.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import logging
33
import os
44
import sys
5+
import warnings
56
from datetime import datetime, timedelta
67
from typing import Any, Dict, Optional, Union
78
from typing_extensions import Unpack
@@ -1788,7 +1789,7 @@ def get_feature_flag_payload(
17881789
person_properties=None,
17891790
group_properties=None,
17901791
only_evaluate_locally=False,
1791-
send_feature_flag_events=True,
1792+
send_feature_flag_events=False,
17921793
disable_geoip=None,
17931794
):
17941795
"""
@@ -1802,7 +1803,7 @@ def get_feature_flag_payload(
18021803
person_properties: A dictionary of person properties.
18031804
group_properties: A dictionary of group properties.
18041805
only_evaluate_locally: Whether to only evaluate locally.
1805-
send_feature_flag_events: Whether to send feature flag events.
1806+
send_feature_flag_events: Deprecated. Use get_feature_flag() instead if you need events.
18061807
disable_geoip: Whether to disable GeoIP for this request.
18071808
18081809
Examples:
@@ -1818,6 +1819,14 @@ def get_feature_flag_payload(
18181819
Category:
18191820
Feature flags
18201821
"""
1822+
if send_feature_flag_events:
1823+
warnings.warn(
1824+
"send_feature_flag_events is deprecated in get_feature_flag_payload() and will be removed "
1825+
"in a future version. Use get_feature_flag() if you want to send $feature_flag_called events.",
1826+
DeprecationWarning,
1827+
stacklevel=2,
1828+
)
1829+
18211830
feature_flag_result = self._get_feature_flag_result(
18221831
key,
18231832
distinct_id,

posthog/test/test_feature_flags.py

Lines changed: 9 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,7 @@ def test_boolean_feature_flag_payload_decide(self, patch_flags, patch_capture):
30623062
"some-distinct-id",
30633063
match_value=True,
30643064
person_properties={"region": "USA"},
3065+
send_feature_flag_events=True,
30653066
),
30663067
300,
30673068
)
@@ -4051,7 +4052,9 @@ def test_capture_is_called_with_flag_details_and_payload(
40514052

40524053
self.assertEqual(
40534054
client.get_feature_flag_payload(
4054-
"decide-flag-with-payload", "some-distinct-id"
4055+
"decide-flag-with-payload",
4056+
"some-distinct-id",
4057+
send_feature_flag_events=True,
40554058
),
40564059
{"foo": "bar"},
40574060
)
@@ -4127,9 +4130,10 @@ def test_capture_is_called_but_does_not_add_all_flags(self, patch_flags):
41274130

41284131
@mock.patch.object(Client, "capture")
41294132
@mock.patch("posthog.client.flags")
4130-
def test_capture_is_called_in_get_feature_flag_payload(
4133+
def test_get_feature_flag_payload_does_not_send_feature_flag_called_events(
41314134
self, patch_flags, patch_capture
41324135
):
4136+
"""Test that get_feature_flag_payload does NOT send $feature_flag_called events"""
41334137
patch_flags.return_value = {
41344138
"featureFlags": {"person-flag": True},
41354139
"featureFlagPayloads": {"person-flag": 300},
@@ -4151,68 +4155,18 @@ def test_capture_is_called_in_get_feature_flag_payload(
41514155
"rollout_percentage": 100,
41524156
}
41534157
],
4158+
"payloads": {"true": '"payload"'},
41544159
},
41554160
}
41564161
]
41574162

4158-
# Call get_feature_flag_payload with match_value=None to trigger get_feature_flag
4159-
client.get_feature_flag_payload(
4160-
key="person-flag",
4161-
distinct_id="some-distinct-id",
4162-
person_properties={"region": "USA", "name": "Aloha"},
4163-
)
4164-
4165-
# Assert that capture was called once, with the correct parameters
4166-
self.assertEqual(patch_capture.call_count, 1)
4167-
patch_capture.assert_called_with(
4168-
"$feature_flag_called",
4169-
distinct_id="some-distinct-id",
4170-
properties={
4171-
"$feature_flag": "person-flag",
4172-
"$feature_flag_response": True,
4173-
"locally_evaluated": True,
4174-
"$feature/person-flag": True,
4175-
},
4176-
groups={},
4177-
disable_geoip=None,
4178-
)
4179-
4180-
# Reset mocks for further tests
4181-
patch_capture.reset_mock()
4182-
patch_flags.reset_mock()
4183-
4184-
# Call get_feature_flag_payload again for the same user; capture should not be called again because we've already reported an event for this distinct_id + flag
4185-
client.get_feature_flag_payload(
4163+
payload = client.get_feature_flag_payload(
41864164
key="person-flag",
41874165
distinct_id="some-distinct-id",
41884166
person_properties={"region": "USA", "name": "Aloha"},
41894167
)
4190-
4168+
self.assertIsNotNone(payload)
41914169
self.assertEqual(patch_capture.call_count, 0)
4192-
patch_capture.reset_mock()
4193-
4194-
# Call get_feature_flag_payload for a different user; capture should be called
4195-
client.get_feature_flag_payload(
4196-
key="person-flag",
4197-
distinct_id="some-distinct-id2",
4198-
person_properties={"region": "USA", "name": "Aloha"},
4199-
)
4200-
4201-
self.assertEqual(patch_capture.call_count, 1)
4202-
patch_capture.assert_called_with(
4203-
"$feature_flag_called",
4204-
distinct_id="some-distinct-id2",
4205-
properties={
4206-
"$feature_flag": "person-flag",
4207-
"$feature_flag_response": True,
4208-
"locally_evaluated": True,
4209-
"$feature/person-flag": True,
4210-
},
4211-
groups={},
4212-
disable_geoip=None,
4213-
)
4214-
4215-
patch_capture.reset_mock()
42164170

42174171
@mock.patch("posthog.client.flags")
42184172
def test_fallback_to_api_in_get_feature_flag_payload_when_flag_has_static_cohort(

0 commit comments

Comments
 (0)