Class: Kameleoon::KameleoonClient
- Inherits:
-
Object
- Object
- Kameleoon::KameleoonClient
- Includes:
- Exception
- Defined in:
- lib/kameleoon/kameleoon_client.rb
Overview
Client for Kameleoon
Defined Under Namespace
Classes: EvaluatedExperiment
Instance Attribute Summary collapse
-
#site_code ⇒ Object
readonly
Returns the value of attribute site_code.
Instance Method Summary collapse
-
#add_data(visitor_code, *args, track: true) ⇒ Object
Associate various data to a visitor.
-
#evaluate_audiences(visitor_code) ⇒ Object
Evaluates the visitor against all available Audiences Explorer segments and tracks those that match.
-
#feature_active?(visitor_code, feature_key, is_unique_identifier: nil, track: true) ⇒ Bool
Check if feature is active for a given visitor code.
-
#flush(visitor_code = nil, instant: false, is_unique_identifier: 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_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_data_file ⇒ Kameleoon::Types::DataFile
Retrieves the current SDK configuration (also known as the data file), containing all feature flags and their variations.
-
#get_engine_tracking_code(visitor_code) ⇒ String
The
get_engine_tracking_codereturns 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: nil) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation.
-
#get_feature_variation_key(visitor_code, feature_key, is_unique_identifier: nil) ⇒ 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: nil) ⇒ Array
The get_remote_visitor_data is a method for retrieving custom data for the latest visit of
visitor_codefrom 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_variation(visitor_code, feature_key, track: true) ⇒ Kameleoon::Types::Variation
Retrieves the variation assigned to the given visitor for a specific feature flag.
-
#get_variations(visitor_code, only_active: false, track: true) ⇒ Hash
Forms a dictionary of variations assigned to a given visitor across all feature flags.
-
#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.
-
#is_ready? ⇒ Boolean
Indicates whether the SDK is ready for use, i.e.
-
#on_update_configuration(handler) ⇒ Object
The
on_update_configuration()method allows you to handle the event when configuration has updated data. -
#set_event_handler(event_type, handler) ⇒ Object
Sets the SDK event handler for the specified event type.
-
#set_forced_variation(visitor_code, experiment_id, variation_key, force_targeting: true) ⇒ Object
Sets or resets a forced variation for a visitor in a specific experiment, so the experiment will be evaluated to the variation for the visitor.
- #set_legal_consent(visitor_code, consent, cookies = nil) ⇒ Object
-
#track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: nil, negative: false, metadata: nil) ⇒ Object
Track conversions on a particular goal.
-
#wait_init(timeout_millisecond = nil) ⇒ Boolean
Block until the SDK has loaded its configuration and is ready to use.
Constructor Details
#initialize(site_code, config) ⇒ KameleoonClient
You should create KameleoonClient with the Client Factory only.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/kameleoon/kameleoon_client.rb', line 60 def initialize(site_code, config) raise Exception::SiteCodeIsEmpty, 'Provided site_sode is empty' if site_code&.empty? != false Logging::KameleoonLogger.info("CALL: KameleoonClient.new(site_code: '%s', config: %s)", site_code, config) @scheduler = Rufus::Scheduler.new @site_code = site_code @config = config @disposed = false @real_time_event_service = nil @update_configuration_handler = nil @fetch_configuration_update_job = nil data_file = Configuration::DataFile.new(config.environment, nil) @data_manager = Managers::Data::DataManager.new(data_file) @visitor_manager = Kameleoon::DataManager::VisitorManager.new( @data_manager, config.session_duration_second, @scheduler ) @hybrid_manager = Hybrid::ManagerImpl.new(HYBRID_EXPIRATION_TIME, @data_manager) @event_manager = Events::EventManager.new @network_manager = Network::NetworkManager.new( config.environment, config.default_timeout_millisecond, Network::AccessTokenSourceFactory.new(config.client_id, config.client_secret), Network::UrlProvider.new(site_code, config.network_domain), @event_manager ) @tracking_manager = Managers::Tracking::TrackingManager.new( @data_manager, @network_manager, @visitor_manager, config.tracking_interval_second, @scheduler ) @warehouse_manager = Managers::Warehouse::WarehouseManager.new(@network_manager, @visitor_manager) @remote_data_manager = Managers::RemoteData::RemoteDataManager.new( @data_manager, @network_manager, @visitor_manager ) @cookie_manager = Network::Cookie::CookieManager.new(@data_manager, @visitor_manager, config.top_level_domain) @readiness = ClientReadiness.new(site_code, config.environment) @targeting_manager = Targeting::TargetingManager.new(@data_manager, @visitor_manager) if @config.verbose_mode == true && Logging::KameleoonLogger.log_level == Logging::LogLevel::WARNING Logging::KameleoonLogger.log_level = Logging::LogLevel::INFO end Logging::KameleoonLogger.info("RETURN: KameleoonClient.new(site_code: '%s', config: %s)", site_code, config) end |
Instance Attribute Details
#site_code ⇒ Object (readonly)
Returns the value of attribute site_code.
55 56 57 |
# File 'lib/kameleoon/kameleoon_client.rb', line 55 def site_code @site_code end |
Instance Method Details
#add_data(visitor_code, *args, track: true) ⇒ 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.
222 223 224 225 226 227 228 229 230 231 |
# File 'lib/kameleoon/kameleoon_client.rb', line 222 def add_data(visitor_code, *args, track: true) Logging::KameleoonLogger.info("CALL: KameleoonClient.add_data(visitor_code: '%s', args: %s, track: %s)", visitor_code, args, track) Utils::VisitorCode.validate(visitor_code) @visitor_manager.add_data(visitor_code, *args, track: track) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.add_data(visitor_code: '%s', args: %s, track: %s)", visitor_code, args, track ) end |
#evaluate_audiences(visitor_code) ⇒ Object
Evaluates the visitor against all available Audiences Explorer segments and tracks those that match. A detailed analysis of segment performance can then be performed directly in Audiences Explorer.
861 862 863 864 865 866 867 868 869 870 871 872 873 |
# File 'lib/kameleoon/kameleoon_client.rb', line 861 def evaluate_audiences(visitor_code) Logging::KameleoonLogger.info("CALL: KameleoonClient.evaluate_audiences(visitor_code: '%s')", visitor_code) Utils::VisitorCode.validate(visitor_code) segments = @data_manager.data_file.audience_tracking_segments.select do |seg| check_targeting(visitor_code, nil, seg) end unless segments.empty? segments.map! { |seg| TargetedSegment.new(seg.id) } @visitor_manager.add_data(visitor_code, *segments) end @tracking_manager.add_visitor_code(visitor_code) Logging::KameleoonLogger.info("RETURN: KameleoonClient.evaluate_audiences(visitor_code: '%s')", visitor_code) end |
#feature_active?(visitor_code, feature_key, is_unique_identifier: nil, track: true) ⇒ Bool
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.
identifier. This field is optional.
(true) or disabled (false); the default value is true.
a random boolean value (true if the user should have this feature or false if not).
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/kameleoon/kameleoon_client.rb', line 345 def feature_active?(visitor_code, feature_key, is_unique_identifier: nil, track: true) Logging::KameleoonLogger.info( "CALL: KameleoonClient.feature_active?(visitor_code: '%s', feature_key: '%s', " \ 'is_unique_identifier: %s, track: %s)', visitor_code, feature_key, is_unique_identifier, track ) Utils::VisitorCode.validate(visitor_code) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? feature_flag = @data_manager.data_file.get_feature_flag(feature_key) variation_key, = get_variation_info(visitor_code, feature_flag, track) feature_active = variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF @tracking_manager.add_visitor_code(visitor_code) if track Logging::KameleoonLogger.info( "RETURN: KameleoonClient.feature_active?(visitor_code: '%s', feature_key: '%s', " \ 'is_unique_identifier: %s, track: %s) -> (feature_active: %s)', visitor_code, feature_key, is_unique_identifier, track, feature_active ) feature_active rescue Exception::FeatureEnvironmentDisabled Logging::KameleoonLogger.debug('Feature environment disabled') false end |
#flush(visitor_code = nil, instant: false, is_unique_identifier: 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.
according to the scheduled tracking interval (false).
identifier. This field is optional.
longer than 255 chars
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 |
# File 'lib/kameleoon/kameleoon_client.rb', line 289 def flush(visitor_code = nil, instant: false, is_unique_identifier: nil) Logging::KameleoonLogger.info( "CALL: KameleoonClient.flush(visitor_code: '%s', instant: %s, is_unique_identifier: %s)", visitor_code, instant, is_unique_identifier ) if visitor_code.nil? @visitor_manager.enumerate do |visitor_code, visitor| has_unsent_data = false visitor.enumerate_sendable_data do |data| if data.unsent has_unsent_data = true next false end next true end @tracking_manager.add_visitor_code(visitor_code) if has_unsent_data end else Utils::VisitorCode.validate(visitor_code) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? if instant @tracking_manager.track_visitor(visitor_code) else @tracking_manager.add_visitor_code(visitor_code) end end Logging::KameleoonLogger.info( "RETURN: KameleoonClient.flush(visitor_code: '%s', instant: %s, is_unique_identifier: %s)", visitor_code, instant, is_unique_identifier ) 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.
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
# File 'lib/kameleoon/kameleoon_client.rb', line 686 def get_active_feature_list_for_visitor(visitor_code) Logging::KameleoonLogger.info( '[DEPRECATION] `get_active_feature_list_for_visitor` is deprecated. Please use `get_active_features` instead.' ) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_active_feature_list_for_visitor(visitor_code: '%s')", visitor_code ) Utils::VisitorCode.validate(visitor_code) visitor = @visitor_manager.get_visitor(visitor_code) list_keys = [] @data_manager.data_file.feature_flags.each do |feature_key, feature_flag| begin eval_exp = evaluate(visitor, visitor_code, feature_flag, false, false) rescue Exception::FeatureEnvironmentDisabled next end variation_key = calculate_variation_key(eval_exp, feature_flag.default_variation_key) list_keys.push(feature_key) if variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF end Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_active_feature_list_for_visitor(visitor_code: '%s') -> (features: %s)", visitor_code, list_keys ) 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.
DEPRECATED. Please use get_variations(visitor_code, only_active: true, track: false) instead.
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 |
# File 'lib/kameleoon/kameleoon_client.rb', line 723 def get_active_features(visitor_code) Logging::KameleoonLogger.info( '[DEPRECATION] `get_active_features` is deprecated. ' \ 'Please use `get_variations(visitor_code, only_active: true, track: false)` instead.' ) Logging::KameleoonLogger.info("CALL: KameleoonClient.get_active_features(visitor_code: '%s')", visitor_code) Utils::VisitorCode.validate(visitor_code) visitor = @visitor_manager.get_visitor(visitor_code) map_active_features = {} @data_manager.data_file.feature_flags.each_value do |feature_flag| begin eval_exp = evaluate(visitor, visitor_code, feature_flag, false, false) rescue Exception::FeatureEnvironmentDisabled next end variation_key = calculate_variation_key(eval_exp, feature_flag.default_variation_key) next if variation_key == Configuration::VariationType::VARIATION_OFF variation = feature_flag.get_variation_by_key(variation_key) map_active_features[feature_flag.feature_key] = create_external_variation(variation, eval_exp) end map_active_features.freeze Logging::KameleoonLogger.info("RETURN: KameleoonClient.get_active_features(visitor_code: '%s') -> (features: %s)", visitor_code, map_active_features) map_active_features end |
#get_data_file ⇒ Kameleoon::Types::DataFile
Retrieves the current SDK configuration (also known as the data file), containing all feature flags and their variations.
880 881 882 883 884 885 |
# File 'lib/kameleoon/kameleoon_client.rb', line 880 def get_data_file Logging::KameleoonLogger.info('CALL: KameleoonClient.get_data_file') data_file = @data_manager.external_data_file Logging::KameleoonLogger.info('RETURN: KameleoonClient.get_data_file -> (data_file: %s)', data_file) data_file 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.
797 798 799 800 801 802 803 804 805 806 |
# File 'lib/kameleoon/kameleoon_client.rb', line 797 def get_engine_tracking_code(visitor_code) Logging::KameleoonLogger.info("CALL: KameleoonClient.get_engine_tracking_code(visitor_code: '%s')", visitor_code) visitor_variations = @visitor_manager.get_visitor(visitor_code)&.variations engine_tracking_code = @hybrid_manager.get_engine_tracking_code(visitor_variations) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_engine_tracking_code(visitor_code: '%s') -> (engine_tracking_code: '%s')", visitor_code, engine_tracking_code ) engine_tracking_code end |
#get_feature_list ⇒ Array
Returns a list of all feature flag keys.
DEPRECATED. Please use get_data_file instead.
668 669 670 671 672 673 674 675 676 |
# File 'lib/kameleoon/kameleoon_client.rb', line 668 def get_feature_list # rubocop:disable Naming/AccessorMethodName Logging::KameleoonLogger.info( '[DEPRECATION] `get_feature_list` is deprecated. Please use `get_data_file` instead.' ) Logging::KameleoonLogger.info('CALL: KameleoonClient.get_feature_list') features = @data_manager.data_file.feature_flags.keys Logging::KameleoonLogger.info('RETURN: KameleoonClient.get_feature_list -> (features: %s)', features) features end |
#get_feature_variable(visitor_code, feature_key, variable_name, is_unique_identifier: nil) ⇒ Object
Retrieves a feature variable value from assigned for visitor variation
A feature variable can be changed easily via our web application.
identifier. This field is optional.
the current environment
DEPRECATED. Please use get_variation(visitor_code, feature_key, track: true) instead.
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/kameleoon/kameleoon_client.rb', line 504 def get_feature_variable(visitor_code, feature_key, variable_name, is_unique_identifier: nil) Logging::KameleoonLogger.info( '[DEPRECATION] `get_feature_variable` is deprecated. ' \ 'Please use `get_variation(visitor_code, feature_key, track: true)` instead.' ) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_feature_variable(visitor_code: '%s', feature_key: '%s', variable_name: '%s', " \ 'is_unique_identifier: %s)', visitor_code, feature_key, variable_name, is_unique_identifier ) Utils::VisitorCode.validate(visitor_code) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? feature_flag, variation_key = _get_feature_variation_key(visitor_code, feature_key) variation = feature_flag.get_variation_by_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 value = variable.get_value Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_feature_variable(visitor_code: '%s', feature_key: '%s', variable_name: '%s', " \ 'is_unique_identifier: %s) -> (variable: %s)', visitor_code, feature_key, variable_name, is_unique_identifier, value ) value end |
#get_feature_variation_key(visitor_code, feature_key, is_unique_identifier: nil) ⇒ 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.
identifier. This field is optional.
the current environment
DEPRECATED. Please use get_variation(visitor_code, feature_key, track: true) instead.
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/kameleoon/kameleoon_client.rb', line 465 def get_feature_variation_key(visitor_code, feature_key, is_unique_identifier: nil) Logging::KameleoonLogger.info( '[DEPRECATION] `get_feature_variation_key` is deprecated. ' \ 'Please use `get_variation(visitor_code, feature_key, track: true)` instead.' ) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_feature_variation_key(visitor_code: '%s', feature_key: '%s', " \ 'is_unique_identifier: %s)', visitor_code, feature_key, is_unique_identifier ) Utils::VisitorCode.validate(visitor_code) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? _, variation_key = _get_feature_variation_key(visitor_code, feature_key) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_feature_variation_key(visitor_code: '%s', feature_key: '%s', " \ "is_unique_identifier: %s) -> (variation_key: '%s')", visitor_code, feature_key, is_unique_identifier, variation_key ) 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.
DEPRECATED. Please use get_variation(visitor_code, feature_key, track: false) instead.
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 |
# File 'lib/kameleoon/kameleoon_client.rb', line 546 def get_feature_variation_variables(feature_key, variation_key) Logging::KameleoonLogger.info( '[DEPRECATION] `get_feature_variation_variables` is deprecated. ' \ 'Please use `get_variation(visitor_code, feature_key, track: false)` instead.' ) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_feature_variation_variables(feature_key: '%s', variation_key: '%s')", feature_key, variation_key ) feature_flag = @data_manager.data_file.get_feature_flag(feature_key) variation = feature_flag.get_variation_by_key(variation_key) @data_manager.data_file.ensure_environment_enabled(feature_flag) if variation.nil? raise Exception::FeatureVariationNotFound.new(variation_key), "Variation key #{variation_key} not found" end variables = {} variation.variables.each { |var| variables[var.key] = var.get_value } Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_feature_variation_variables(feature_key: '%s', variation_key: '%s') " \ '-> (variables: %s)', feature_key, variation_key, variables ) 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.
583 584 585 586 587 588 589 590 591 592 |
# File 'lib/kameleoon/kameleoon_client.rb', line 583 def get_remote_data(key, timeout = @default_timeout) Logging::KameleoonLogger.info("CALL: KameleoonClient.get_remote_data(key: '%s', timeout: %s)", key, timeout) remote_data = @remote_data_manager.get_data(key, timeout) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_remote_data(key: '%s', timeout: %s) -> (remote_data: %s)", key, timeout, remote_data ) remote_data end |
#get_remote_visitor_data(visitor_code, timeout = nil, add_data: true, filter: nil, is_unique_identifier: nil) ⇒ 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.
identifier. This field is optional.
610 611 612 613 614 615 616 617 618 619 620 621 622 623 |
# File 'lib/kameleoon/kameleoon_client.rb', line 610 def get_remote_visitor_data(visitor_code, timeout = nil, add_data: true, filter: nil, is_unique_identifier: nil) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_remote_visitor_data(visitor_code: '%s', timeout: %s, add_data: %s, " \ 'filter: %s, is_unique_identifier: %s)', visitor_code, timeout, add_data, filter, is_unique_identifier ) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? visitor_data = @remote_data_manager.get_visitor_data(visitor_code, add_data, filter, timeout) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_remote_visitor_data(visitor_code: '%s', timeout: %s, add_data: %s, " \ 'filter: %s, is_unique_identifier: %s) -> (visitor_data: %s)', visitor_code, timeout, add_data, filter, is_unique_identifier, visitor_data ) visitor_data end |
#get_variation(visitor_code, feature_key, track: true) ⇒ Kameleoon::Types::Variation
Retrieves the variation assigned to the given visitor for a specific feature flag.
(true) or disabled (false); the default value is true.
rule of the feature flag, otherwise the method returns the default variation of the feature flag.
the current environment.
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/kameleoon/kameleoon_client.rb', line 383 def get_variation(visitor_code, feature_key, track: true) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_variation(visitor_code: '%s', feature_key: '%s', track: %s)", visitor_code, feature_key, track ) Utils::VisitorCode.validate(visitor_code) feature_flag = @data_manager.data_file.get_feature_flag(feature_key) variation_key, eval_exp = get_variation_info(visitor_code, feature_flag, track) variation = feature_flag.get_variation_by_key(variation_key) external_variation = create_external_variation(variation, eval_exp) @tracking_manager.add_visitor_code(visitor_code) if track Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_variation(visitor_code: '%s', feature_key: '%s', track: %s)" \ ' -> (variation: %s)', visitor_code, feature_key, track, external_variation ) external_variation end |
#get_variations(visitor_code, only_active: false, track: true) ⇒ Hash
Forms a dictionary of variations assigned to a given visitor across all feature flags. This method iterates over all available feature flags and returns the assigned variation for each flag associated with the specified visitor.
flags (true) or for any feature flags (false); the default value is false.
(true) or disabled (false); the default value is true.
variations (or the default variation of that feature flag) as values (Kameleoon::Types::Variation).
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/kameleoon/kameleoon_client.rb', line 417 def get_variations(visitor_code, only_active: false, track: true) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_variations(visitor_code: '%s', only_active: %s, track: %s)", visitor_code, only_active, track ) Utils::VisitorCode.validate(visitor_code) variations = {} @data_manager.data_file.feature_flags.each_value do |feature_flag| begin variation_key, eval_exp = get_variation_info(visitor_code, feature_flag, track) rescue Exception::FeatureEnvironmentDisabled next end next if only_active && (variation_key == Configuration::VariationType::VARIATION_OFF) variation = feature_flag.get_variation_by_key(variation_key) variations[feature_flag.feature_key] = create_external_variation(variation, eval_exp) end variations.freeze @tracking_manager.add_visitor_code(visitor_code) if track Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_variations(visitor_code: '%s', only_active: %s, track: %s)" \ ' -> (variations: %s)', visitor_code, only_active, track, variations ) variations 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')
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/kameleoon/kameleoon_client.rb', line 181 def get_visitor_code(, default_visitor_code = nil) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_visitor_code(cookies: %s, default_visitor_code: '%s')", , default_visitor_code ) visitor_code = @cookie_manager.get_or_add(, default_visitor_code) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_visitor_code(cookies: %s, default_visitor_code: '%s') -> (visitor_code: '%s')", , default_visitor_code, visitor_code ) 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
warehouse targeting documentation
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.
648 649 650 651 652 653 654 655 656 657 658 659 660 661 |
# File 'lib/kameleoon/kameleoon_client.rb', line 648 def get_visitor_warehouse_audience(visitor_code, custom_data_index, timeout = nil, warehouse_key: nil) Logging::KameleoonLogger.info( "CALL: KameleoonClient.get_visitor_warehouse_audience(visitor_code: '%s', custom_data_index: %s, " \ "timeout: %s, warehouse_key: '%s')", visitor_code, custom_data_index, timeout, warehouse_key ) warehouse_audience = @warehouse_manager.get_visitor_warehouse_audience(visitor_code, custom_data_index, warehouse_key, timeout) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.get_visitor_warehouse_audience(visitor_code: '%s', custom_data_index: %s, " \ "timeout: %s, warehouse_key: '%s') -> (warehouse_audience: %s)", visitor_code, custom_data_index, timeout, warehouse_key, warehouse_audience ) warehouse_audience end |
#is_ready? ⇒ Boolean
Indicates whether the SDK is ready for use, i.e. its configuration has been
successfully loaded. Unlike wait_init, it returns immediately without blocking.
It returns true if the SDK has been successfully initialized, false otherwise
(including while the initialization is still pending or has failed).
Readiness is monotonic: once the SDK becomes ready it stays ready.
149 150 151 152 153 |
# File 'lib/kameleoon/kameleoon_client.rb', line 149 def is_ready? # rubocop:disable Naming/PredicateName ready = @readiness.ready? Logging::KameleoonLogger.info('CALL/RETURN: KameleoonClient.is_ready? -> (ready: %s)', ready) ready 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.
DEPRECATED. Please use set_event_handler(Kameleoon::Events::EventType::DATAFILE_UPDATE, handler) instead.
780 781 782 783 784 785 786 787 |
# File 'lib/kameleoon/kameleoon_client.rb', line 780 def on_update_configuration(handler) Logging::KameleoonLogger.info( '[DEPRECATION] `on_update_configuration` is deprecated. ' \ 'Please use `set_event_handler(Kameleoon::Events::EventType::DATAFILE_UPDATE, handler)` instead.' ) Logging::KameleoonLogger.info('CALL/RETURN: KameleoonClient.on_update_configuration(handler)') @update_configuration_handler = handler end |
#set_event_handler(event_type, handler) ⇒ Object
Sets the SDK event handler for the specified event type.
The handler is called when the corresponding SDK event occurs. Supported event types are
defined in Kameleoon::Events::EventType. Passing nil clears the handler for the
specified event type.
Kameleoon::Events::EventType constants.
The handler must respond to the methods required by the selected event type
(see Kameleoon::Events::HttpRequestHandler and Kameleoon::Events::DataFileUpdateHandler).
765 766 767 768 769 |
# File 'lib/kameleoon/kameleoon_client.rb', line 765 def set_event_handler(event_type, handler) Logging::KameleoonLogger.info("CALL: KameleoonClient.set_event_handler(event_type: '%s', handler)", event_type) @event_manager.set_event_handler(event_type, handler) Logging::KameleoonLogger.info("RETURN: KameleoonClient.set_event_handler(event_type: '%s', handler)", event_type) end |
#set_forced_variation(visitor_code, experiment_id, variation_key, force_targeting: true) ⇒ Object
Sets or resets a forced variation for a visitor in a specific experiment, so the experiment will be evaluated to the variation for the visitor.
In order to reset the forced variation set the variation_key parameter to nil.
If the forced variation you want to reset does not exist, the method will have no effect.
to. Set to nil to reset the forced variation.
conditions. Otherwise, the normal targeting logic will be preserved. Optional (defaults to true).
the feature flag. the experiment.
827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 |
# File 'lib/kameleoon/kameleoon_client.rb', line 827 def set_forced_variation(visitor_code, experiment_id, variation_key, force_targeting: true) Logging::KameleoonLogger.info( "CALL: KameleoonClient.set_forced_variation(visitor_code: '%s', experiment_id: %d, variation_key: %s, " \ 'force_targeting: %s)', visitor_code, experiment_id, variation_key.nil? ? 'nil' : "'#{variation_key}'", force_targeting ) Utils::VisitorCode.validate(visitor_code) if variation_key.nil? visitor = @visitor_manager.get_visitor(visitor_code) visitor&.reset_forced_experiment_variation(experiment_id) else rule_info = @data_manager.data_file.rule_info_by_exp_id[experiment_id] if rule_info.nil? raise Exception::FeatureExperimentNotFound.new(experiment_id), "Experiment #{experiment_id} is not found" end var_by_exp = rule_info.rule.experiment.get_variation_by_key(variation_key) forced_variation = DataManager::ForcedExperimentVariation.new(rule_info.rule, var_by_exp, force_targeting) @visitor_manager.add_data(visitor_code, forced_variation) end Logging::KameleoonLogger.info( "RETURN: KameleoonClient.set_forced_variation(visitor_code: '%s', experiment_id: %d, variation_key: %s, " \ 'force_targeting: %s)', visitor_code, experiment_id, variation_key.nil? ? 'nil' : "'#{variation_key}'", force_targeting ) end |
#set_legal_consent(visitor_code, consent, cookies = nil) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/kameleoon/kameleoon_client.rb', line 193 def (visitor_code, , = nil) Logging::KameleoonLogger.info( "CALL: KameleoonClient.set_legal_consent(visitor_code: '%s', consent: %s, cookies: %s)", visitor_code, , ) Utils::VisitorCode.validate(visitor_code) visitor = @visitor_manager.get_or_create_visitor(visitor_code) visitor. = ? DataManager::LegalConsent::GIVEN : DataManager::LegalConsent::NOT_GIVEN @cookie_manager.update(visitor_code, , ) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.set_legal_consent(visitor_code: '%s', consent: %s, cookies: %s)", visitor_code, , ) end |
#track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: nil, negative: false, metadata: nil) ⇒ 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.
identifier. This field is optional.
This field is optional (false by default).
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/kameleoon/kameleoon_client.rb', line 253 def track_conversion( visitor_code, goal_id, revenue = 0.0, is_unique_identifier: nil, negative: false, metadata: nil ) Logging::KameleoonLogger.info( "CALL: KameleoonClient.track_conversion(visitor_code: '%s', goal_id: %s, revenue: %s, " \ 'is_unique_identifier: %s, negative: %s, metadata: %s)', visitor_code, goal_id, revenue, is_unique_identifier, negative, ) Utils::VisitorCode.validate(visitor_code) set_unique_identifier(visitor_code, is_unique_identifier) unless is_unique_identifier.nil? add_data(visitor_code, Conversion.new(goal_id, revenue, negative, metadata: )) @tracking_manager.add_visitor_code(visitor_code) Logging::KameleoonLogger.info( "RETURN: KameleoonClient.track_conversion(visitor_code: '%s', goal_id: %s, revenue: %s, " \ 'is_unique_identifier: %s, negative: %s, metadata: %s)', visitor_code, goal_id, revenue, is_unique_identifier, negative, ) end |
#wait_init(timeout_millisecond = nil) ⇒ Boolean
Block until the SDK has loaded its configuration and is ready to use.
The method returns the result of the configuration fetch: true once the
SDK has been initialized, or false as soon as the fetch has failed
(without waiting for background retries) or when no fetch result is
available within the timeout. The failure which prevented the SDK from
initializing is reported to the log.
An expired timeout or a failed fetch does not affect the SDK state: the client keeps retrying in the background, and once the SDK becomes ready, a subsequent call returns true.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/kameleoon/kameleoon_client.rb', line 123 def wait_init(timeout_millisecond = nil) Logging::KameleoonLogger.info('CALL: KameleoonClient.wait_init(timeout_millisecond: %s)', timeout_millisecond) if timeout_millisecond.nil? || timeout_millisecond.negative? timeout_millisecond = @config.default_timeout_millisecond end error = @readiness.wait_with_timeout(timeout_millisecond / 1000.0) if error.nil? Logging::KameleoonLogger.info('Kameleoon is initialized') else Logging::KameleoonLogger.error('Kameleoon failed to initialize due to error: %s', error.) end result = error.nil? Logging::KameleoonLogger.info('RETURN: KameleoonClient.wait_init -> (result: %s)', result) result end |