Skip to content

Commit 969be5f

Browse files
experiment_id and variation_id added to payloads
1 parent 55bc008 commit 969be5f

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

optimizely/optimizely.py

+34-5
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ def _get_feature_variable_for_type(
340340
user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)
341341

342342
decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
343-
343+
experiment_id = decision.experiment.id if decision.experiment else None
344+
variation_id = decision.variation.id if decision.variation else None
345+
344346
if decision.variation:
345347

346348
feature_enabled = decision.variation.featureEnabled
@@ -386,6 +388,8 @@ def _get_feature_variable_for_type(
386388
'variable_value': actual_value,
387389
'variable_type': variable_type,
388390
'source_info': source_info,
391+
'experiment_id': experiment_id,
392+
'variation_id': variation_id
389393
},
390394
)
391395
return actual_value
@@ -427,7 +431,9 @@ def _get_all_feature_variables_for_type(
427431
user_context = OptimizelyUserContext(self, self.logger, user_id, attributes, False)
428432

429433
decision, _ = self.decision_service.get_variation_for_feature(project_config, feature_flag, user_context)
430-
434+
experiment_id = decision.experiment.id if decision.experiment else None
435+
variation_id = decision.variation.id if decision.variation else None
436+
431437
if decision.variation:
432438

433439
feature_enabled = decision.variation.featureEnabled
@@ -480,6 +486,8 @@ def _get_all_feature_variables_for_type(
480486
'variable_values': all_variables,
481487
'source': decision.source,
482488
'source_info': source_info,
489+
'experiment_id': experiment_id,
490+
'variation_id': variation_id
483491
},
484492
)
485493
return all_variables
@@ -646,13 +654,21 @@ def get_variation(
646654
decision_notification_type = enums.DecisionNotificationTypes.FEATURE_TEST
647655
else:
648656
decision_notification_type = enums.DecisionNotificationTypes.AB_TEST
649-
657+
658+
experiment_id = experiment.id if experiment else None
659+
variation_id = variation.id if variation else None
660+
650661
self.notification_center.send_notifications(
651662
enums.NotificationTypes.DECISION,
652663
decision_notification_type,
653664
user_id,
654665
attributes or {},
655-
{'experiment_key': experiment_key, 'variation_key': variation_key},
666+
{
667+
'experiment_key': experiment_key,
668+
'variation_key': variation_key,
669+
'experiment_id': experiment_id,
670+
'variation_id': variation_id
671+
},
656672
)
657673

658674
return variation_key
@@ -738,6 +754,8 @@ def is_feature_enabled(self, feature_key: str, user_id: str, attributes: Optiona
738754
'feature_enabled': feature_enabled,
739755
'source': decision.source,
740756
'source_info': source_info,
757+
'experiment_id': decision.experiment.id,
758+
'variation_id': decision.variation.id
741759
},
742760
)
743761

@@ -1202,6 +1220,15 @@ def _create_optimizely_decision(
12021220
if flag_decision is not None and flag_decision.variation is not None
12031221
else None
12041222
)
1223+
1224+
rollout_id = feature_flag.rolloutId if decision_source == DecisionSources.ROLLOUT else None
1225+
experiment_id = project_config.get_experiment_id_by_key_or_rollout_id(rule_key, rollout_id)
1226+
variation_id = None
1227+
if variation_key:
1228+
variation = project_config.get_variation_from_key_by_experiment_id(experiment_id, variation_key)
1229+
if variation:
1230+
variation_id = variation.id
1231+
12051232
# Send notification
12061233
self.notification_center.send_notifications(
12071234
enums.NotificationTypes.DECISION,
@@ -1215,7 +1242,9 @@ def _create_optimizely_decision(
12151242
'variation_key': variation_key,
12161243
'rule_key': rule_key,
12171244
'reasons': decision_reasons if should_include_reasons else [],
1218-
'decision_event_dispatched': decision_event_dispatched
1245+
'decision_event_dispatched': decision_event_dispatched,
1246+
'experiment_id': experiment_id,
1247+
'variation_id': variation_id
12191248

12201249
},
12211250
)

optimizely/project_config.py

+28
Original file line numberDiff line numberDiff line change
@@ -716,3 +716,31 @@ def get_flag_variation(
716716
return variation
717717

718718
return None
719+
720+
def get_experiment_id_by_key_or_rollout_id(self, key: str, rollout_id: Optional[str] = None) -> Optional[str]:
721+
"""
722+
Retrieves the experiment ID associated with a given rule key or a specific rollout.
723+
724+
Args:
725+
key: The key associated with the experiment rule.
726+
rollout_id: The ID of the rollout to search if the key is not found.
727+
728+
Returns:
729+
Optional[str]: The experiment ID if found, otherwise None.
730+
"""
731+
# Try getting the experiment from experiment_key_map first
732+
if key:
733+
experiment = self.get_experiment_from_key(key)
734+
if experiment:
735+
return experiment.id
736+
737+
# If key is not found in experiment_key_map, check a specific rollout (if provided)
738+
if rollout_id:
739+
rollout = self.get_rollout_from_id(rollout_id)
740+
if rollout:
741+
for experiment in rollout.experiments:
742+
experiment = entities.Experiment(**experiment)
743+
if experiment.key == key:
744+
return experiment.id
745+
746+
return None

0 commit comments

Comments
 (0)