Skip to content

Commit b44ba1e

Browse files
feat: added "enabled" field to decision metadata structure (#301)
1 parent c816d3b commit b44ba1e

File tree

4 files changed

+34
-10
lines changed

4 files changed

+34
-10
lines changed

pkg/client/client.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (o *OptimizelyClient) Activate(experimentKey string, userContext entities.U
7777
// send an impression event
7878
result = experimentDecision.Variation.Key
7979
if ue, ok := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, *decisionContext.Experiment,
80-
experimentDecision.Variation, userContext, "", experimentKey, "experiment"); ok {
80+
experimentDecision.Variation, userContext, "", experimentKey, "experiment", true); ok {
8181
o.EventProcessor.ProcessEvent(ue)
8282
}
8383
}
@@ -132,7 +132,7 @@ func (o *OptimizelyClient) IsFeatureEnabled(featureKey string, userContext entit
132132
}
133133

134134
if ue, ok := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, featureDecision.Experiment,
135-
featureDecision.Variation, userContext, featureKey, featureDecision.Experiment.Key, featureDecision.Source); ok {
135+
featureDecision.Variation, userContext, featureKey, featureDecision.Experiment.Key, featureDecision.Source, result); ok && featureDecision.Source != "" {
136136
o.EventProcessor.ProcessEvent(ue)
137137
}
138138

@@ -476,7 +476,7 @@ func (o *OptimizelyClient) GetDetailedFeatureDecisionUnsafe(featureKey string, u
476476
if !disableTracking {
477477
// send impression event for feature tests
478478
if ue, ok := event.CreateImpressionUserEvent(decisionContext.ProjectConfig, featureDecision.Experiment,
479-
featureDecision.Variation, userContext, featureKey, featureDecision.Experiment.Key, featureDecision.Source); ok {
479+
featureDecision.Variation, userContext, featureKey, featureDecision.Experiment.Key, featureDecision.Source, decisionInfo.Enabled); ok {
480480
o.EventProcessor.ProcessEvent(ue)
481481
}
482482
}

pkg/event/events.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type DecisionMetadata struct {
5555
RuleKey string `json:"rule_key"`
5656
RuleType string `json:"rule_type"`
5757
VariationKey string `json:"variation_key"`
58+
Enabled bool `json:"enabled"`
5859
}
5960

6061
// ConversionEvent represents a conversion event

pkg/event/factory.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ func createImpressionEvent(
6464
experiment entities.Experiment,
6565
variation *entities.Variation,
6666
attributes map[string]interface{},
67-
flagKey, ruleKey, ruleType string,
67+
flagKey, ruleKey, ruleType string, enabled bool,
6868
) ImpressionEvent {
6969

7070
metadata := DecisionMetadata{
7171
FlagKey: flagKey,
7272
RuleKey: ruleKey,
7373
RuleType: ruleType,
74+
Enabled: enabled,
7475
}
7576

7677
var variationID string
@@ -94,13 +95,13 @@ func createImpressionEvent(
9495

9596
// CreateImpressionUserEvent creates and returns ImpressionEvent for user
9697
func CreateImpressionUserEvent(projectConfig config.ProjectConfig, experiment entities.Experiment,
97-
variation *entities.Variation, userContext entities.UserContext, flagKey, ruleKey, ruleType string) (UserEvent, bool) {
98+
variation *entities.Variation, userContext entities.UserContext, flagKey, ruleKey, ruleType string, enabled bool) (UserEvent, bool) {
9899

99100
if (ruleType == decisionPkg.Rollout || variation == nil) && !projectConfig.SendFlagDecisions() {
100101
return UserEvent{}, false
101102
}
102103

103-
impression := createImpressionEvent(projectConfig, experiment, variation, userContext.Attributes, flagKey, ruleKey, ruleType)
104+
impression := createImpressionEvent(projectConfig, experiment, variation, userContext.Attributes, flagKey, ruleKey, ruleType, enabled)
104105

105106
userEvent := UserEvent{}
106107
userEvent.Timestamp = makeTimestamp()

pkg/event/factory_test.go

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ var userContext = entities.UserContext{
103103

104104
func BuildTestImpressionEvent() UserEvent {
105105
tc := TestConfig{}
106-
impressionUserEvent, _ := CreateImpressionUserEvent(tc, testExperiment, &testVariation, userContext, "", testExperiment.Key, "experiment")
106+
impressionUserEvent, _ := CreateImpressionUserEvent(tc, testExperiment, &testVariation, userContext, "", testExperiment.Key, "experiment", true)
107107
return impressionUserEvent
108108
}
109109

@@ -195,20 +195,42 @@ func TestCreateImpressionUserEvent(t *testing.T) {
195195
}
196196

197197
for _, scenario := range scenarios {
198-
_, ok := CreateImpressionUserEvent(tc, testExperiment, &testVariation, userContext, "", testExperiment.Key, scenario.flagType)
198+
userEvent, ok := CreateImpressionUserEvent(tc, testExperiment, &testVariation, userContext, "", testExperiment.Key, scenario.flagType, true)
199199
assert.Equal(t, ok, scenario.expected)
200+
201+
if ok {
202+
metaData := userEvent.Impression.Metadata
203+
assert.Equal(t, "", metaData.FlagKey)
204+
assert.Equal(t, testExperiment.Key, metaData.RuleKey)
205+
assert.Equal(t, scenario.flagType, metaData.RuleType)
206+
assert.Equal(t, true, metaData.Enabled)
207+
}
200208
}
201209

202210
// nil variation should _always_ return false
203211
for _, scenario := range scenarios {
204-
_, ok := CreateImpressionUserEvent(tc, testExperiment, nil, userContext, "", testExperiment.Key, scenario.flagType)
212+
userEvent, ok := CreateImpressionUserEvent(tc, testExperiment, nil, userContext, "", testExperiment.Key, scenario.flagType, false)
205213
assert.False(t, ok)
214+
if ok {
215+
metaData := userEvent.Impression.Metadata
216+
assert.Equal(t, "", metaData.FlagKey)
217+
assert.Equal(t, testExperiment.Key, metaData.RuleKey)
218+
assert.Equal(t, scenario.flagType, metaData.RuleType)
219+
assert.Equal(t, false, metaData.Enabled)
220+
}
206221
}
207222

208223
// should _always_ return true if sendFlagDecisions is set
209224
tc.sendFlagDecisions = true
210225
for _, scenario := range scenarios {
211-
_, ok := CreateImpressionUserEvent(tc, testExperiment, nil, userContext, "", testExperiment.Key, scenario.flagType)
226+
userEvent, ok := CreateImpressionUserEvent(tc, testExperiment, nil, userContext, "", testExperiment.Key, scenario.flagType, true)
212227
assert.True(t, ok)
228+
if ok {
229+
metaData := userEvent.Impression.Metadata
230+
assert.Equal(t, "", metaData.FlagKey)
231+
assert.Equal(t, testExperiment.Key, metaData.RuleKey)
232+
assert.Equal(t, scenario.flagType, metaData.RuleType)
233+
assert.Equal(t, true, metaData.Enabled)
234+
}
213235
}
214236
}

0 commit comments

Comments
 (0)