optimizely-sdk 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0245df6d1e29df95c54b1e1ffff564bf8271697a
4
- data.tar.gz: 5d083b0a9b67ae6785bb7c39f52f60eca8c50701
3
+ metadata.gz: 6b62ea44bca6fdbbd5c1086e88505cc27968fdff
4
+ data.tar.gz: 95a11cfbea360da196f6ba1eb9f243a5d8edfbd8
5
5
  SHA512:
6
- metadata.gz: 1ea777db826ad4a880336a6ffc804d016fd8c156107e8405c2598c12f96030bc896463b9b4418ec18332e0bb632f4e81b5c5e632527873cd1e22ed06b8e95f3b
7
- data.tar.gz: a3ac225efa1189c7b2b11b9d25711fd6eec3f7fc2b35ccd3e51795fcbdcc34005842d6ea79a95e93645e1a91317e7f8e0b4cf1e72eea4a84dd4d537bc31ebf09
6
+ metadata.gz: 615a93c3c0c3993d4f719bf97f967b3ac6af4e1cc75e3beb6ab968be6f30d3cbe5844aaaea31b71a78ee3b096564194af6a782d8a671510c965ecc4558caba52
7
+ data.tar.gz: 5df1b06c97e9454e149764ff30db4e78a59c5f9265062ffa337af26da7405fa534f33ae5c8777b4f45536e6ad27045129ce1ac9ac6fb7e055005087e76624dc1
data/lib/optimizely.rb CHANGED
@@ -162,13 +162,13 @@ module Optimizely
162
162
  @config.get_variation_key_from_id(experiment_key, variation_id)
163
163
  end
164
164
 
165
- def track(event_key, user_id, attributes = nil, event_value = nil)
165
+ def track(event_key, user_id, attributes = nil, event_tags = nil)
166
166
  # Send conversion event to Optimizely.
167
167
  #
168
168
  # event_key - Goal key representing the event which needs to be recorded.
169
169
  # user_id - String ID for user.
170
170
  # attributes - Hash representing visitor attributes and values which need to be recorded.
171
- # event_value - Value associated with the event. Can be used to represent revenue in cents.
171
+ # event_tags - Hash representing metadata associated with the event.
172
172
 
173
173
  unless @is_valid
174
174
  logger = SimpleLogger.new
@@ -176,7 +176,15 @@ module Optimizely
176
176
  return nil
177
177
  end
178
178
 
179
+ if event_tags and event_tags.is_a? Numeric
180
+ event_tags = {
181
+ 'revenue' => event_tags
182
+ }
183
+ @logger.log(Logger::WARN, 'Event value is deprecated in track call. Use event tags to pass in revenue value instead.')
184
+ end
185
+
179
186
  return nil if attributes && !attributes_valid?(attributes)
187
+ return nil if event_tags && !event_tags_valid?(event_tags)
180
188
 
181
189
  experiment_ids = @config.get_experiment_ids_for_goal(event_key)
182
190
  if experiment_ids.empty?
@@ -202,7 +210,7 @@ module Optimizely
202
210
  end
203
211
 
204
212
  conversion_event = @event_builder.create_conversion_event(event_key, user_id, attributes,
205
- event_value, valid_experiment_keys)
213
+ event_tags, valid_experiment_keys)
206
214
  @logger.log(Logger::INFO,
207
215
  'Dispatching conversion event to URL %s with params %s.' % [conversion_event.url,
208
216
  conversion_event.params])
@@ -251,6 +259,15 @@ module Optimizely
251
259
  true
252
260
  end
253
261
 
262
+ def event_tags_valid?(event_tags)
263
+ unless Helpers::Validator.event_tags_valid?(event_tags)
264
+ @logger.log(Logger::ERROR, 'Provided event tags are in an invalid format.')
265
+ @error_handler.handle_error(InvalidEventTagFormatError)
266
+ return false
267
+ end
268
+ true
269
+ end
270
+
254
271
  def validate_inputs(datafile, skip_json_validation)
255
272
  unless skip_json_validation
256
273
  raise InvalidInputError.new('datafile') unless Helpers::Validator.datafile_valid?(datafile)
@@ -16,6 +16,7 @@
16
16
  require_relative './audience'
17
17
  require_relative './params'
18
18
  require_relative './version'
19
+ require_relative '../optimizely/helpers/event_tag_utils'
19
20
 
20
21
  module Optimizely
21
22
  class Event
