optimizely-sdk 5.0.0.pre.beta → 5.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +202 -202
  3. data/lib/optimizely/audience.rb +127 -127
  4. data/lib/optimizely/bucketer.rb +156 -156
  5. data/lib/optimizely/condition_tree_evaluator.rb +123 -123
  6. data/lib/optimizely/config/datafile_project_config.rb +48 -32
  7. data/lib/optimizely/config/proxy_config.rb +34 -34
  8. data/lib/optimizely/config_manager/async_scheduler.rb +95 -95
  9. data/lib/optimizely/config_manager/http_project_config_manager.rb +340 -338
  10. data/lib/optimizely/config_manager/project_config_manager.rb +25 -25
  11. data/lib/optimizely/config_manager/static_project_config_manager.rb +55 -54
  12. data/lib/optimizely/decide/optimizely_decide_option.rb +28 -28
  13. data/lib/optimizely/decide/optimizely_decision.rb +60 -60
  14. data/lib/optimizely/decide/optimizely_decision_message.rb +26 -26
  15. data/lib/optimizely/decision_service.rb +563 -563
  16. data/lib/optimizely/error_handler.rb +39 -39
  17. data/lib/optimizely/event/batch_event_processor.rb +235 -235
  18. data/lib/optimizely/event/entity/conversion_event.rb +44 -44
  19. data/lib/optimizely/event/entity/decision.rb +38 -38
  20. data/lib/optimizely/event/entity/event_batch.rb +86 -86
  21. data/lib/optimizely/event/entity/event_context.rb +50 -50
  22. data/lib/optimizely/event/entity/impression_event.rb +48 -48
  23. data/lib/optimizely/event/entity/snapshot.rb +33 -33
  24. data/lib/optimizely/event/entity/snapshot_event.rb +48 -48
  25. data/lib/optimizely/event/entity/user_event.rb +22 -22
  26. data/lib/optimizely/event/entity/visitor.rb +36 -36
  27. data/lib/optimizely/event/entity/visitor_attribute.rb +38 -38
  28. data/lib/optimizely/event/event_factory.rb +156 -156
  29. data/lib/optimizely/event/event_processor.rb +25 -25
  30. data/lib/optimizely/event/forwarding_event_processor.rb +44 -44
  31. data/lib/optimizely/event/user_event_factory.rb +88 -88
  32. data/lib/optimizely/event_builder.rb +221 -221
  33. data/lib/optimizely/event_dispatcher.rb +69 -69
  34. data/lib/optimizely/exceptions.rb +193 -149
  35. data/lib/optimizely/helpers/constants.rb +459 -459
  36. data/lib/optimizely/helpers/date_time_utils.rb +30 -30
  37. data/lib/optimizely/helpers/event_tag_utils.rb +132 -132
  38. data/lib/optimizely/helpers/group.rb +31 -31
  39. data/lib/optimizely/helpers/http_utils.rb +68 -68
  40. data/lib/optimizely/helpers/sdk_settings.rb +61 -61
  41. data/lib/optimizely/helpers/validator.rb +236 -238
  42. data/lib/optimizely/helpers/variable_type.rb +67 -67
  43. data/lib/optimizely/logger.rb +46 -46
  44. data/lib/optimizely/notification_center.rb +174 -174
  45. data/lib/optimizely/notification_center_registry.rb +71 -71
  46. data/lib/optimizely/odp/lru_cache.rb +114 -114
  47. data/lib/optimizely/odp/odp_config.rb +102 -102
  48. data/lib/optimizely/odp/odp_event.rb +75 -75
  49. data/lib/optimizely/odp/odp_event_api_manager.rb +70 -70
  50. data/lib/optimizely/odp/odp_event_manager.rb +286 -286
  51. data/lib/optimizely/odp/odp_manager.rb +159 -159
  52. data/lib/optimizely/odp/odp_segment_api_manager.rb +122 -122
  53. data/lib/optimizely/odp/odp_segment_manager.rb +97 -97
  54. data/lib/optimizely/optimizely_config.rb +273 -271
  55. data/lib/optimizely/optimizely_factory.rb +184 -186
  56. data/lib/optimizely/optimizely_user_context.rb +238 -238
  57. data/lib/optimizely/params.rb +31 -31
  58. data/lib/optimizely/project_config.rb +99 -99
  59. data/lib/optimizely/semantic_version.rb +166 -166
  60. data/lib/optimizely/user_condition_evaluator.rb +391 -391
  61. data/lib/optimizely/user_profile_service.rb +35 -35
  62. data/lib/optimizely/version.rb +21 -21
  63. data/lib/optimizely.rb +1262 -1262
  64. metadata +12 -10
