Class: Kameleoon::KameleoonClient
- Inherits:
-
Object
- Object
- Kameleoon::KameleoonClient
- Includes:
- Exception
- Defined in:
- lib/kameleoon/kameleoon_client.rb
Overview
Client for Kameleoon
Instance Attribute Summary collapse
-
#site_code ⇒ Object
readonly
Returns the value of attribute site_code.
Instance Method Summary collapse
-
#add_data(visitor_code, *args) ⇒ Object
Associate various data to a visitor.
-
#feature_active?(visitor_code, feature_key, is_unique_identifier: false) ⇒ Boolean
Check if feature is active for a given visitor code.
-
#flush(visitor_code = nil, is_unique_identifier: false) ⇒ 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_active_features(visitor_code) ⇒ Hash
Returns a Hash that contains the assigned variations of the active features using the keys of the corresponding active features.
-
#get_engine_tracking_code(visitor_code) ⇒ String
The ‘get_engine_tracking_code` returns the JavasScript code to be inserted in your page to send automatically the exposure events to the analytics solution you are using.
-
#get_feature_list ⇒ Array
Returns a list of all feature flag keys.
-
#get_feature_variable(visitor_code, feature_key, variable_name, is_unique_identifier: false) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation.
-
#get_feature_variation_key(visitor_code, feature_key, is_unique_identifier: false) ⇒ Object
get_feature_variation_key returns a variation key for visitor code.
-
#get_feature_variation_variables(feature_key, variation_key) ⇒ Object
Retrieves all feature variable values for a given variation.
-
#get_remote_data(key, timeout = @default_timeout) ⇒ Hash
The get_remote_data method allows you to retrieve data (according to a key passed as argument) stored on a remote Kameleoon server.
-
#get_remote_visitor_data(visitor_code, timeout = nil, add_data: true, filter: nil, is_unique_identifier: false) ⇒ Array
The get_remote_visitor_data is a method for retrieving custom data for the latest visit of ‘visitor_code` from Kameleoon Data API and optionally adding it to the storage so that other methods could decide whether the current visitor is targeted or not.
-
#get_visitor_code(cookies, default_visitor_code = nil) ⇒ String
Obtain a visitor code.
-
#get_visitor_warehouse_audience(visitor_code, custom_data_index, timeout = nil, warehouse_key: nil) ⇒ Kameleoon::CustomData
Retrieves data associated with a visitor’s warehouse audiences and adds it to the visitor.
-
#initialize(site_code, config) ⇒ KameleoonClient
constructor
You should create KameleoonClient with the Client Factory only.
-
#on_update_configuration(handler) ⇒ Object
The ‘on_update_configuration()` method allows you to handle the event when configuration has updated data.
- #set_legal_consent(visitor_code, consent, cookies = nil) ⇒ Object
-
#track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: false) ⇒ Object
Track conversions on a particular goal.
- #wait_init ⇒ Object
Constructor Details
#initialize(site_code, config) ⇒ KameleoonClient
You should create KameleoonClient with the Client Factory only.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/kameleoon/kameleoon_client.rb', line 43 def initialize(site_code, config) raise Exception::SiteCodeIsEmpty, 'Provided site_sode is empty' if site_code&.empty? != false @site_code = site_code @config = config @real_time_configuration_service = nil @update_configuration_handler = nil @fetch_configuration_update_job = nil @data_file = Configuration::DataFile.new(config.environment, method(:log)) @visitor_manager = Kameleoon::DataManager::VisitorManager.new(config.session_duration_second, method(:log)) @hybrid_manager = Kameleoon::Hybrid::ManagerImpl.new(HYBRID_EXPIRATION_TIME, method(:log)) @network_manager = Network::NetworkManager.new( config.environment, config.default_timeout_millisecond, Network::AccessTokenSourceFactory.new(config.client_id, config.client_secret, method(:log)), Network::UrlProvider.new(site_code), method(:log) ) @warehouse_manager = Managers::Warehouse::WarehouseManager.new(@network_manager, @visitor_manager, method(:log)) @remote_data_manager = Kameleoon::Managers::RemoteData::RemoteDataManager.new(@network_manager, @visitor_manager, method(:log)) = Network::Cookie::CookieManager.new(config.top_level_domain) @readiness = ClientReadiness.new @targeting_manager = Kameleoon::Targeting::TargetingManager.new(@visitor_manager, @data_file) @visitor_manager.custom_data_info = @data_file.custom_data_info end |
Instance Attribute Details
#site_code ⇒ Object (readonly)
Returns the value of attribute site_code.
38 39 40 |
# File 'lib/kameleoon/kameleoon_client.rb', line 38 def site_code @site_code end |
Instance Method Details
#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.
124 125 126 127 |
# File 'lib/kameleoon/kameleoon_client.rb', line 124 def add_data(visitor_code, *args) Utils::VisitorCode.validate(visitor_code) @visitor_manager.add_data(visitor_code, *args) end |
#feature_active?(visitor_code, feature_key, is_unique_identifier: false) ⇒ 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.
This field is optional.
195 196 197 198 199 200 |
# File 'lib/kameleoon/kameleoon_client.rb', line 195 def feature_active?(visitor_code, feature_key, is_unique_identifier: false) _, variation_key = _get_feature_variation_key(visitor_code, feature_key, is_unique_identifier) variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF rescue Exception::FeatureEnvironmentDisabled false end |
#flush(visitor_code = nil, is_unique_identifier: false) ⇒ 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.
This field is optional.
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/kameleoon/kameleoon_client.rb', line 165 def flush(visitor_code = nil, is_unique_identifier: false) Utils::VisitorCode.validate(visitor_code) unless visitor_code.nil? if visitor_code.nil? @visitor_manager.enumerate do |visitor_code, visitor| _send_tracking_request(visitor_code, visitor, false, is_unique_identifier) end else _send_tracking_request(visitor_code, nil, true, is_unique_identifier) end end |
#get_active_feature_list_for_visitor(visitor_code) ⇒ Array
Returns a list of active feature flag keys for a visitor
DEPRECATED. Please use ‘get_active_features` instead.
358 359 360 361 362 363 364 365 366 367 368 369 370 |
# File 'lib/kameleoon/kameleoon_client.rb', line 358 def get_active_feature_list_for_visitor(visitor_code) warn '[DEPRECATION] `get_active_feature_list_for_visitor` is deprecated. Please use `get_active_features` instead.' Utils::VisitorCode.validate(visitor_code) list_keys = [] @data_file.feature_flags.each do |feature_key, feature_flag| next unless feature_flag.environment_enabled variation, rule, = _calculate_variation_key_for_feature(visitor_code, feature_flag) variation_key = _get_variation_key(variation, rule, feature_flag) list_keys.push(feature_key) if variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF end list_keys end |
#get_active_features(visitor_code) ⇒ Hash
Returns a Hash that contains the assigned variations of the active features using the keys
of the corresponding active features.
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 |
# File 'lib/kameleoon/kameleoon_client.rb', line 381 def get_active_features(visitor_code) Utils::VisitorCode.validate(visitor_code) map_active_features = {} @data_file.feature_flags.each_value do |feature_flag| next unless feature_flag.environment_enabled var_by_exp, rule = _calculate_variation_key_for_feature(visitor_code, feature_flag) variation_key = _get_variation_key(var_by_exp, rule, feature_flag) next if variation_key == Configuration::VariationType::VARIATION_OFF variation = feature_flag.get_variation_key(variation_key) variables = {} variation&.variables&.each do |variable| variables[variable.key] = Kameleoon::Types::Variable.new( variable.key, variable.type, _parse_feature_variable(variable) ) end variables.freeze map_active_features[feature_flag.feature_key] = Kameleoon::Types::Variation.new( variation_key, var_by_exp ? var_by_exp.variation_id : nil, rule ? rule.experiment_id : nil, variables ) end map_active_features.freeze end |
#get_engine_tracking_code(visitor_code) ⇒ String
The ‘get_engine_tracking_code` returns the JavasScript code to be inserted in your page to send automatically the exposure events to the analytics solution you are using.
the exposure events to the analytics solution you are using.
435 436 437 438 |
# File 'lib/kameleoon/kameleoon_client.rb', line 435 def get_engine_tracking_code(visitor_code) visitor_variations = @visitor_manager.get_visitor(visitor_code)&.variations @hybrid_manager.get_engine_tracking_code(visitor_variations) end |
#get_feature_list ⇒ Array
Returns a list of all feature flag keys
346 347 348 |
# File 'lib/kameleoon/kameleoon_client.rb', line 346 def get_feature_list # rubocop:disable Naming/AccessorMethodName @data_file.feature_flags.keys end |
#get_feature_variable(visitor_code, feature_key, variable_name, is_unique_identifier: false) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation
A feature variable can be changed easily via our web application.
This field is optional.
the current environment
243 244 245 246 247 248 249 250 251 252 |
# File 'lib/kameleoon/kameleoon_client.rb', line 243 def get_feature_variable(visitor_code, feature_key, variable_name, is_unique_identifier: false) feature_flag, variation_key = _get_feature_variation_key(visitor_code, feature_key, is_unique_identifier) 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, is_unique_identifier: false) ⇒ 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.
This field is optional.
the current environment
221 222 223 224 |
# File 'lib/kameleoon/kameleoon_client.rb', line 221 def get_feature_variation_key(visitor_code, feature_key, is_unique_identifier: false) _, variation_key = _get_feature_variation_key(visitor_code, feature_key, is_unique_identifier) variation_key end |
#get_feature_variation_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.
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/kameleoon/kameleoon_client.rb', line 268 def get_feature_variation_variables(feature_key, variation_key) feature_flag = @data_file.get_feature_flag(feature_key) variation = feature_flag.get_variation_key(variation_key) if variation.nil? raise Exception::FeatureVariationNotFound.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_remote_data(key, timeout = @default_timeout) ⇒ Hash
The get_remote_data 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.
This field is optional.
292 293 294 |
# File 'lib/kameleoon/kameleoon_client.rb', line 292 def get_remote_data(key, timeout = @default_timeout) @remote_data_manager.get_data(key, timeout) end |
#get_remote_visitor_data(visitor_code, timeout = nil, add_data: true, filter: nil, is_unique_identifier: false) ⇒ Array
The get_remote_visitor_data is a method for retrieving custom data for the latest visit of ‘visitor_code` from Kameleoon Data API and optionally adding it to the storage so that other methods could decide whether the current visitor is targeted or not.
This field is mandatory. for a visitor. If not specified, the default value is ‘True`. This field is optional. This field is optional. This field is optional.
311 312 313 |
# File 'lib/kameleoon/kameleoon_client.rb', line 311 def get_remote_visitor_data(visitor_code, timeout = nil, add_data: true, filter: nil, is_unique_identifier: false) @remote_data_manager.get_visitor_data(visitor_code, add_data, filter, is_unique_identifier, timeout) end |
#get_visitor_code(cookies, 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 default_visitor_code 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’)
100 101 102 |
# File 'lib/kameleoon/kameleoon_client.rb', line 100 def get_visitor_code(, default_visitor_code = nil) .get_or_add(, default_visitor_code) end |
#get_visitor_warehouse_audience(visitor_code, custom_data_index, timeout = nil, warehouse_key: nil) ⇒ Kameleoon::CustomData
Retrieves data associated with a visitor’s warehouse audiences and adds it to the visitor. Retrieves all audience data associated with the visitor in your data warehouse using the specified ‘visitor_code` and `warehouse_key`. The `warehouse_key` is typically your internal user ID. The `custom_data_index` parameter corresponds to the Kameleoon custom data that Kameleoon uses to target your visitors. You can refer to the <a href=“help.kameleoon.com/warehouse-audience-targeting/”>warehouse targeting documentation</a> for additional details. The method returns a `CustomData` object, confirming that the data has been added to the visitor and is available for targeting purposes.
This field is mandatory. your BigQuery Audiences. This field is mandatory. This field is optional. This field is optional.
338 339 340 |
# File 'lib/kameleoon/kameleoon_client.rb', line 338 def get_visitor_warehouse_audience(visitor_code, custom_data_index, timeout = nil, warehouse_key: nil) @warehouse_manager.get_visitor_warehouse_audience(visitor_code, custom_data_index, warehouse_key, timeout) end |
#on_update_configuration(handler) ⇒ Object
The ‘on_update_configuration()` method allows you to handle the event when configuration has updated data. It takes one input parameter: callable handler. The handler that will be called when the configuration is updated using a real-time configuration event.
is updated using a real-time configuration event.
423 424 425 |
# File 'lib/kameleoon/kameleoon_client.rb', line 423 def on_update_configuration(handler) @update_configuration_handler = handler end |
#set_legal_consent(visitor_code, consent, cookies = nil) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/kameleoon/kameleoon_client.rb', line 104 def (visitor_code, , = nil) Utils::VisitorCode.validate(visitor_code) visitor = @visitor_manager.get_or_create_visitor(visitor_code) visitor. = .update(visitor_code, , ) end |
#track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: false) ⇒ 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.
This field is optional.
146 147 148 149 150 |
# File 'lib/kameleoon/kameleoon_client.rb', line 146 def track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: false) Utils::VisitorCode.validate(visitor_code) add_data(visitor_code, Conversion.new(goal_id, revenue)) flush(visitor_code, is_unique_identifier: is_unique_identifier) end |
#wait_init ⇒ Object
70 71 72 |
# File 'lib/kameleoon/kameleoon_client.rb', line 70 def wait_init @readiness.wait end |