@@ -89,20 +90,21 @@ module Optimizely
89
90
  Event.new(:post, IMPRESSION_EVENT_ENDPOINT, @params, POST_HEADERS)
90
91
  end
91
92
 
92
- def create_conversion_event(event_key, user_id, attributes, event_value, experiment_keys)
93
+ def create_conversion_event(event_key, user_id, attributes, event_tags, experiment_keys)
93
94
  # Create conversion Event to be sent to the logging endpoint.
94
95
  #
95
96
  # event_key - Event key representing the event which needs to be recorded.
96
97
  # user_id - ID for user.
97
98
  # attributes - Hash representing user attributes and values which need to be recorded.
98
- # event_value - Value associated with the event. Can be used to represent revenue in cents.
99
+ # event_tags - Hash representing metadata associated with the event.
99
100
  # experiment_keys - Array of valid experiment keys for the event
100
101
  #
101
102
  # Returns event hash encapsulating the conversion event.
102
103
 
103
104
  @params = {}
104
105
  add_common_params(user_id, attributes)
105
- add_conversion_event(event_key, event_value)
106
+ add_conversion_event(event_key)
107
+ add_event_tags(event_tags)
106
108
  add_layer_states(user_id, experiment_keys)
107
109
  Event.new(:post, CONVERSION_EVENT_ENDPOINT, @params, POST_HEADERS)
108
110
  end
@@ -161,26 +163,47 @@ module Optimizely
161
163
  }
162
164
  end
163
165
 
164
- def add_conversion_event(event_key, event_value)
166
+ def add_event_tags(event_tags)
167
+ @params['eventFeatures'] ||= []
168
+ @params['eventMetrics'] ||= []
169
+
170
+ return if event_tags.nil?
171
+
172
+ event_tags.each_pair do |event_tag_key, event_tag_value|
173
+ next if event_tag_value.nil?
174
+
175
+ event_feature = {
176
+ 'id' => event_tag_key,
177
+ 'type' => 'custom',
178
+ 'value' => event_tag_value,
179
+ 'shouldIndex' => false,
180
+ }
181
+ @params['eventFeatures'].push(event_feature)
182
+
183
+ end
184
+
185
+ event_value = Helpers::EventTagUtils.get_revenue_value(event_tags)
186
+
187
+ if event_value
188
+ event_metric = {
189
+ 'name' => 'revenue',
190
+ 'value' => event_value
191
+ }
192
+ @params['eventMetrics'].push(event_metric)
193
+ end
194
+
195
+ end
196
+
197
+ def add_conversion_event(event_key)
165
198
  # Add conversion event information to the event.
166
199
  #
167
200
  # event_key - Event key representing the event which needs to be recorded.
168
- # event_value - Value associated with the event. Can be used to represent revenue in cents.
169
201
 
170
202
  event_id = @config.event_key_map[event_key]['id']
171
203
  event_name = @config.event_key_map[event_key]['key']
172
204
 
173
205
  @params['eventEntityId'] = event_id
174
- @params['eventFeatures'] = []
175
206
  @params['eventName'] = event_name
176
- @params['eventMetrics'] = []
177
-
178
- if event_value
179
- @params['eventMetrics'].push({
180
- 'name' => 'revenue',
181
- 'value' => event_value,
182
- })
183
- end
184
207
  end
185
208
 
186
209
  def add_layer_states(user_id, experiment_keys)
@@ -241,18 +264,21 @@ module Optimizely
241
264
  Event.new(:get, sprintf(OFFLINE_API_PATH, project_id: @params[Params::PROJECT_ID]), @params, {})
242
265
  end
243
266
 
244
- def create_conversion_event(event_key, user_id, attributes, event_value, experiment_keys)
267
+ def create_conversion_event(event_key, user_id, attributes, event_tags, experiment_keys)
245
268
  # Create conversion Event to be sent to the logging endpoint.
246
269
  #
247
270
  # event_key - Goal key representing the event which needs to be recorded.
248
271
  # user_id - ID for user.
249
272
  # attributes - Hash representing user attributes and values which need to be recorded.
250
- # event_value - Value associated with the event. Can be used to represent revenue in cents.
273
+ # event_tags - Hash representing metadata associated with the event.
251
274
  # experiment_keys - Array of valid experiment keys for the goal
252
275
  #
253
276
  # Returns event hash encapsulating the conversion event.
254
277
 
255
278
  @params = {}