@@ -1,88 +1,88 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2019-2020, Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- require_relative 'entity/conversion_event'
19
- require_relative 'entity/impression_event'
20
- require_relative 'entity/event_context'
21
- require_relative 'event_factory'
22
- module Optimizely
23
- class UserEventFactory
24
- # UserEventFactory builds ImpressionEvent and ConversionEvent objects from a given user_event.
25
- def self.create_impression_event(project_config, experiment, variation_id, metadata, user_id, user_attributes)
26
- # Create impression Event to be sent to the logging endpoint.
27
- #
28
- # project_config - Instance of ProjectConfig
29
- # experiment - Instance Experiment for which impression needs to be recorded.
30
- # variation_id - String ID for variation which would be presented to user.
31
- # user_id - String ID for user.
32
- # attributes - Hash Representing user attributes and values which need to be recorded.
33
- #
34
- # Returns Event encapsulating the impression event.
35
- event_context = Optimizely::EventContext.new(
36
- account_id: project_config.account_id,
37
- project_id: project_config.project_id,
38
- anonymize_ip: project_config.anonymize_ip,
39
- revision: project_config.revision,
40
- client_name: CLIENT_ENGINE,
41
- client_version: VERSION
42
- ).as_json
43
-
44
- visitor_attributes = Optimizely::EventFactory.build_attribute_list(user_attributes, project_config)
45
- experiment_layer_id = experiment['layerId']
46
- Optimizely::ImpressionEvent.new(
47
- event_context: event_context,
48
- user_id: user_id,
49
- experiment_layer_id: experiment_layer_id,
50
- experiment_id: experiment['id'],
51
- variation_id: variation_id,
52
- metadata: metadata,
53
- visitor_attributes: visitor_attributes,
54
- bot_filtering: project_config.bot_filtering
55
- )
56
- end
57
-
58
- def self.create_conversion_event(project_config, event, user_id, user_attributes, event_tags)
59
- # Create conversion Event to be sent to the logging endpoint.
60
- #
61
- # project_config - Instance of ProjectConfig
62
- # event - Event which needs to be recorded.
63
- # user_id - String ID for user.
64
- # attributes - Hash Representing user attributes and values which need to be recorded.
65
- # event_tags - Hash representing metadata associated with the event.
66
- #
67
- # Returns Event encapsulating the conversion event.
68
-
69
- event_context = Optimizely::EventContext.new(
70
- account_id: project_config.account_id,
71
- project_id: project_config.project_id,
72
- anonymize_ip: project_config.anonymize_ip,
73
- revision: project_config.revision,
74
- client_name: CLIENT_ENGINE,
75
- client_version: VERSION
76
- ).as_json
77
-
78
- Optimizely::ConversionEvent.new(
79
- event_context: event_context,
80
- event: event,
81
- user_id: user_id,
82
- visitor_attributes: Optimizely::EventFactory.build_attribute_list(user_attributes, project_config),
83
- tags: event_tags,
84
- bot_filtering: project_config.bot_filtering
85
- )
86
- end
87
- end
88
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2019-2020, Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require_relative 'entity/conversion_event'
19
+ require_relative 'entity/impression_event'
20
+ require_relative 'entity/event_context'
21
+ require_relative 'event_factory'
22
+ module Optimizely
23
+ class UserEventFactory
24
+ # UserEventFactory builds ImpressionEvent and ConversionEvent objects from a given user_event.
25
+ def self.create_impression_event(project_config, experiment, variation_id, metadata, user_id, user_attributes)
26
+ # Create impression Event to be sent to the logging endpoint.
27
+ #
28
+ # project_config - Instance of ProjectConfig
29
+ # experiment - Instance Experiment for which impression needs to be recorded.
30
+ # variation_id - String ID for variation which would be presented to user.
31
+ # user_id - String ID for user.
32
+ # attributes - Hash Representing user attributes and values which need to be recorded.
33
+ #
34
+ # Returns Event encapsulating the impression event.
35
+ event_context = Optimizely::EventContext.new(
36
+ account_id: project_config.account_id,
37
+ project_id: project_config.project_id,
38
+ anonymize_ip: project_config.anonymize_ip,
39
+ revision: project_config.revision,
40
+ client_name: CLIENT_ENGINE,
41
+ client_version: VERSION
42
+ ).as_json
43
+
44
+ visitor_attributes = Optimizely::EventFactory.build_attribute_list(user_attributes, project_config)
45
+ experiment_layer_id = experiment['layerId']
46
+ Optimizely::ImpressionEvent.new(
47
+ event_context: event_context,
48
+ user_id: user_id,
49
+ experiment_layer_id: experiment_layer_id,
50
+ experiment_id: experiment['id'],
51
+ variation_id: variation_id,
52
+ metadata: metadata,
53
+ visitor_attributes: visitor_attributes,
54
+ bot_filtering: project_config.bot_filtering
55
+ )
56
+ end
57
+
58
+ def self.create_conversion_event(project_config, event, user_id, user_attributes, event_tags)
59
+ # Create conversion Event to be sent to the logging endpoint.
60
+ #
61
+ # project_config - Instance of ProjectConfig
62
+ # event - Event which needs to be recorded.
63
+ # user_id - String ID for user.
64
+ # attributes - Hash Representing user attributes and values which need to be recorded.
65
+ # event_tags - Hash representing metadata associated with the event.
66
+ #
67
+ # Returns Event encapsulating the conversion event.
68
+
69
+ event_context = Optimizely::EventContext.new(
70
+ account_id: project_config.account_id,
71
+ project_id: project_config.project_id,
72
+ anonymize_ip: project_config.anonymize_ip,
73
+ revision: project_config.revision,
74
+ client_name: CLIENT_ENGINE,
75
+ client_version: VERSION
76
+ ).as_json
77
+
78
+ Optimizely::ConversionEvent.new(
79
+ event_context: event_context,
80
+ event: event,
81
+ user_id: user_id,
82
+ visitor_attributes: Optimizely::EventFactory.build_attribute_list(user_attributes, project_config),
83
+ tags: event_tags,
84
+ bot_filtering: project_config.bot_filtering
85
+ )
86
+ end
87
+ end
88
+ end
@@ -1,221 +1,221 @@
1
- # frozen_string_literal: true
2
-
3
- #
4
- # Copyright 2016-2019, 2022, Optimizely and contributors
5
- #
6
- # Licensed under the Apache License, Version 2.0 (the "License");
7
- # you may not use this file except in compliance with the License.
8
- # You may obtain a copy of the License at
9
- #
10
- # http://www.apache.org/licenses/LICENSE-2.0
11
- #
12
- # Unless required by applicable law or agreed to in writing, software
13
- # distributed under the License is distributed on an "AS IS" BASIS,
14
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
- # See the License for the specific language governing permissions and
16
- # limitations under the License.
17
- #
18
- require_relative 'audience'
19
- require_relative 'helpers/constants'
20
- require_relative 'helpers/event_tag_utils'
21
- require_relative 'params'
22
- require_relative 'version'
23
-
24
- require 'securerandom'
25
-
26
- module Optimizely
27
- class Event
28
- # Representation of an event which can be sent to the Optimizely logging endpoint.
29
-
30
- attr_reader :http_verb, :params, :url, :headers
31
-
32
- def initialize(http_verb, url, params, headers)
33
- @http_verb = http_verb
34
- @url = url
35
- @params = params
36
- @headers = headers
37
- end
38
-
39
- # Override equality operator to make two events with the same contents equal for testing purposes
40
- def ==(other)
41
- @http_verb == other.http_verb && @url == other.url && @params == other.params && @headers == other.headers
42
- end
43
- end
44
-
45
- class BaseEventBuilder
46
- CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom'
47
-
48
- def initialize(logger)
49
- @logger = logger
50
- end
51
-
52
- private
53
-
54
- def get_common_params(project_config, user_id, attributes)
55
- # Get params which are used in both conversion and impression events.
56
- #
57
- # project_config - +Object+ Instance of ProjectConfig
58
- # user_id - +String+ ID for user
59
- # attributes - +Hash+ representing user attributes and values which need to be recorded.
60
- #
61
- # Returns +Hash+ Common event params
62
-
63
- visitor_attributes = []
64
-
65
- attributes&.keys&.each do |attribute_key|
66
- # Omit attribute values that are not supported by the log endpoint.
67
- attribute_value = attributes[attribute_key]
68
- if Helpers::Validator.attribute_valid?(attribute_key, attribute_value)
69
- attribute_id = project_config.get_attribute_id attribute_key
70
- if attribute_id
71
- visitor_attributes.push(
72
- entity_id: attribute_id,
73
- key: attribute_key,
74
- type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
75
- value: attribute_value
76
- )
77
- end
78
- end
79
- end
80
- # Append Bot Filtering Attribute
81
- if project_config.bot_filtering == true || project_config.bot_filtering == false
82
- visitor_attributes.push(
83
- entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
84
- key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
85
- type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
86
- value: project_config.bot_filtering
87
- )
88
- end
89
-
90
- {
91
- account_id: project_config.account_id,
92
- project_id: project_config.project_id,
93
- visitors: [
94
- {
95
- attributes: visitor_attributes,
96
- snapshots: [],
97
- visitor_id: user_id
98
- }
99
- ],
100
- anonymize_ip: project_config.anonymize_ip,
101
- revision: project_config.revision,
102
- client_name: CLIENT_ENGINE,
103
- enrich_decisions: true,
104
- client_version: VERSION
105
- }
106
- end
107
- end
108
-
109
- class EventBuilder < BaseEventBuilder
110
- ENDPOINT = 'https://logx.optimizely.com/v1/events'
111
- POST_HEADERS = {'Content-Type' => 'application/json'}.freeze
112
- ACTIVATE_EVENT_KEY = 'campaign_activated'
113
-
114
- def create_impression_event(project_config, experiment, variation_id, user_id, attributes)
115
- # Create impression Event to be sent to the logging endpoint.
116
- #
117
- # project_config - +Object+ Instance of ProjectConfig
118
- # experiment - +Object+ Experiment for which impression needs to be recorded.
119
- # variation_id - +String+ ID for variation which would be presented to user.
120
- # user_id - +String+ ID for user.
121
- # attributes - +Hash+ representing user attributes and values which need to be recorded.
122
- #
123
- # Returns +Event+ encapsulating the impression event.
124
-
125
- event_params = get_common_params(project_config, user_id, attributes)
126
- impression_params = get_impression_params(project_config, experiment, variation_id)
127
- event_params[:visitors][0][:snapshots].push(impression_params)
128
-
129
- Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
130
- end
131
-
132
- def create_conversion_event(project_config, event, user_id, attributes, event_tags)
133
- # Create conversion Event to be sent to the logging endpoint.
134
- #
135
- # project_config - +Object+ Instance of ProjectConfig
136
- # event - +Object+ Event which needs to be recorded.
137
- # user_id - +String+ ID for user.
138
- # attributes - +Hash+ representing user attributes and values which need to be recorded.
139
- # event_tags - +Hash+ representing metadata associated with the event.
140
- #
141
- # Returns +Event+ encapsulating the conversion event.
142
-
143
- event_params = get_common_params(project_config, user_id, attributes)
144
- conversion_params = get_conversion_params(event, event_tags)
145
- event_params[:visitors][0][:snapshots] = [conversion_params]
146
-
147
- Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
148
- end
149
-
150
- private
151
-
152
- def get_impression_params(project_config, experiment, variation_id)
153
- # Creates object of params specific to impression events
154
- #
155
- # project_config - +Object+ Instance of ProjectConfig
156
- # experiment - +Hash+ experiment for which impression needs to be recorded
157
- # variation_id - +string+ ID for variation which would be presented to user
158
- #
159
- # Returns +Hash+ Impression event params
160
-
161
- experiment_key = experiment['key']
162
- experiment_id = experiment['id']
163
-
164
- {
165
- decisions: [{
166
- campaign_id: project_config.experiment_key_map[experiment_key]['layerId'],
167
- experiment_id: experiment_id,
168
- variation_id: variation_id
169
- }],
170
- events: [{
171
- entity_id: project_config.experiment_key_map[experiment_key]['layerId'],
172
- timestamp: create_timestamp,
173
- key: ACTIVATE_EVENT_KEY,
174
- uuid: create_uuid
175
- }]
176
- }
177
- end
178
-
179
- def get_conversion_params(event, event_tags)
180
- # Creates object of params specific to conversion events
181
- #
182
- # event - +Object+ Event which needs to be recorded.
183
- # event_tags - +Hash+ Values associated with the event.
184
- #
185
- # Returns +Hash+ Conversion event params
186
-
187
- single_snapshot = {}
188
- event_object = {
189
- entity_id: event['id'],
190
- timestamp: create_timestamp,
191
- uuid: create_uuid,
192
- key: event['key']
193
- }
194
-
195
- if event_tags
196
- revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
197
- event_object[:revenue] = revenue_value if revenue_value
198
-
199
- numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
200
- event_object[:value] = numeric_value if numeric_value
201
-
202
- event_object[:tags] = event_tags unless event_tags.empty?
203
- end
204
-
205
- single_snapshot[:events] = [event_object]
206
- single_snapshot
207
- end
208
-
209
- def create_timestamp
210
- # Returns +Integer+ Current timestamp
211
-
212
- (Time.now.to_f * 1000).to_i
213
- end
214
-
215
- def create_uuid
216
- # Returns +String+ Random UUID
217
-
218
- SecureRandom.uuid
219
- end
220
- end
221
- end
1
+ # frozen_string_literal: true
2
+
3
+ #
4
+ # Copyright 2016-2019, 2022-2023, Optimizely and contributors
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+ require_relative 'audience'
19
+ require_relative 'helpers/constants'
20
+ require_relative 'helpers/event_tag_utils'
21
+ require_relative 'params'
22
+ require_relative 'version'
23
+
24
+ require 'securerandom'
25
+
26
+ module Optimizely
27
+ class Event
28
+ # Representation of an event which can be sent to the Optimizely logging endpoint.
29
+
30
+ attr_reader :http_verb, :params, :url, :headers
31
+
32
+ def initialize(http_verb, url, params, headers)
33
+ @http_verb = http_verb
34
+ @url = url
35
+ @params = params
36
+ @headers = headers
37
+ end
38
+
39
+ # Override equality operator to make two events with the same contents equal for testing purposes
40
+ def ==(other)
41
+ @http_verb == other.http_verb && @url == other.url && @params == other.params && @headers == other.headers
42
+ end
43
+ end
44
+
45
+ class BaseEventBuilder
46
+ CUSTOM_ATTRIBUTE_FEATURE_TYPE = 'custom'
47
+
48
+ def initialize(logger)
49
+ @logger = logger
50
+ end
51
+
52
+ private
53
+
54
+ def get_common_params(project_config, user_id, attributes)
55
+ # Get params which are used in both conversion and impression events.
56
+ #
57
+ # project_config - +Object+ Instance of ProjectConfig
58
+ # user_id - +String+ ID for user
59
+ # attributes - +Hash+ representing user attributes and values which need to be recorded.
60
+ #
61
+ # Returns +Hash+ Common event params
62
+
63
+ visitor_attributes = []
64
+
65
+ attributes&.each_key do |attribute_key|
66
+ # Omit attribute values that are not supported by the log endpoint.
67
+ attribute_value = attributes[attribute_key]
68
+ next unless Helpers::Validator.attribute_valid?(attribute_key, attribute_value)
69
+
70
+ attribute_id = project_config.get_attribute_id attribute_key
71
+ next unless attribute_id
72
+
73
+ visitor_attributes.push(
74
+ entity_id: attribute_id,
75
+ key: attribute_key,
76
+ type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
77
+ value: attribute_value
78
+ )
79
+ end
80
+ # Append Bot Filtering Attribute
81
+ if project_config.bot_filtering == true || project_config.bot_filtering == false
82
+ visitor_attributes.push(
83
+ entity_id: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
84
+ key: Optimizely::Helpers::Constants::CONTROL_ATTRIBUTES['BOT_FILTERING'],
85
+ type: CUSTOM_ATTRIBUTE_FEATURE_TYPE,
86
+ value: project_config.bot_filtering
87
+ )
88
+ end
89
+
90
+ {
91
+ account_id: project_config.account_id,
92
+ project_id: project_config.project_id,
93
+ visitors: [
94
+ {
95
+ attributes: visitor_attributes,
96
+ snapshots: [],
97
+ visitor_id: user_id
98
+ }
99
+ ],
100
+ anonymize_ip: project_config.anonymize_ip,
101
+ revision: project_config.revision,
102
+ client_name: CLIENT_ENGINE,
103
+ enrich_decisions: true,
104
+ client_version: VERSION
105
+ }
106
+ end
107
+ end
108
+
109
+ class EventBuilder < BaseEventBuilder
110
+ ENDPOINT = 'https://logx.optimizely.com/v1/events'
111
+ POST_HEADERS = {'Content-Type' => 'application/json'}.freeze
112
+ ACTIVATE_EVENT_KEY = 'campaign_activated'
113
+
114
+ def create_impression_event(project_config, experiment, variation_id, user_id, attributes)
115
+ # Create impression Event to be sent to the logging endpoint.
116
+ #
117
+ # project_config - +Object+ Instance of ProjectConfig
118
+ # experiment - +Object+ Experiment for which impression needs to be recorded.
119
+ # variation_id - +String+ ID for variation which would be presented to user.
120
+ # user_id - +String+ ID for user.
121
+ # attributes - +Hash+ representing user attributes and values which need to be recorded.
122
+ #
123
+ # Returns +Event+ encapsulating the impression event.
124
+
125
+ event_params = get_common_params(project_config, user_id, attributes)
126
+ impression_params = get_impression_params(project_config, experiment, variation_id)
127
+ event_params[:visitors][0][:snapshots].push(impression_params)
128
+
129
+ Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
130
+ end
131
+
132
+ def create_conversion_event(project_config, event, user_id, attributes, event_tags)
133
+ # Create conversion Event to be sent to the logging endpoint.
134
+ #
135
+ # project_config - +Object+ Instance of ProjectConfig
136
+ # event - +Object+ Event which needs to be recorded.
137
+ # user_id - +String+ ID for user.
138
+ # attributes - +Hash+ representing user attributes and values which need to be recorded.
139
+ # event_tags - +Hash+ representing metadata associated with the event.
140
+ #
141
+ # Returns +Event+ encapsulating the conversion event.
142
+
143
+ event_params = get_common_params(project_config, user_id, attributes)
144
+ conversion_params = get_conversion_params(event, event_tags)
145
+ event_params[:visitors][0][:snapshots] = [conversion_params]
146
+
147
+ Event.new(:post, ENDPOINT, event_params, POST_HEADERS)
148
+ end
149
+
150
+ private
151
+
152
+ def get_impression_params(project_config, experiment, variation_id)
153
+ # Creates object of params specific to impression events
154
+ #
155
+ # project_config - +Object+ Instance of ProjectConfig
156
+ # experiment - +Hash+ experiment for which impression needs to be recorded
157
+ # variation_id - +string+ ID for variation which would be presented to user
158
+ #
159
+ # Returns +Hash+ Impression event params
160
+
161
+ experiment_key = experiment['key']
162
+ experiment_id = experiment['id']
163
+
164
+ {
165
+ decisions: [{
166
+ campaign_id: project_config.experiment_key_map[experiment_key]['layerId'],
167
+ experiment_id: experiment_id,
168
+ variation_id: variation_id
169
+ }],
170
+ events: [{
171
+ entity_id: project_config.experiment_key_map[experiment_key]['layerId'],
172
+ timestamp: create_timestamp,
173
+ key: ACTIVATE_EVENT_KEY,
174
+ uuid: create_uuid
175
+ }]
176
+ }
177
+ end
178
+
179
+ def get_conversion_params(event, event_tags)
180
+ # Creates object of params specific to conversion events
181
+ #
182
+ # event - +Object+ Event which needs to be recorded.
183
+ # event_tags - +Hash+ Values associated with the event.
184
+ #
185
+ # Returns +Hash+ Conversion event params
186
+
187
+ single_snapshot = {}
188
+ event_object = {
189
+ entity_id: event['id'],
190
+ timestamp: create_timestamp,
191
+ uuid: create_uuid,
192
+ key: event['key']
193
+ }
194
+
195
+ if event_tags
196
+ revenue_value = Helpers::EventTagUtils.get_revenue_value(event_tags, @logger)
197
+ event_object[:revenue] = revenue_value if revenue_value
198
+
199
+ numeric_value = Helpers::EventTagUtils.get_numeric_value(event_tags, @logger)
200
+ event_object[:value] = numeric_value if numeric_value
201
+
202
+ event_object[:tags] = event_tags unless event_tags.empty?
203
+ end
204
+
205
+ single_snapshot[:events] = [event_object]
206
+ single_snapshot
207
+ end
208
+
209
+ def create_timestamp
210
+ # Returns +Integer+ Current timestamp
211
+
212
+ (Time.now.to_f * 1000).to_i
213
+ end
214
+
215
+ def create_uuid
216
+ # Returns +String+ Random UUID
217
+
218
+ SecureRandom.uuid
219
+ end
220
+ end
221
+ end