Skip to content

Commit d5727e1

Browse files
committed
send exposure-time user context attributes with experiment ingestor events
1 parent ac7e773 commit d5727e1

2 files changed

Lines changed: 65 additions & 7 deletions

File tree

‎growthbook/plugins/growthbook_tracking.py‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ def cleanup(self) -> None:
173173

174174
def _setup_experiment_tracking(self, gb_instance: Any) -> None:
175175
def tracking_wrapper(experiment: Any, result: Any, user_context: Any = None) -> None:
176-
self._track_experiment_viewed(experiment, result)
176+
self._track_experiment_viewed(experiment, result, user_context)
177177
if self.additional_callback:
178178
self._safe_execute(self.additional_callback, experiment, result, user_context)
179179

@@ -218,11 +218,25 @@ def eval_feature_wrapper(key: str, *args: Any, **kwargs: Any) -> Any:
218218

219219
gb_instance.eval_feature = eval_feature_wrapper
220220

221-
def _track_experiment_viewed(self, experiment: Any, result: Any) -> None:
221+
def _track_experiment_viewed(
222+
self, experiment: Any, result: Any, user_context: Any = None
223+
) -> None:
222224
try:
223-
attrs: Dict[str, Any] = {}
224-
if self._gb_instance is not None:
225-
attrs = getattr(self._gb_instance, "_attributes", {}) or {}
225+
# Prefer the exposure-time user context both clients pass to the
226+
# tracking callback (the JS plugin does the same via userContext).
227+
# The instance fallback only exists for the sync client's legacy
228+
# two-argument invocation paths; the async client has no
229+
# _attributes, so without user_context its events had none.
230+
attrs: Dict[str, Any] = (
231+
getattr(user_context, "attributes", None)
232+
or getattr(self._gb_instance, "_attributes", None)
233+
or {}
234+
)
235+
url: str = (
236+
getattr(user_context, "url", None)
237+
or getattr(self._gb_instance, "_url", None)
238+
or ""
239+
)
226240

227241
payload = _build_event_payload(
228242
event_name="$$experiment_viewed",
@@ -236,7 +250,7 @@ def _track_experiment_viewed(self, experiment: Any, result: Any) -> None:
236250
"hash_value": result.hashValue,
237251
},
238252
attributes=attrs,
239-
url=getattr(self._gb_instance, "_url", "") if self._gb_instance else "",
253+
url=url,
240254
sdk_version=self._get_sdk_version(),
241255
)
242256
self._add_event_to_batch(payload)

‎tests/test_plugins.py‎

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,4 +654,48 @@ def test_build_event_payload_device_id_fallback(self):
654654
self.assertEqual(payload["device_id"], "anon-1")
655655

656656
payload2 = _build_event_payload("e", {}, {"id": "raw-id"}, "", "x")
657-
self.assertEqual(payload2["device_id"], "raw-id")
657+
self.assertEqual(payload2["device_id"], "raw-id")
658+
659+
class TestExperimentEventContext(unittest.TestCase):
660+
"""Experiment-viewed ingestor events must carry the exposure-time user
661+
context (the attributes used for bucketing/leaf routing), matching the JS
662+
plugin's use of userContext. The instance fallback only covers the sync
663+
client's legacy two-argument invocation."""
664+
665+
def _make_plugin_and_events(self):
666+
from growthbook.plugins.growthbook_tracking import GrowthBookTrackingPlugin
667+
668+
plugin = GrowthBookTrackingPlugin(ingestor_host="https://test.growthbook.io")
669+
events = []
670+
plugin._add_event_to_batch = events.append
671+
return plugin, events
672+
673+
def _result_stub(self):
674+
return MagicMock(
675+
variationId=1, inExperiment=True, hashUsed=True,
676+
hashAttribute="id", hashValue="u1", key="1",
677+
)
678+
679+
def test_event_uses_exposure_time_user_context(self):
680+
plugin, events = self._make_plugin_and_events()
681+
user_context = MagicMock(
682+
attributes={"id": "u1", "country": "US"}, url="https://app.example.com"
683+
)
684+
plugin._track_experiment_viewed(
685+
Experiment(key="exp", variations=[0, 1]), self._result_stub(), user_context
686+
)
687+
self.assertEqual(len(events), 1)
688+
self.assertEqual(events[0]["context_json"], {"country": "US"})
689+
self.assertEqual(events[0]["device_id"], "u1")
690+
self.assertEqual(events[0]["url"], "https://app.example.com")
691+
692+
def test_event_falls_back_to_instance_attributes(self):
693+
plugin, events = self._make_plugin_and_events()
694+
plugin._gb_instance = MagicMock(
695+
_attributes={"id": "u2"}, _url="https://legacy.example.com"
696+
)
697+
plugin._track_experiment_viewed(
698+
Experiment(key="exp", variations=[0, 1]), self._result_stub(), None
699+
)
700+
self.assertEqual(events[0]["device_id"], "u2")
701+
self.assertEqual(events[0]["url"], "https://legacy.example.com")

0 commit comments

Comments
 (0)