279
+
280
+ event_value = Helpers::EventTagUtils.get_revenue_value(event_tags)
281
+
256
282
  add_common_params(user_id, attributes)
257
283
  add_conversion_goal(event_key, event_value)
258
284
  add_experiment_variation_params(user_id, experiment_keys)
@@ -40,6 +40,14 @@ module Optimizely
40
40
  end
41
41
  end
42
42
 
43
+ class InvalidEventTagFormatError < Error
44
+ # Raised when attributes are provided in an invalid format (e.g. not a Hash)
45
+
46
+ def initialize(msg = 'Event tags provided are in an invalid format.')
47
+ super
48
+ end
49
+ end
50
+
43
51
  class InvalidExperimentError < Error
44
52
  # Raised when an invalid experiment key is provided
45
53
 
@@ -0,0 +1,57 @@
1
+ #
2
+ # Copyright 2017, Optimizely and contributors
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ require 'optimizely/logger'
17
+ require 'optimizely/helpers/validator'
18
+
19
+ module Optimizely
20
+ module Helpers
21
+ module EventTagUtils
22
+ module_function
23
+
24
+ def get_revenue_value(event_tags)
25
+ # Grab the revenue value from the event tags. "revenue" is a reserved keyword.
26
+ #
27
+ # event_tags - Hash representing metadata associated with the event.
28
+ # Returns revenue value as an integer number
29
+ # Returns nil if revenue can't be retrieved from the event tags.
30
+
31
+ if event_tags.nil? or !Helpers::Validator.attributes_valid?(event_tags)
32
+ return nil
33
+ end
34
+
35
+ unless event_tags.has_key?('revenue')
36
+ return nil
37
+ end
38
+
39
+ logger = SimpleLogger.new
40
+ raw_value = event_tags['revenue']
41
+
42
+ unless raw_value.is_a? Numeric
43
+ logger.log(Logger::WARN, "Failed to parse revenue value #{raw_value} from event tags.")
44
+ return nil
45
+ end
46
+
47
+ if raw_value.is_a? Float
48
+ logger.log(Logger::WARN, "Failed to parse revenue value #{raw_value} from event tags.")
49
+ return nil
50
+ end
51
+
52
+ logger.log(Logger::INFO, "Parsed revenue value #{raw_value} from event tags.")
53
+ raw_value
54
+ end
55
+ end
56
+ end
57
+ end
@@ -32,6 +32,16 @@ module Optimizely
32
32
  attributes.is_a?(Hash)
33
33
  end
34
34
 
35
+ def event_tags_valid?(event_tags)
36
+ # Determines if provided event tags are valid.
37
+ #
38
+ # event_tags - Event tags to be validated.
39
+ #
40
+ # Returns boolean depending on validity of event tags.
41
+
42
+ event_tags.is_a?(Hash)
43
+ end
44
+
35
45
  def datafile_valid?(datafile)
36
46
  # Determines if a given datafile is valid.
37
47
  #
@@ -1,5 +1,5 @@
1
1
  #
2
- # Copyright 2016, Optimizely and contributors
2
+ # Copyright 2016-2017, Optimizely and contributors
3
3
  #
4
4
  # Licensed under the Apache License, Version 2.0 (the "License");
5
5
  # you may not use this file except in compliance with the License.
@@ -14,5 +14,5 @@
14
14
  # limitations under the License.
15
15
  #
16
16
  module Optimizely
17
- VERSION = '1.0.2'.freeze
17
+ VERSION = '1.1.0'.freeze
18
18
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: optimizely-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Delikat
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2017-02-15 00:00:00.000000000 Z
13
+ date: 2017-03-16 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: bundler
@@ -126,6 +126,7 @@ files:
126
126
  - lib/optimizely/event_dispatcher.rb
127
127
  - lib/optimizely/exceptions.rb
128
128
  - lib/optimizely/helpers/constants.rb
129
+ - lib/optimizely/helpers/event_tag_utils.rb
129
130
  - lib/optimizely/helpers/group.rb
130
131
  - lib/optimizely/helpers/validator.rb
131
132
  - lib/optimizely/logger.rb
@@ -152,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
153
  version: '0'
153
154
  requirements: []
154
155
  rubyforge_project:
155
- rubygems_version: 2.6.10
156
+ rubygems_version: 2.5.2
156
157
  signing_key:
157
158
  specification_version: 4
158
159
  summary: Ruby SDK for Optimizely's testing framework