@@ -234,162 +234,4 @@ def add_time
234
234
@params [ 'timestamp' ] = ( Time . now . to_f * 1000 ) . to_i
235
235
end
236
236
end
237
-
238
- class EventBuilderV1 < BaseEventBuilder
239
- # Class which encapsulates methods to build events for tracking impressions and conversions.
240
-
241
- # Attribute mapping format
242
- ATTRIBUTE_PARAM_FORMAT = '%{segment_prefix}%{segment_id}'
243
-
244
- # Experiment mapping format
245
- EXPERIMENT_PARAM_FORMAT = '%{experiment_prefix}%{experiment_id}'
246
-
247
- # Event endpoint path
248
- OFFLINE_API_PATH = 'https://%{project_id}.log.optimizely.com/event'
249
-
250
- def create_impression_event ( experiment_key , variation_id , user_id , attributes )
251
- # Create conversion Event to be sent to the logging endpoint.
252
- #
253
- # experiment_key - Experiment for which impression needs to be recorded.
254
- # variation_id - ID for variation which would be presented to user.
255
- # user_id - ID for user.
256
- # attributes - Hash representing user attributes and values which need to be recorded.
257
- #
258
- # Returns event hash encapsulating the impression event.
259
-
260
- @params = { }
261
- add_common_params ( user_id , attributes )
262
- add_impression_goal ( experiment_key )
263
- add_experiment ( experiment_key , variation_id )
264
- Event . new ( :get , sprintf ( OFFLINE_API_PATH , project_id : @params [ Params ::PROJECT_ID ] ) , @params , { } )
265
- end
266
-
267
- def create_conversion_event ( event_key , user_id , attributes , event_tags , experiment_keys )
268
- # Create conversion Event to be sent to the logging endpoint.
269
- #
270
- # event_key - Goal key representing the event which needs to be recorded.
271
- # user_id - ID for user.
272
- # attributes - Hash representing user attributes and values which need to be recorded.
273
- # event_tags - Hash representing metadata associated with the event.
274
- # experiment_keys - Array of valid experiment keys for the goal
275
- #
276
- # Returns event hash encapsulating the conversion event.
277
-
278
- @params = { }
279
-
280
- event_value = Helpers ::EventTagUtils . get_revenue_value ( event_tags )
281
-
282
- add_common_params ( user_id , attributes )
283
- add_conversion_goal ( event_key , event_value )
284
- add_experiment_variation_params ( user_id , experiment_keys )
285
- Event . new ( :get , sprintf ( OFFLINE_API_PATH , project_id : @params [ Params ::PROJECT_ID ] ) , @params , { } )
286
- end
287
-
288
- private
289
-
290
- def add_project_id
291
- # Add project ID to the event.
292
-
293
- @params [ Params ::PROJECT_ID ] = @config . project_id
294
- end
295
-
296
- def add_account_id
297
- # Add account ID to the event.
298
-
299
- @params [ Params ::ACCOUNT_ID ] = @config . account_id
300
- end
301
-
302
- def add_user_id ( user_id )
303
- # Add user ID to the event.
304
-
305
- @params [ Params ::END_USER_ID ] = user_id
306
- end
307
-
308
- def add_attributes ( attributes )
309
- # Add attribute(s) information to the event.
310
- #
311
- # attributes - Hash representing user attributes and values which need to be recorded.
312
-
313
- return if attributes . nil?
314
-
315
- attributes . keys . each do |attribute_key |
316
- attribute_value = attributes [ attribute_key ]
317
- next unless attribute_value
318
-
319
- # Skip attributes not in the datafile
320
- segment_id = @config . get_segment_id ( attribute_key )
321
- next unless segment_id
322
- segment_param = sprintf ( ATTRIBUTE_PARAM_FORMAT ,
323
- segment_prefix : Params ::SEGMENT_PREFIX , segment_id : segment_id )
324
- params [ segment_param ] = attribute_value
325
- end
326
- end
327
-
328
- def add_source
329
- # Add source information to the event.
330
-
331
- @params [ Params ::SOURCE ] = sprintf ( 'ruby-sdk-%{version}' , version : VERSION )
332
- end
333
-
334
- def add_time
335
- # Add time information to the event.
336
-
337
- @params [ Params ::TIME ] = Time . now . strftime ( '%s' ) . to_i
338
- end
339
-
340
- def add_impression_goal ( experiment_key )
341
- # Add impression goal information to the event.
342
- #
343
- # experiment_key - Experiment which is being activated.
344
-
345
- # For tracking impressions, goal ID is set equal to experiment ID of experiment being activated.
346
- @params [ Params ::GOAL_ID ] = @config . get_experiment_id ( experiment_key )
347
- @params [ Params ::GOAL_NAME ] = 'visitor-event'
348
- end
349
-
350
- def add_experiment ( experiment_key , variation_id )
351
- # Add experiment to variation mapping to the impression event.
352
- #
353
- # experiment_key - Experiment which is being activated.
354
- # variation_id - ID for variation which would be presented to user.
355
-
356
- experiment_id = @config . get_experiment_id ( experiment_key )
357
- experiment_param = sprintf ( EXPERIMENT_PARAM_FORMAT ,
358
- experiment_prefix : Params ::EXPERIMENT_PREFIX , experiment_id : experiment_id )
359
- @params [ experiment_param ] = variation_id
360
- end
361
-
362
- def add_experiment_variation_params ( user_id , experiment_keys )
363
- # Maps experiment and corresponding variation as parameters to be used in the event tracking call.
364
- #
365
- # user_id - ID for user.
366
- # experiment_keys - Array of valid experiment keys for the goal
367
-
368
- experiment_keys . each do |experiment_key |
369
- variation_id = @bucketer . bucket ( experiment_key , user_id )
370
- experiment_id = @config . experiment_key_map [ experiment_key ] [ 'id' ]
371
- experiment_param = sprintf ( EXPERIMENT_PARAM_FORMAT ,
372
- experiment_prefix : Params ::EXPERIMENT_PREFIX , experiment_id : experiment_id )
373
- @params [ experiment_param ] = variation_id
374
- end
375
- end
376
-
377
- def add_conversion_goal ( event_key , event_value )
378
- # Add conversion goal information to the event.
379
- #
380
- # event_key - Goal key representing the event which needs to be recorded.
381
- # event_value - Value associated with the event. Can be used to represent revenue in cents.
382
-
383
- goal_id = @config . event_key_map [ event_key ] [ 'id' ]
384
- event_ids = goal_id
385
-
386
- if event_value
387
- event_ids = sprintf ( '%{goal_id},%{revenue_id}' , goal_id : goal_id , revenue_id : @config . get_revenue_goal_id )
388
- @params [ Params ::EVENT_VALUE ] = event_value
389
- end
390
-
391
- @params [ Params ::GOAL_ID ] = event_ids
392
- @params [ Params ::GOAL_NAME ] = event_key
393
- end
394
- end
395
237
end
0 commit comments