@@ -12,13 +12,19 @@ export { VariableValuesObject, VariableValue }
12
12
13
13
type Partial < T > = { [ P in keyof T ] ?: T [ P ] }
14
14
15
+ // TODO use optimizely.UserAttributes when this is fixed in
16
+ // https://github.com/optimizely/javascript-sdk/issues/211
17
+ type UserAttributes = {
18
+ [ attribute : string ] : any
19
+ }
20
+
15
21
export interface OptimizelySDKWrapperConfig extends Partial < optimizely . Config > {
16
22
datafile ?: OptimizelyDatafile
17
23
sdkKey ?: string
18
24
UNSTABLE_datafileLoader ?: ResourceLoader < OptimizelyDatafile >
19
25
20
- attributes ?: optimizely . UserAttributes
21
- UNSTABLE_attributesLoader ?: ResourceLoader < optimizely . UserAttributes >
26
+ attributes ?: UserAttributes
27
+ UNSTABLE_attributesLoader ?: ResourceLoader < UserAttributes >
22
28
23
29
userId ?: string
24
30
UNSTABLE_userIdLoader ?: ResourceLoader < UserId >
@@ -27,7 +33,7 @@ export interface OptimizelySDKWrapperConfig extends Partial<optimizely.Config> {
27
33
type TrackEventCallArgs = [
28
34
string ,
29
35
string | undefined ,
30
- optimizely . UserAttributes | undefined ,
36
+ UserAttributes | undefined ,
31
37
optimizely . EventTags | undefined
32
38
]
33
39
@@ -42,7 +48,7 @@ export class OptimizelySDKWrapper {
42
48
43
49
public datafile : OptimizelyDatafile | null
44
50
private userId : string | null
45
- private attributes : optimizely . UserAttributes
51
+ private attributes : UserAttributes
46
52
47
53
private initialConfig : OptimizelySDKWrapperConfig
48
54
@@ -117,40 +123,38 @@ export class OptimizelySDKWrapper {
117
123
*
118
124
*
119
125
* @param {string } experimentKey
120
- * @param {string } [overrideUserId ]
121
- * @param {optimizely. UserAttributes } [overrideAttributes ]
126
+ * @param {string } [userId ]
127
+ * @param {UserAttributes } [attributes ]
122
128
* @returns {(string | null) }
123
129
* @memberof OptimizelySDKWrapper
124
130
*/
125
131
public activate (
126
132
experimentKey : string ,
127
- overrideUserId ? : string ,
128
- overrideAttributes ?: optimizely . UserAttributes ,
133
+ userId : string ,
134
+ attributes ?: UserAttributes ,
129
135
) : string | null {
130
136
if ( ! this . isInitialized ) {
131
137
return null
132
138
}
133
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
134
139
return this . instance . activate ( experimentKey , userId , attributes )
135
140
}
136
141
137
142
/**
138
143
*
139
144
* @param {string } experimentKey
140
- * @param {string } [overrideUserId ]
141
- * @param {optimizely. UserAttributes } [overrideAttributes ]
145
+ * @param {string } [userId ]
146
+ * @param {UserAttributes } [attributes ]
142
147
* @returns {(string | null) }
143
148
* @memberof OptimizelySDKWrapper
144
149
*/
145
150
public getVariation (
146
151
experimentKey : string ,
147
- overrideUserId ? : string ,
148
- overrideAttributes ?: optimizely . UserAttributes ,
152
+ userId : string ,
153
+ attributes ?: UserAttributes ,
149
154
) : string | null {
150
155
if ( ! this . isInitialized ) {
151
156
return null
152
157
}
153
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
154
158
return this . instance . getVariation ( experimentKey , userId , attributes )
155
159
}
156
160
@@ -163,28 +167,21 @@ export class OptimizelySDKWrapper {
163
167
* The first is a shortcut in the case where userId and attributes are stored on the SDK instance
164
168
*
165
169
* @param {string } eventKey
166
- * @param {(string | optimizely.EventTags) } [overrideUserId ]
167
- * @param {optimizely. UserAttributes } [overrideAttributes ]
170
+ * @param {(string | optimizely.EventTags) } [userId ]
171
+ * @param {UserAttributes } [attributes ]
168
172
* @param {optimizely.EventTags } [eventTags]
169
173
* @memberof OptimizelySDKWrapper
170
174
*/
171
175
public track (
172
176
eventKey : string ,
173
- overrideUserId ? : string | optimizely . EventTags ,
174
- overrideAttributes ?: optimizely . UserAttributes ,
177
+ userId : string ,
178
+ attributes ?: UserAttributes ,
175
179
eventTags ?: optimizely . EventTags ,
176
180
) : void {
177
- if ( typeof overrideUserId !== 'undefined' && typeof overrideUserId !== 'string' ) {
178
- eventTags = overrideUserId
179
- overrideUserId = undefined
180
- overrideAttributes = undefined
181
- }
182
-
183
181
if ( ! this . isInitialized ) {
184
- this . trackEventQueue . push ( [ eventKey , overrideUserId , overrideAttributes , eventTags ] )
182
+ this . trackEventQueue . push ( [ eventKey , userId , attributes , eventTags ] )
185
183
return
186
184
}
187
- let [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
188
185
this . instance . track ( eventKey , userId , attributes , eventTags )
189
186
}
190
187
@@ -193,41 +190,39 @@ export class OptimizelySDKWrapper {
193
190
* loaded, this will return `false`
194
191
*
195
192
* @param {string } feature
196
- * @param {string } [overrideUserId ]
197
- * @param {optimizely. UserAttributes } [overrideAttributes ]
193
+ * @param {string } [userId ]
194
+ * @param {UserAttributes } [attributes ]
198
195
* @returns {boolean }
199
196
* @memberof OptimizelySDKWrapper
200
197
*/
201
198
public isFeatureEnabled (
202
199
feature : string ,
203
- overrideUserId ? : string ,
204
- overrideAttributes ?: optimizely . UserAttributes ,
200
+ userId : string ,
201
+ attributes ?: UserAttributes ,
205
202
) : boolean {
206
203
if ( ! this . isInitialized ) {
207
204
return false
208
205
}
209
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
210
206
return this . instance . isFeatureEnabled ( feature , userId , attributes )
211
207
}
212
208
213
209
/**
214
210
* Get all variables for a feature, regardless of the feature being enabled/disabled
215
211
*
216
212
* @param {string } feature
217
- * @param {string } [overrideUserId ]
218
- * @param {optimizely. UserAttributes } [overrideAttributes ]
213
+ * @param {string } [userId ]
214
+ * @param {UserAttributes } [attributes ]
219
215
* @returns {VariableValuesObject }
220
216
* @memberof OptimizelySDKWrapper
221
217
*/
222
218
public getFeatureVariables (
223
219
feature : string ,
224
- overrideUserId ? : string ,
225
- overrideAttributes ?: optimizely . UserAttributes ,
220
+ userId : string ,
221
+ attributes ?: UserAttributes ,
226
222
) : VariableValuesObject {
227
223
if ( ! this . isInitialized ) {
228
224
return { }
229
225
}
230
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
231
226
const variableDefs = this . getVariableDefsForFeature ( feature )
232
227
if ( ! variableDefs ) {
233
228
// TODO: error
@@ -261,130 +256,90 @@ export class OptimizelySDKWrapper {
261
256
public getFeatureVariableString (
262
257
feature : string ,
263
258
variable : string ,
264
- overrideUserId ? : string ,
265
- overrideAttributes ?: optimizely . UserAttributes ,
259
+ userId : string ,
260
+ attributes ?: UserAttributes ,
266
261
) : string | null {
267
262
if ( ! this . isInitialized ) {
268
263
return null
269
264
}
270
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
271
265
272
266
return this . instance . getFeatureVariableString ( feature , variable , userId , attributes )
273
267
}
274
268
275
269
public getFeatureVariableBoolean (
276
270
feature : string ,
277
271
variable : string ,
278
- overrideUserId ? : string ,
279
- overrideAttributes ?: optimizely . UserAttributes ,
272
+ userId : string ,
273
+ attributes ?: UserAttributes ,
280
274
) : boolean | null {
281
275
if ( ! this . isInitialized ) {
282
276
return null
283
277
}
284
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
285
278
286
279
return this . instance . getFeatureVariableBoolean ( feature , variable , userId , attributes )
287
280
}
288
281
289
282
public getFeatureVariableInteger (
290
283
feature : string ,
291
284
variable : string ,
292
- overrideUserId ? : string ,
293
- overrideAttributes ?: optimizely . UserAttributes ,
285
+ userId : string ,
286
+ attributes ?: UserAttributes ,
294
287
) : number | null {
295
288
if ( ! this . isInitialized ) {
296
289
return null
297
290
}
298
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
299
291
300
292
return this . instance . getFeatureVariableInteger ( feature , variable , userId , attributes )
301
293
}
302
294
303
295
public getFeatureVariableDouble (
304
296
feature : string ,
305
297
variable : string ,
306
- overrideUserId ? : string ,
307
- overrideAttributes ?: optimizely . UserAttributes ,
298
+ userId : string ,
299
+ attributes ?: UserAttributes ,
308
300
) : number | null {
309
301
if ( ! this . isInitialized ) {
310
302
return null
311
303
}
312
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
313
-
314
304
return this . instance . getFeatureVariableDouble ( feature , variable , userId , attributes )
315
305
}
316
306
317
307
/**
318
308
* Get an array of all enabled features
319
309
*
320
- * @param {string } [overrideUserId ]
321
- * @param {optimizely. UserAttributes } [overrideAttributes ]
310
+ * @param {string } [userId ]
311
+ * @param {UserAttributes } [attributes ]
322
312
* @returns {Array<string> }
323
313
* @memberof OptimizelySDKWrapper
324
314
*/
325
- public getEnabledFeatures ( overrideUserId ? : string , overrideAttributes ?: optimizely . UserAttributes ) : Array < string > {
315
+ public getEnabledFeatures ( userId : string , attributes ?: UserAttributes ) : Array < string > {
326
316
if ( ! this . isInitialized ) {
327
317
return [ ]
328
318
}
329
- const [ userId , attributes ] = this . getUserIdAndAttributes ( overrideUserId , overrideAttributes )
330
319
return this . instance . getEnabledFeatures ( userId , attributes )
331
320
}
332
321
333
322
/**
334
323
* @param {string } experiment
335
- * @param {string } [overrideUserId ]
324
+ * @param {string } [userId ]
336
325
* @returns {(string | null) }
337
326
* @memberof OptimizelySDKWrapper
338
327
*/
339
- public getForcedVariation ( experiment : string , overrideUserId ?: string ) : string | null {
340
- const [ userId ] = this . getUserIdAndAttributes ( overrideUserId )
328
+ public getForcedVariation ( experiment : string , userId : string ) : string | null {
341
329
return this . instance . getForcedVariation ( experiment , userId )
342
330
}
343
331
344
332
/**
345
333
* @param {string } experiment
346
- * @param {string } overrideUserIdOrVariationKey
334
+ * @param {string } userId
347
335
* @param {string } [variationKey]
348
336
* @returns {boolean }
349
337
* @memberof OptimizelySDKWrapper
350
338
*/
351
- public setForcedVariation ( experiment : string , overrideUserIdOrVariationKey : string , variationKey ?: string ) : boolean {
352
- if ( typeof variationKey === 'undefined' ) {
353
- const [ userId ] = this . getUserIdAndAttributes ( )
354
- return this . instance . setForcedVariation ( experiment , userId , overrideUserIdOrVariationKey )
355
- }
356
-
357
- const [ userId ] = this . getUserIdAndAttributes ( overrideUserIdOrVariationKey )
358
-
339
+ public setForcedVariation ( experiment : string , userId : string , variationKey : string ) : boolean {
359
340
return this . instance . setForcedVariation ( experiment , userId , variationKey )
360
341
}
361
342
362
- protected getUserIdAndAttributes (
363
- overrideUserId ?: string ,
364
- overrideAttributes ?: optimizely . UserAttributes ,
365
- ) : [ string , optimizely . UserAttributes ] {
366
- let userId
367
- if ( overrideUserId ) {
368
- userId = overrideUserId
369
- } else if ( this . userId ) {
370
- userId = this . userId
371
- }
372
-
373
- if ( ! userId ) {
374
- // TODO make this a warning
375
- throw new Error ( 'No userId supplied' )
376
- }
377
-
378
- // TODO store this as state when onInitialized is called
379
- let attributes = this . attributes
380
- if ( overrideAttributes ) {
381
- // should we override or merge attributes here
382
- attributes = overrideAttributes
383
- }
384
-
385
- return [ userId , attributes ]
386
- }
387
-
388
343
protected getVariableDefsForFeature ( feature : string ) : VariableDef [ ] | null {
389
344
if ( ! this . datafile ) {
390
345
return null
@@ -405,8 +360,8 @@ export class OptimizelySDKWrapper {
405
360
}
406
361
}
407
362
408
- private setupAttributesLoader ( config : OptimizelySDKWrapperConfig ) : ResourceLoader < optimizely . UserAttributes > {
409
- let attributesLoader : ResourceLoader < optimizely . UserAttributes >
363
+ private setupAttributesLoader ( config : OptimizelySDKWrapperConfig ) : ResourceLoader < UserAttributes > {
364
+ let attributesLoader : ResourceLoader < UserAttributes >
410
365
411
366
if ( config . UNSTABLE_attributesLoader ) {
412
367
attributesLoader = config . UNSTABLE_attributesLoader
0 commit comments