10000 feat(flag_decisions): update decisionSnapshot in event-processor to i… · rserrano-eSW/javascript-sdk@a24bba8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a24bba8

Browse files
authored
feat(flag_decisions): update decisionSnapshot in event-processor to include metadata object (optimizely#601)
* Include metadata object to makeDecisionSnapshot * Update CHANGELOG * Fix specs
1 parent 0576e46 commit a24bba8

File tree

6 files changed

+150
-5
lines changed

6 files changed

+150
-5
lines changed

packages/event-processor/CHANGELOG.MD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
66

77
## [Unreleased]
88
Changes that have landed but are not yet released.
9+
- Update `Visitor.Snapshot` to include metadata object to support sending flag decisions.
910

1011
## [0.6.0] - July 28, 2020
1112

packages/event-processor/__tests__/buildEventV1.spec.ts

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { ImpressionEvent, ConversionEvent } from '../src/events'
2424

2525
describe('buildEventV1', () => {
2626
describe('buildImpressionEventV1', () => {
27-
it('should build an build an ImpressionEventV1', () => {
27+
it('should build an ImpressionEventV1 when experiment and variation are defined', () => {
2828
const impressionEvent: ImpressionEvent = {
2929
type: 'impression',
3030
timestamp: 69,
@@ -58,6 +58,10 @@ describe('buildEventV1', () => {
5858
id: 'varId',
5959
key: 'varKey',
6060
},
61+
62+
ruleKey: 'expKey',
63+
flagKey: 'flagKey1',
64+
ruleType: 'experiment',
6165
}
6266

6367
const result = buildImpressionEventV1(impressionEvent)
@@ -79,6 +83,12 @@ describe('buildEventV1', () => {
7983
campaign_id: 'layerId',
8084
experiment_id: 'expId',
8185
variation_id: 'varId',
86+
metadata: {
87+
flag_key: 'flagKey1',
88+
rule_key: 'expKey',
89+
rule_type: 'experiment',
90+
variation_key: 'varKey',
91+
},
8292
},
8393
],
8494
events: [
@@ -110,6 +120,103 @@ describe('buildEventV1', () => {
110120
],
111121
})
112122
})
123+
124+
it('should build an ImpressionEventV1 when experiment and variation are not defined', () => {
125+
const impressionEvent: ImpressionEvent = {
126+
type: 'impression',
127+
timestamp: 69,
128+
uuid: 'uuid',
129+
130+
context: {
131+
accountId: 'accountId',
132+
projectId: 'projectId',
133+
clientName: 'node-sdk',
134+
clientVersion: '3.0.0',
135+
revision: 'revision',
136+
botFiltering: true,
137+
anonymizeIP: true,
138+
},
139+
140+
user: {
141+
id: 'userId',
142+
attributes: [{ entityId: 'attr1-id', key: 'attr1-key', value: 'attr1-value' }],
143+
},
144+
145+
layer: {
146+
id: null,
147+
},
148+
149+
experiment: {
150+
id: null,
151+
key: '',
152+
},
153+
154+
variation: {
155+
id: null,
156+
key: '',
157+
},
158+
159+
ruleKey: '',
160+
flagKey: 'flagKey1',
161+
ruleType: 'rollout',
162+
}
163+
164+
const result = buildImpressionEventV1(impressionEvent)
165+
expect(result).toEqual({
166+
client_name: 'node-sdk',
167+
client_version: '3.0.0',
168+
account_id: 'accountId',
169+
project_id: 'projectId',
170+
revision: 'revision',
171+
anonymize_ip: true,
172+
enrich_decisions: true,
173+
174+
visitors: [
175+
{
176+
snapshots: [
177+
{
178+
decisions: [
179+
{
180+
campaign_id: null,
181+
experiment_id: null,
182+
variation_id: null,
183+
metadata: {
184+
flag_key: 'flagKey1',
185+
rule_key: '',
186+
rule_type: 'rollout',
187+
variation_key: '',
188+
},
189+
},
190+
],
191+
events: [
192+
{
193+
entity_id: null,
194+
timestamp: 69,
195+
key: 'campaign_activated',
196+
uuid: 'uuid',
197+
},
198+
],
199+
},
200+
],
201+
visitor_id: 'userId',
202+
attributes: [
203+
{
204+
entity_id: 'attr1-id',
205+
key: 'attr1-key',
206+
type: 'custom',
207+
value: 'attr1-value',
208+
},
209+
{
210+
entity_id: '$opt_bot_filtering',
211+
key: '$opt_bot_filtering',
212+
type: 'custom',
213+
value: true,
214+
},
215+
],
216+
},
217+
],
218+
})
219+
})
113220
})
114221

115222
describe('buildConversionEventV1', () => {
@@ -438,6 +545,10 @@ describe('buildEventV1', () => {
438545
id: 'varId',
439546
key: 'varKey',
440547
},
548+
549+
ruleKey: 'expKey',
550+
flagKey: 'flagKey1',
551+
ruleType: 'experiment',
441552
}
442553

443554
const result = makeBatchedEventV1([impressionEvent, conversionEvent])
@@ -460,6 +571,12 @@ describe('buildEventV1', () => {
460571
campaign_id: 'layerId',
461572
experiment_id: 'expId',
462573
variation_id: 'varId',
574+
metadata: {
575+
flag_key: 'flagKey1',
576+
rule_key: 'expKey',
577+
rule_type: 'experiment',
578+
variation_key: 'varKey',
579+
},
463580
},
464581
],
465582
events: [

packages/event-processor/__tests__/v1EventProcessor.react_native.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ function createImpressionEvent() {
6262
id: 'varId',
6363
key: 'varKey',
6464
},
65+
66+
ruleKey: 'expKey',
67+
flagKey: 'flagKey1',
68+
ruleType: 'experiment',
6569
}
6670
}
6771

packages/event-processor/__tests__/v1EventProcessor.spec.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ function createImpressionEvent() {
5959
id: 'varId',
6060
key: 'varKey',
6161
},
62+
63+
ruleKey: 'expKey',
64+
flagKey: 'flagKey1',
65+
ruleType: 'experiment',
6266
}
6367
}
6468

packages/event-processor/src/events.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,22 @@ export interface ImpressionEvent extends BaseEvent {
4545
}
4646

4747
layer: {
48-
id: string
48+
id: string | null
4949
} | null
5050

5151
experiment: {
52-
id: string
52+
id: string | null
5353
key: string
5454
} | null
5555

5656
variation: {
57-
id: string
57+
id: string | null
5858
key: string
5959
} | null
60+
61+
ruleKey: string
62+
flagKey: string
63+
ruleType: string
6064
}
6165

6266
export interface ConversionEvent extends BaseEvent {

packages/event-processor/src/v1/buildEventV1.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ namespace Visitor {
4444
campaign_id: string | null
4545
experiment_id: string | null
4646
variation_id: string | null
47+
metadata: Metadata
48+
}
49+
50+
type Metadata = {
51+
flag_key: string;
52+
rule_key: string;
53+
rule_type: string;
54+
variation_key: string;
4755
}
4856

4957
export type SnapshotEvent = {
@@ -135,17 +143,24 @@ function makeConversionSnapshot(conversion: ConversionEvent): Visitor.Snapshot {
135143
}
136144

137145
function makeDecisionSnapshot(event: ImpressionEvent): Visitor.Snapshot {
138-
const { layer, experiment, variation } = event
146+
const { layer, experiment, variation, ruleKey, flagKey, ruleType } = event
139147
let layerId = layer ? layer.id : null
140148
let experimentId = experiment ? experiment.id : null
141149
let variationId = variation ? variation.id : null
150+
let variationKey = variation ? variation.key : ''
142151

143152
return {
144153
decisions: [
145154
{
146155
campaign_id: layerId,
147156
experiment_id: experimentId,
148157
variation_id: variationId,
158+
metadata: {
159+
flag_key: flagKey,
160+
rule_key: ruleKey,
161+
rule_type: ruleType,
162+
variation_key: variationKey,
163+
},
149164
},
150165
],
151166
events: [

0 commit comments

Comments
 (0)
0