Class: Kameleoon::Client
- Inherits:
-
Object
- Object
- Kameleoon::Client
- Defined in:
- lib/kameleoon/client.rb
Overview
Client for Kameleoon
Instance Method Summary collapse
-
#activate_feature(visitor_code, feature_key) ⇒ Object
associated targeting segment conditions were not fulfilled.
-
#add_data(visitor_code, *args) ⇒ Object
Associate various data to a visitor.
-
#feature_active?(visitor_code, feature_key) ⇒ Boolean
Check if feature is active for a given visitor code.
-
#flush(visitor_code = nil) ⇒ Object
Flush the associated data.
-
#get_active_feature_list_for_visitor(visitor_code) ⇒ Array
Returns a list of active feature flag keys for a visitor.
-
#get_experiment_list ⇒ Array
Returns a list of all experiment ids.
-
#get_experiment_list_for_visitor(visitor_code, only_allocated: true) ⇒ Array
Returns a list of all experiment ids targeted for a visitor if only_allocated is ‘true` returns a list of allocated experiments for a visitor.
-
#get_feature_all_variables(feature_key, variation_key) ⇒ Object
Retrieves all feature variable values for a given variation.
-
#get_feature_list ⇒ Array
Returns a list of all feature flag keys.
-
#get_feature_variable(visitor_code, feature_key, variable_name) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation.
-
#get_feature_variation_key(visitor_code, feature_key) ⇒ Object
get_feature_variation_key returns a variation key for visitor code.
-
#get_variation_associated_data(variation_id) ⇒ Hash
Obtain variation associated data.
-
#get_visitor_code(cookies, top_level_domain, default_visitor_code = nil) ⇒ String
Obtain a visitor code.
-
#initialize(site_code, path_config_file, interval, default_timeout, client_id = nil, client_secret = nil) ⇒ Client
constructor
You should create Client with the Client Factory only.
-
#obtain_feature_variable(feature_key, variable_key) ⇒ Object
Retrieve a feature variable.
-
#obtain_variation_associated_data(variation_id) ⇒ Object
DEPRECATED.
-
#obtain_visitor_code(cookies, top_level_domain, default_visitor_code = nil) ⇒ Object
DEPRECATED.
-
#retrieve_data_from_remote_source(key, timeout = @default_timeout) ⇒ Hash
The retrieved_data_from_remote_source method allows you to retrieve data (according to a key passed as argument) stored on a remote Kameleoon server.
-
#track_conversion(visitor_code, goal_id, revenue = 0.0) ⇒ Object
Track conversions on a particular goal.
-
#trigger_experiment(visitor_code, experiment_id) ⇒ Integer
Trigger an experiment.
Methods included from Cookie
#check_visitor_code, #obtain_hash_double, #obtain_hash_double_helper, #obtain_hash_double_v2, #read_and_write
Constructor Details
#initialize(site_code, path_config_file, interval, default_timeout, client_id = nil, client_secret = nil) ⇒ Client
You should create Client with the Client Factory only.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/kameleoon/client.rb', line 33 def initialize(site_code, path_config_file, interval, default_timeout, client_id = nil, client_secret = nil) config = YAML.load_file(path_config_file) @site_code = site_code @default_timeout = config['default_timeout'] || default_timeout # in ms refresh_interval = config['actions_configuration_refresh_interval'] @interval = refresh_interval.nil? ? interval : "#{refresh_interval}m" @tracking_url = config['tracking_url'] || "https://api-ssx.kameleoon.com" @api_data_url = "https://api-data.kameleoon.com" @client_id = client_id || config['client_id'] @client_secret = client_secret || config['client_secret'] @data_maximum_size = config['visitor_data_maximum_size'] || 500 # mb @environment = config['environment'] || DEFAULT_ENVIRONMENT @verbose_mode = config['verbose_mode'] || false @experiments = [] @feature_flags = [] @feature_flags_v2 = [] @data = {} @user_agents = {} @variation_storage = Kameleoon::Storage::VariationStorage.new end |
Instance Method Details
#activate_feature(visitor_code, feature_key) ⇒ Object
associated targeting segment conditions were not fulfilled. He should see the reference variation
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/kameleoon/client.rb', line 256 def activate_feature(visitor_code, feature_key) check_visitor_code(visitor_code) feature_flag = find_feature_flag(feature_key) check_site_code_enable(feature_flag) unless check_targeting(visitor_code, feature_flag.id.to_i, feature_flag) raise Exception::NotTargeted.new(visitor_code), "Visitor '#{visitor_code}' is not targeted for FF with key '#{feature_key}'" end if feature_flag.is_scheduled_active(Time.now.to_i) threshold = obtain_hash_double(visitor_code, {}, feature_flag.id) if threshold >= 1 - feature_flag.exposition_rate track_experiment(visitor_code, feature_flag.id, feature_flag.variations.first['id']) true else track_experiment(visitor_code, feature_flag.id, REFERENCE, none_variation: true) false end else false end end |
#add_data(visitor_code, *args) ⇒ Object
Associate various data to a visitor.
Note that this method doesn’t return any value and doesn’t interact with the Kameleoon back-end servers by itself. Instead, the declared data is saved for future sending via the flush method. This reduces the number of server calls made, as data is usually grouped into a single server call triggered by the execution of the flush method.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/kameleoon/client.rb', line 149 def add_data(visitor_code, *args) check_visitor_code(visitor_code) while ObjectSpace.memsize_of(@data) > @data_maximum_size * (2**20) do @data.shift end args.each do |data_element| if data_element.is_a?(UserAgent) add_user_agent_data(visitor_code, data_element) next end @data[visitor_code] = [] unless @data.key?(visitor_code) @data[visitor_code].push(data_element) end end |
#feature_active?(visitor_code, feature_key) ⇒ Boolean
Check if feature is active for a given visitor code
This method takes a visitor_code and feature_key as mandatory arguments to check if the specified feature will be active for a given user. If such a user has never been associated with this feature flag, the SDK returns a boolean value randomly (true if the user should have this feature or false if not). If a user with a given visitorCode is already registered with this feature flag, it will detect the previous feature flag value. You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.
296 297 298 299 |
# File 'lib/kameleoon/client.rb', line 296 def feature_active?(visitor_code, feature_key) _, variation_key = _get_feature_variation_key(visitor_code, feature_key) variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF end |
#flush(visitor_code = nil) ⇒ Object
Flush the associated data.
The data added with the method add_data, is not directly sent to the kameleoon servers. It’s stored and accumulated until it is sent automatically by the trigger_experiment or track_conversion methods. With this method you can manually send it.
196 197 198 199 200 201 202 203 |
# File 'lib/kameleoon/client.rb', line 196 def flush(visitor_code = nil) check_visitor_code(visitor_code) unless visitor_code.nil? if !visitor_code.nil? track_data(visitor_code) else @data.select { |_, values| values.any? { |data| !data.sent } }.each_key { |key| flush(key) } end end |
#get_active_feature_list_for_visitor(visitor_code) ⇒ Array
Returns a list of active feature flag keys for a visitor
467 468 469 470 471 472 473 474 475 |
# File 'lib/kameleoon/client.rb', line 467 def get_active_feature_list_for_visitor(visitor_code) check_visitor_code(visitor_code) list_keys = [] @feature_flags_v2.each do |feature_flag| variation_key, = _calculate_variation_key_for_feature(visitor_code, feature_flag) list_keys.push(feature_flag.feature_key) if variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF end list_keys end |
#get_experiment_list ⇒ Array
Returns a list of all experiment ids
431 432 433 |
# File 'lib/kameleoon/client.rb', line 431 def get_experiment_list @experiments.map { |it| it.id.to_i } end |
#get_experiment_list_for_visitor(visitor_code, only_allocated: true) ⇒ Array
Returns a list of all experiment ids targeted for a visitor if only_allocated is ‘true` returns a list of allocated experiments for a visitor
442 443 444 445 446 447 448 449 450 451 |
# File 'lib/kameleoon/client.rb', line 442 def get_experiment_list_for_visitor(visitor_code, only_allocated: true) list_ids = [] @experiments.each do |experiment| next unless check_targeting(visitor_code, experiment.id.to_i, experiment) next if only_allocated && variation_for_experiment(visitor_code, experiment).nil? list_ids.push(experiment.id.to_i) end list_ids end |
#get_feature_all_variables(feature_key, variation_key) ⇒ Object
Retrieves all feature variable values for a given variation
This method takes a feature_key and variation_key as mandatory arguments and returns a list of variables for a given variation key A feature variable can be changed easily via our web application.
358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/kameleoon/client.rb', line 358 def get_feature_all_variables(feature_key, variation_key) feature_flag = find_feature_flag_v2(feature_key) variation = feature_flag.get_variation_key(variation_key) if variation.nil? raise Exception::VariationConfigurationNotFound.new(variation_key), "Variation key #{variation_key} not found" end variables = {} variation.variables.each { |var| variables[var.key] = _parse_feature_variable(var) } variables end |
#get_feature_list ⇒ Array
Returns a list of all feature flag keys
457 458 459 |
# File 'lib/kameleoon/client.rb', line 457 def get_feature_list @feature_flags_v2.map(&:feature_key) end |
#get_feature_variable(visitor_code, feature_key, variable_name) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation
A feature variable can be changed easily via our web application.
334 335 336 337 338 339 340 341 342 343 |
# File 'lib/kameleoon/client.rb', line 334 def get_feature_variable(visitor_code, feature_key, variable_name) feature_flag, variation_key = _get_feature_variation_key(visitor_code, feature_key) variation = feature_flag.get_variation_key(variation_key) variable = variation&.get_variable_by_key(variable_name) if variable.nil? raise Exception::FeatureVariableNotFound.new(variable_name), "Feature variable #{variable_name} not found" end _parse_feature_variable(variable) end |
#get_feature_variation_key(visitor_code, feature_key) ⇒ Object
get_feature_variation_key returns a variation key for visitor code
This method takes a visitorCode and featureKey as mandatory arguments and returns a variation assigned for a given visitor If such a user has never been associated with any feature flag rules, the SDK returns a default variation key You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.
316 317 318 319 |
# File 'lib/kameleoon/client.rb', line 316 def get_feature_variation_key(visitor_code, feature_key) _, variation_key = _get_feature_variation_key(visitor_code, feature_key) variation_key end |
#get_variation_associated_data(variation_id) ⇒ Hash
Obtain variation associated data.
To retrieve JSON data associated with a variation, call the get_variation_associated_data method of our SDK. The JSON data usually represents some metadata of the variation, and can be configured on our web application interface or via our Automation API. This method takes the variationID as a parameter and will return the data as a json string. It will throw an exception () if the variation ID is wrong or corresponds to an experiment that is not yet online.
220 221 222 223 224 225 226 227 228 |
# File 'lib/kameleoon/client.rb', line 220 def get_variation_associated_data(variation_id) variation = @experiments.map(&:variations).flatten.select { |var| var['id'].to_i == variation_id.to_i }.first if variation.nil? raise Exception::VariationConfigurationNotFound.new(variation_id), "Variation key #{variation_id} not found" else JSON.parse(variation['customJson']) end end |
#get_visitor_code(cookies, top_level_domain, default_visitor_code = nil) ⇒ String
The implementation logic is described here:
Obtain a visitor code.
This method should be called to obtain the Kameleoon visitor_code for the current visitor. This is especially important when using Kameleoon in a mixed front-end and back-end environment, where user identification consistency must be guaranteed. First we check if a kameleoonVisitorCode cookie or query parameter associated with the current HTTP request can be found. If so, we will use this as the visitor identifier. If no cookie / parameter is found in the current request, we either randomly generate a new identifier, or use the defaultVisitorCode argument as identifier if it is passed. This allows our customers to use their own identifiers as visitor codes, should they wish to. This can have the added benefit of matching Kameleoon visitors with their own users without any additional look-ups in a matching table. In any case, the server-side (via HTTP header) kameleoonVisitorCode cookie is set with the value. Then this identifier value is finally returned by the method.
cookies = => ‘1234asdf4321fdsa’ visitor_code = get_visitor_code(cookies, ‘my-domaine.com’)
80 81 82 |
# File 'lib/kameleoon/client.rb', line 80 def get_visitor_code(, top_level_domain, default_visitor_code = nil) read_and_write(, top_level_domain, , default_visitor_code) end |
#obtain_feature_variable(feature_key, variable_key) ⇒ Object
Retrieve a feature variable.
A feature variable can be changed easily via our web application.
DEPRECATED. Please use ‘get_feature_variable` instead.
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/kameleoon/client.rb', line 382 def obtain_feature_variable(feature_key, variable_key) warn '[DEPRECATION] `obtain_feature_variable` is deprecated. Please use `get_feature_variable` instead.' feature_flag = find_feature_flag(feature_key) custom_json = JSON.parse(feature_flag.variations.first['customJson'])[variable_key.to_s] if custom_json.nil? raise Exception::FeatureVariableNotFound.new('Feature variable not found') end case custom_json['type'] when 'Boolean' custom_json['value'] when 'String' custom_json['value'] when 'Number' custom_json['value'].to_i when 'JSON' JSON.parse(custom_json['value']) else raise TypeError.new('Unknown type for feature variable') end end |
#obtain_variation_associated_data(variation_id) ⇒ Object
DEPRECATED. Please use ‘get_variation_associated_data` instead.
231 232 233 234 235 |
# File 'lib/kameleoon/client.rb', line 231 def obtain_variation_associated_data(variation_id) warn '[DEPRECATION] `obtain_variation_associated_data` is deprecated. Please use `get_variation_associated_data` instead.' get_variation_associated_data(variation_id) end |
#obtain_visitor_code(cookies, top_level_domain, default_visitor_code = nil) ⇒ Object
DEPRECATED. Please use ‘get_visitor_code` instead.
85 86 87 88 |
# File 'lib/kameleoon/client.rb', line 85 def obtain_visitor_code(, top_level_domain, default_visitor_code = nil) warn '[DEPRECATION] `obtain_visitor_code` is deprecated. Please use `get_visitor_code` instead.' get_visitor_code(, top_level_domain, default_visitor_code) end |
#retrieve_data_from_remote_source(key, timeout = @default_timeout) ⇒ Hash
The retrieved_data_from_remote_source method allows you to retrieve data (according to a key passed as argument) stored on a remote Kameleoon server. Usually data will be stored on our remote servers via the use of our Data API. This method, along with the availability of our highly scalable servers for this purpose, provides a convenient way to quickly store massive amounts of data that can be later retrieved for each of your visitors / users.
415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/kameleoon/client.rb', line 415 def retrieve_data_from_remote_source(key, timeout = @default_timeout) = { connect_timeout: (timeout.to_f / 1000.0) } path = get_api_data_request_url(key) log "Retrieve API Data connexion: #{.inspect}" response = get_sync(@api_data_url + path, ) if successful_sync?(response) JSON.parse(response.body) unless response.nil? else return nil end end |
#track_conversion(visitor_code, goal_id, revenue = 0.0) ⇒ Object
Track conversions on a particular goal
This method requires visitor_code and goal_id to track conversion on this particular goal. In addition, this method also accepts revenue as a third optional argument to track revenue. The visitor_code usually is identical to the one that was used when triggering the experiment. The track_conversion method doesn’t return any value. This method is non-blocking as the server call is made asynchronously.
179 180 181 182 183 |
# File 'lib/kameleoon/client.rb', line 179 def track_conversion(visitor_code, goal_id, revenue = 0.0) check_visitor_code(visitor_code) add_data(visitor_code, Conversion.new(goal_id, revenue)) flush(visitor_code) end |
#trigger_experiment(visitor_code, experiment_id) ⇒ Integer
Trigger an experiment.
If such a visitor_code has never been associated with any variation, the SDK returns a randomly selected variation If a user with a given visitor_code is already registered with a variation, it will detect the previously registered variation and return the variation_id. You have to make sure that proper error handling is set up in your code as shown in the example to the right to catch potential exceptions.
Usually, this happens because the user has been associated with excluded traffic associated targeting segment conditions were not fulfilled. He should see the reference variation
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/kameleoon/client.rb', line 111 def trigger_experiment(visitor_code, experiment_id) check_visitor_code(visitor_code) experiment = @experiments.find { |exp| exp.id.to_s == experiment_id.to_s } if experiment.nil? raise Exception::ExperimentConfigurationNotFound.new(experiment_id) end check_site_code_enable(experiment) if check_targeting(visitor_code, experiment_id, experiment) saved_variation = get_valid_saved_variation(visitor_code, experiment) variation_key = saved_variation || variation_for_experiment(visitor_code, experiment) if !variation_key.nil? track_experiment(visitor_code, experiment_id, variation_key) @variation_storage.update_variation(visitor_code, experiment_id, variation_key) if saved_variation.nil? variation_key else track_experiment(visitor_code, experiment_id, REFERENCE, true) raise Exception::NotAllocated.new(visitor_code), "Experiment #{experiment_id} is not active for visitor #{visitor_code}" end else raise Exception::NotTargeted.new(visitor_code), "Experiment #{experiment_id} is not targeted for visitor #{visitor_code}" end end |