Skip to content

Commit 30112aa

Browse files
authored
feat: only track exposure when experiment feature is enabled (#226)
1 parent 134e6b6 commit 30112aa

2 files changed

Lines changed: 35 additions & 2 deletions

File tree

flagsmith/flagsmith.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,13 @@ def get_experiment_flag(
357357
358358
Skips the exposure event when the resolved flag is a `DefaultFlag`
359359
(i.e. the feature was not present and was served via the
360-
`default_flag_handler`), to keep experimentation data clean.
360+
`default_flag_handler`) or when the feature is disabled, to keep
361+
experimentation data clean.
361362
"""
362363
if not self._event_processor:
363364
raise ValueError("Events must be enabled to use experiment flags.")
364365
flag = self.get_identity_flags(identifier, traits).get_flag(feature_name)
365-
if isinstance(flag, Flag):
366+
if isinstance(flag, Flag) and flag.enabled:
366367
self.track_exposure_event(
367368
feature_name=feature_name,
368369
identifier=identifier,

tests/test_flagsmith.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,3 +1178,35 @@ def test_get_experiment_flag_falls_back_to_value_without_variant(
11781178
traits=None,
11791179
metadata=None,
11801180
)
1181+
1182+
1183+
def test_get_experiment_flag_skips_exposure_for_disabled_feature(
1184+
mocker: MockerFixture, api_key: str
1185+
) -> None:
1186+
# Given - a resolved flag for a disabled feature
1187+
config = EventProcessorConfig(events_api_url="http://test/")
1188+
flagsmith = Flagsmith(
1189+
environment_key=api_key, enable_events=True, event_processor_config=config
1190+
)
1191+
flag = Flag(
1192+
enabled=False,
1193+
value="blue",
1194+
feature_name="checkout_v2",
1195+
feature_id=1,
1196+
variant="control",
1197+
)
1198+
mocker.patch.object(
1199+
flagsmith,
1200+
"get_identity_flags",
1201+
return_value=Flags(flags={"checkout_v2": flag}),
1202+
)
1203+
mock_track = mocker.patch.object(flagsmith._event_processor, "track_exposure_event")
1204+
1205+
# When
1206+
result = flagsmith.get_experiment_flag(
1207+
feature_name="checkout_v2", identifier="user1"
1208+
)
1209+
1210+
# Then - the flag is returned but no exposure event is tracked
1211+
assert result is flag
1212+
mock_track.assert_not_called()

0 commit comments

Comments
 (0)