1
1
/**
2
2
*
3
- * Copyright 2016, Optimizely and contributors
3
+ * Copyright 2016-2017 , Optimizely and contributors
4
4
*
5
5
* Licensed under the Apache License, Version 2.0 (the "License");
6
6
* you may not use this file except in compliance with the License.
38
38
import com .optimizely .ab .event .internal .EventBuilderV1 ;
39
39
import com .optimizely .ab .event .internal .EventBuilderV2 ;
40
40
import com .optimizely .ab .event .internal .payload .Event .ClientEngine ;
41
+ import com .optimizely .ab .internal .EventTagUtils ;
41
42
import com .optimizely .ab .internal .ProjectValidationUtils ;
43
+ import com .optimizely .ab .internal .ReservedEventKey ;
42
44
import com .optimizely .ab .notification .NotificationListener ;
43
45
import com .optimizely .ab .notification .NotificationBroadcaster ;
44
46
@@ -117,22 +119,9 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
117
119
return activate (experimentKey , userId , Collections .<String , String >emptyMap ());
118
120
}
119
121
120
- public @ Nullable Variation activate (@ Nonnull String experimentKey ,
121
- @ Nonnull String userId ,
122
- @ CheckForNull String sessionId ) throws UnknownExperimentException {
123
- return activate (experimentKey , userId , Collections .<String , String >emptyMap (), sessionId );
124
- }
125
-
126
122
public @ Nullable Variation activate (@ Nonnull String experimentKey ,
127
123
@ Nonnull String userId ,
128
124
@ Nonnull Map <String , String > attributes ) throws UnknownExperimentException {
129
- return activate (experimentKey , userId , attributes , null );
130
- }
131
-
132
- public @ Nullable Variation activate (@ Nonnull String experimentKey ,
133
- @ Nonnull String userId ,
134
- @ Nonnull Map <String , String > attributes ,
135
- @ CheckForNull String sessionId ) throws UnknownExperimentException {
136
125
137
126
if (!validateUserId (userId )) {
138
127
logger .info ("Not activating user for experiment \" {}\" ." , experimentKey );
@@ -148,42 +137,27 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
148
137
return null ;
149
138
}
150
139
151
- return activate (currentConfig , experiment , userId , attributes , sessionId );
140
+ return activate (currentConfig , experiment , userId , attributes );
152
141
}
153
142
154
143
public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
155
144
@ Nonnull String userId ) {
156
145
return activate (experiment , userId , Collections .<String , String >emptyMap ());
157
146
}
158
147
159
- public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
160
- @ Nonnull String userId ,
161
- @ CheckForNull String sessionId ) {
162
- return activate (experiment , userId , Collections .<String , String >emptyMap (), sessionId );
163
- }
164
-
165
148
public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
166
149
@ Nonnull String userId ,
167
150
@ Nonnull Map <String , String > attributes ) {
168
151
169
- return activate (experiment , userId , attributes , null );
170
- }
171
-
172
- public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
173
- @ Nonnull String userId ,
174
- @ Nonnull Map <String , String > attributes ,
175
- @ CheckForNull String sessionId ) {
176
-
177
152
ProjectConfig currentConfig = getProjectConfig ();
178
153
179
- return activate (currentConfig , experiment , userId , attributes , sessionId );
154
+ return activate (currentConfig , experiment , userId , attributes );
180
155
}
181
156
182
157
private @ Nullable Variation activate (@ Nonnull ProjectConfig projectConfig ,
183
158
@ Nonnull Experiment experiment ,
184
159
@ Nonnull String userId ,
185
- @ Nonnull Map <String , String > attributes ,
186
- @ CheckForNull String sessionId ) {
160
+ @ Nonnull Map <String , String > attributes ) {
187
161
// determine whether all the given attributes are present in the project config. If not, filter out the unknown
188
162
// attributes.
189
163
attributes = filterAttributes (projectConfig , attributes );
@@ -202,7 +176,7 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
202
176
203
177
if (experiment .isRunning ()) {
204
178
LogEvent impressionEvent = eventBuilder .createImpressionEvent (projectConfig , experiment , variation , userId ,
205
- attributes , sessionId );
179
+ attributes );
206
180
logger .info ("Activating user \" {}\" in experiment \" {}\" ." , userId , experiment .getKey ());
207
181
logger .debug (
208
182
"Dispatching impression event to URL {} with params {} and payload \" {}\" ." ,
@@ -225,61 +199,39 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
225
199
226
200
public void track (@ Nonnull String eventName ,
227
201
@ Nonnull String userId ) throws UnknownEventTypeException {
228
- track (eventName , userId , Collections .<String , String >emptyMap (), null , null );
229
- }
230
-
231
- public void track (@ Nonnull String eventName ,
232
- @ Nonnull String userId ,
233
- @ CheckForNull String sessionId ) throws UnknownEventTypeException {
234
- track (eventName , userId , Collections .<String , String >emptyMap (), null , sessionId );
202
+ track (eventName , userId , Collections .<String , String >emptyMap (), Collections .<String , Object >emptyMap ());
235
203
}
236
204
237
205
public void track (@ Nonnull String eventName ,
238
206
@ Nonnull String userId ,
239
207
@ Nonnull Map <String , String > attributes ) throws UnknownEventTypeException {
240
- track (eventName , userId , attributes , null , null );
241
- }
242
-
243
- public void track (@ Nonnull String eventName ,
244
- @ Nonnull String userId ,
245
- @ Nonnull Map <String , String > attributes ,
246
- @ CheckForNull String sessionId ) throws UnknownEventTypeException {
247
- track (eventName , userId , attributes , null , sessionId );
208
+ track (eventName , userId , attributes , Collections .<String , String >emptyMap ());
248
209
}
249
210
211
+ /**
212
+ * @deprecated see {@link #track(String, String, Map)} and pass in the revenue value as an event tag instead.
213
+ */
250
214
public void track (@ Nonnull String eventName ,
251
215
@ Nonnull String userId ,
252
216
long eventValue ) throws UnknownEventTypeException {
253
- track (eventName , userId , Collections .<String , String >emptyMap (), eventValue );
254
- }
255
-
256
- public void track (@ Nonnull String eventName ,
257
- @ Nonnull String userId ,
258
- long eventValue ,
259
- @ CheckForNull String sessionId ) throws UnknownEventTypeException {
260
- track (eventName , userId , Collections .<String , String >emptyMap (), eventValue , sessionId );
217
+ track (eventName , userId , Collections .<String , String >emptyMap (), Collections .singletonMap (
218
+ ReservedEventKey .REVENUE .toString (), eventValue ));
261
219
}
262
220
221
+ /**
222
+ * @deprecated see {@link #track(String, String, Map, long)} and pass in the revenue value as an event tag instead.
223
+ */
263
224
public void track (@ Nonnull String eventName ,
264
225
@ Nonnull String userId ,
265
226
@ Nonnull Map <String , String > attributes ,
266
227
long eventValue ) throws UnknownEventTypeException {
267
- track (eventName , userId , attributes , ( Long ) eventValue , null );
228
+ track (eventName , userId , attributes , Collections . singletonMap ( ReservedEventKey . REVENUE . toString (), eventValue ) );
268
229
}
269
230
270
231
public void track (@ Nonnull String eventName ,
271
- @ Nonnull String userId ,
272
- @ Nonnull Map <String , String > attributes ,
273
- long eventValue ,
274
- @ CheckForNull String sessionId ) throws UnknownEventTypeException {
275
- track (eventName , userId , attributes , (Long )eventValue , sessionId );
276
- }
277
-
278
- private void track (@ Nonnull String eventName ,
279
232
@ Nonnull String userId ,
280
233
@ Nonnull Map <String , String > attributes ,
281
- @ CheckForNull Long eventValue ,
282
- @ CheckForNull String sessionId ) throws UnknownEventTypeException {
234
+ @ Nonnull Map <String , ?> eventTags ) throws UnknownEventTypeException {
283
235
284
236
ProjectConfig currentConfig = getProjectConfig ();
285
237
@@ -294,10 +246,18 @@ private void track(@Nonnull String eventName,
294
246
// attributes.
295
247
attributes = filterAttributes (currentConfig , attributes );
296
248
249
+ Long eventValue = null ;
250
+ if (eventTags == null ) {
251
+ logger .warn ("Event tags is null when non-null was expected. Defaulting to an empty event tags map." );
252
+ eventTags = Collections .<String , String >emptyMap ();
253
+ } else {
254
+ eventValue = EventTagUtils .getRevenueValue (eventTags );
255
+ }
256
+
297
257
// create the conversion event request parameters, then dispatch
298
258
LogEvent conversionEvent = eventBuilder .createConversionEvent (currentConfig , bucketer , userId ,
299
259
eventType .getId (), eventType .getKey (), attributes ,
300
- eventValue , sessionId );
260
+ eventTags );
301
261
302
262
if (conversionEvent == null ) {
303
263
logger .info ("There are no valid experiments for event \" {}\" to track." , eventName );
@@ -498,6 +458,13 @@ private void track(@Nonnull String eventName,
498
458
* @return a {@link ProjectConfig} instance given a json string
499
459
*/
500
460
private static ProjectConfig getProjectConfig (String datafile ) throws ConfigParseException {
461
+ if (datafile == null ) {
462
+ throw new ConfigParseException ("Unable to parse null datafile." );
463
+ }
464
+ if (datafile .length () == 0 ) {
465
+ throw new ConfigParseException ("Unable to parse empty datafile." );
466
+ }
467
+
501
468
return DefaultConfigParser .getInstance ().parseProjectConfig (datafile );
502
469
}
503
470
@@ -624,10 +591,16 @@ private LiveVariable getLiveVariableOrThrow(ProjectConfig projectConfig, String
624
591
*
625
592
* @param projectConfig the current project config
626
593
* @param attributes the attributes map to validate and potentially filter
627
- * @return the filtered attributes map (containing only attributes that are present in the project config)
628
- *
594
+ * @return the filtered attributes map (containing only attributes that are present in the project config) or an
595
+ * empty map if a null attributes object is passed in
629
596
*/
630
- private Map <String , String > filterAttributes (ProjectConfig projectConfig , Map <String , String > attributes ) {
597
+ private Map <String , String > filterAttributes (@ Nonnull ProjectConfig projectConfig ,
598
+ @ Nonnull Map <String , String > attributes ) {
599
+ if (attributes == null ) {
600
+ logger .warn ("Attributes is null when non-null was expected. Defaulting to an empty attributes map." );
601
+ return Collections .<String , String >emptyMap ();
602
+ }
603
+
631
604
List <String > unknownAttributes = null ;
632
605
633
606
Map <String , Attribute > attributeKeyMapping = projectConfig .getAttributeKeyMapping ();
0 commit comments