@@ -117,9 +117,22 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
117
117
return activate (experimentKey , userId , Collections .<String , String >emptyMap ());
118
118
}
119
119
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
+
120
126
public @ Nullable Variation activate (@ Nonnull String experimentKey ,
121
127
@ Nonnull String userId ,
122
128
@ 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 {
123
136
124
137
if (!validateUserId (userId )) {
125
138
logger .info ("Not activating user for experiment \" {}\" ." , experimentKey );
@@ -135,27 +148,42 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
135
148
return null ;
136
149
}
137
150
138
- return activate (currentConfig , experiment , userId , attributes );
151
+ return activate (currentConfig , experiment , userId , attributes , sessionId );
139
152
}
140
153
141
154
public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
142
155
@ Nonnull String userId ) {
143
156
return activate (experiment , userId , Collections .<String , String >emptyMap ());
144
157
}
145
158
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
+
146
165
public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
147
166
@ Nonnull String userId ,
148
167
@ Nonnull Map <String , String > attributes ) {
149
168
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
+
150
177
ProjectConfig currentConfig = getProjectConfig ();
151
178
152
- return activate (currentConfig , experiment , userId , attributes );
179
+ return activate (currentConfig , experiment , userId , attributes , sessionId );
153
180
}
154
181
155
182
private @ Nullable Variation activate (@ Nonnull ProjectConfig projectConfig ,
156
183
@ Nonnull Experiment experiment ,
157
184
@ Nonnull String userId ,
158
- @ Nonnull Map <String , String > attributes ) {
185
+ @ Nonnull Map <String , String > attributes ,
186
+ @ CheckForNull String sessionId ) {
159
187
// determine whether all the given attributes are present in the project config. If not, filter out the unknown
160
188
// attributes.
161
189
attributes = filterAttributes (projectConfig , attributes );
@@ -172,18 +200,23 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
172
200
return null ;
173
201
}
174
202
175
- LogEvent impressionEvent =
176
- eventBuilder .createImpressionEvent (projectConfig , experiment , variation , userId , attributes );
177
- logger .info ("Activating user \" {}\" in experiment \" {}\" ." , userId , experiment .getKey ());
178
- logger .debug ("Dispatching impression event to URL {} with params {} and payload \" {}\" ." ,
179
- impressionEvent .getEndpointUrl (), impressionEvent .getRequestParams (), impressionEvent .getBody ());
180
- try {
181
- eventHandler .dispatchEvent (impressionEvent );
182
- } catch (Exception e ) {
183
- logger .error ("Unexpected exception in event dispatcher" , e );
184
- }
203
+ if (experiment .isRunning ()) {
204
+ LogEvent impressionEvent = eventBuilder .createImpressionEvent (projectConfig , experiment , variation , userId ,
205
+ attributes , sessionId );
206
+ logger .info ("Activating user \" {}\" in experiment \" {}\" ." , userId , experiment .getKey ());
207
+ logger .debug (
208
+ "Dispatching impression event to URL {} with params {} and payload \" {}\" ." ,
209
+ impressionEvent .getEndpointUrl (), impressionEvent .getRequestParams (), impressionEvent .getBody ());
210
+ try {
211
+ eventHandler .dispatchEvent (impressionEvent );
212
+ } catch (Exception e ) {
213
+ logger .error ("Unexpected exception in event dispatcher" , e );
214
+ }
185
215
186
- notificationBroadcaster .broadcastExperimentActivated (experiment , userId , attributes , variation );
216
+ notificationBroadcaster .broadcastExperimentActivated (experiment , userId , attributes , variation );
217
+ } else {
218
+ logger .info ("Experiment has \" Launched\" status so not dispatching event during activation." );
219
+ }
187
220
188
221
return variation ;
189
222
}
@@ -192,13 +225,26 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
192
225
193
226
public void track (@ Nonnull String eventName ,
194
227
@ Nonnull String userId ) throws UnknownEventTypeException {
195
- track (eventName , userId , Collections .<String , String >emptyMap (), null );
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 );
196
235
}
197
236
198
237
public void track (@ Nonnull String eventName ,
199
238
@ Nonnull String userId ,
200
239
@ Nonnull Map <String , String > attributes ) throws UnknownEventTypeException {
201
- track (eventName , userId , attributes , null );
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 );
202
248
}
203
249
204
250
public void track (@ Nonnull String eventName ,
@@ -207,17 +253,33 @@ public void track(@Nonnull String eventName,
207
253
track (eventName , userId , Collections .<String , String >emptyMap (), eventValue );
208
254
}
209
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 );
261
+ }
262
+
210
263
public void track (@ Nonnull String eventName ,
211
264
@ Nonnull String userId ,
212
265
@ Nonnull Map <String , String > attributes ,
213
266
long eventValue ) throws UnknownEventTypeException {
214
- track (eventName , userId , attributes , (Long )eventValue );
267
+ track (eventName , userId , attributes , (Long )eventValue , null );
268
+ }
269
+
270
+ 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 );
215
276
}
216
277
217
278
private void track (@ Nonnull String eventName ,
218
279
@ Nonnull String userId ,
219
280
@ Nonnull Map <String , String > attributes ,
220
- @ CheckForNull Long eventValue ) throws UnknownEventTypeException {
281
+ @ CheckForNull Long eventValue ,
282
+ @ CheckForNull String sessionId ) throws UnknownEventTypeException {
221
283
222
284
ProjectConfig currentConfig = getProjectConfig ();
223
285
@@ -233,16 +295,9 @@ private void track(@Nonnull String eventName,
233
295
attributes = filterAttributes (currentConfig , attributes );
234
296
235
297
// create the conversion event request parameters, then dispatch
236
- LogEvent conversionEvent ;
237
- if (eventValue == null ) {
238
- conversionEvent = eventBuilder .createConversionEvent (currentConfig , bucketer , userId ,
239
- eventType .getId (), eventType .getKey (),
240
- attributes );
241
- } else {
242
- conversionEvent = eventBuilder .createConversionEvent (currentConfig , bucketer , userId ,
243
- eventType .getId (), eventType .getKey (), attributes ,
244
- eventValue );
245
- }
298
+ LogEvent conversionEvent = eventBuilder .createConversionEvent (currentConfig , bucketer , userId ,
299
+ eventType .getId (), eventType .getKey (), attributes ,
300
+ eventValue , sessionId );
246
301
247
302
if (conversionEvent == null ) {
248
303
logger .info ("There are no valid experiments for event \" {}\" to track." , eventName );
@@ -260,7 +315,7 @@ private void track(@Nonnull String eventName,
260
315
}
261
316
262
317
notificationBroadcaster .broadcastEventTracked (eventName , userId , attributes , eventValue ,
263
- conversionEvent );
318
+ conversionEvent );
264
319
}
265
320
266
321
//======== live variable getters ========//
0 commit comments