@@ -117,9 +117,22 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
117117 return activate (experimentKey , userId , Collections .<String , String >emptyMap ());
118118 }
119119
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+
120126 public @ Nullable Variation activate (@ Nonnull String experimentKey ,
121127 @ Nonnull String userId ,
122128 @ 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 {
123136
124137 if (!validateUserId (userId )) {
125138 logger .info ("Not activating user for experiment \" {}\" ." , experimentKey );
@@ -135,27 +148,42 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
135148 return null ;
136149 }
137150
138- return activate (currentConfig , experiment , userId , attributes );
151+ return activate (currentConfig , experiment , userId , attributes , sessionId );
139152 }
140153
141154 public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
142155 @ Nonnull String userId ) {
143156 return activate (experiment , userId , Collections .<String , String >emptyMap ());
144157 }
145158
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+
146165 public @ Nullable Variation activate (@ Nonnull Experiment experiment ,
147166 @ Nonnull String userId ,
148167 @ Nonnull Map <String , String > attributes ) {
149168
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+
150177 ProjectConfig currentConfig = getProjectConfig ();
151178
152- return activate (currentConfig , experiment , userId , attributes );
179+ return activate (currentConfig , experiment , userId , attributes , sessionId );
153180 }
154181
155182 private @ Nullable Variation activate (@ Nonnull ProjectConfig projectConfig ,
156183 @ Nonnull Experiment experiment ,
157184 @ Nonnull String userId ,
158- @ Nonnull Map <String , String > attributes ) {
185+ @ Nonnull Map <String , String > attributes ,
186+ @ CheckForNull String sessionId ) {
159187 // determine whether all the given attributes are present in the project config. If not, filter out the unknown
160188 // attributes.
161189 attributes = filterAttributes (projectConfig , attributes );
@@ -172,18 +200,23 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
172200 return null ;
173201 }
174202
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+ }
185215
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+ }
187220
188221 return variation ;
189222 }
@@ -192,13 +225,26 @@ private Optimizely(@Nonnull ProjectConfig projectConfig,
192225
193226 public void track (@ Nonnull String eventName ,
194227 @ 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 );
196235 }
197236
198237 public void track (@ Nonnull String eventName ,
199238 @ Nonnull String userId ,
200239 @ 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 );
202248 }
203249
204250 public void track (@ Nonnull String eventName ,
@@ -207,17 +253,33 @@ public void track(@Nonnull String eventName,
207253 track (eventName , userId , Collections .<String , String >emptyMap (), eventValue );
208254 }
209255
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+
210263 public void track (@ Nonnull String eventName ,
211264 @ Nonnull String userId ,
212265 @ Nonnull Map <String , String > attributes ,
213266 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 );
215276 }
216277
217278 private void track (@ Nonnull String eventName ,
218279 @ Nonnull String userId ,
219280 @ Nonnull Map <String , String > attributes ,
220- @ CheckForNull Long eventValue ) throws UnknownEventTypeException {
281+ @ CheckForNull Long eventValue ,
282+ @ CheckForNull String sessionId ) throws UnknownEventTypeException {
221283
222284 ProjectConfig currentConfig = getProjectConfig ();
223285
@@ -233,16 +295,9 @@ private void track(@Nonnull String eventName,
233295 attributes = filterAttributes (currentConfig , attributes );
234296
235297 // 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 );
246301
247302 if (conversionEvent == null ) {
248303 logger .info ("There are no valid experiments for event \" {}\" to track." , eventName );
@@ -260,7 +315,7 @@ private void track(@Nonnull String eventName,
260315 }
261316
262317 notificationBroadcaster .broadcastEventTracked (eventName , userId , attributes , eventValue ,
263- conversionEvent );
318+ conversionEvent );
264319 }
265320
266321 //======== live variable getters ========//
0 commit comments