Class: Kameleoon::KameleoonClient

Inherits:
Object
  • Object
show all
Includes:
Exception
Defined in:
lib/kameleoon/kameleoon_client.rb

Overview

Client for Kameleoon

Defined Under Namespace

Classes: EvaluatedExperiment

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_code, config) ⇒ KameleoonClient

You should create KameleoonClient with the Client Factory only.



53
54
55
56
57
58
59
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
# File 'lib/kameleoon/kameleoon_client.rb', line 53

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
  @real_time_configuration_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)
  @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)
  )
  @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
  @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

  ObjectSpace.define_finalizer(self, method(:dispose))
  Logging::KameleoonLogger.info("RETURN: KameleoonClient.new(site_code: '%s', config: %s)",
                                site_code, config)
end

Instance Attribute Details

#site_codeObject (readonly)

Returns the value of attribute site_code.



48
49
50
# File 'lib/kameleoon/kameleoon_client.rb', line 48

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.

Parameters:

  • visitor_code (String)

    Visitor code

  • data (...Data)

    Data to associate with the visitor code

Raises:



169
170
171
172
173
174
175
176
177
178
# File 'lib/kameleoon/kameleoon_client.rb', line 169

def add_data(visitor_code, *args)
  Logging::KameleoonLogger.info("CALL: KameleoonClient.add_data(visitor_code: '%s', args: %s)",
                                visitor_code, args)
  Utils::VisitorCode.validate(visitor_code)
  @visitor_manager.add_data(visitor_code, *args)
  Logging::KameleoonLogger.info(
    "RETURN: KameleoonClient.add_data(visitor_code: '%s', args: %s)",
    visitor_code, args
  )
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.

Parameters:

  • visitor_code (String)

    The unique visitor code identifying the visitor.

Raises:



785
786
787
788
789
790
791
792
793
794
795
796
797
# File 'lib/kameleoon/kameleoon_client.rb', line 785

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).

Parameters:

  • visitor_code (String)

    Unique identifier of the user. This field is mandatory.

  • feature_key (String)

    Key of the feature flag you want to expose to a user. This field is mandatory.

  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

  • track (Bool) (defaults to: true)

    Optional flag indicating whether tracking of the feature evaluation is enabled

Returns:

  • (Bool)

    If the user has not been associated with your feature flag before, the SDK returns

Raises:



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/kameleoon/kameleoon_client.rb', line 292

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

Parameters:

  • visitor_code (String) (defaults to: nil)

    Optional field - Visitor code, without visitor code it flush all of the data

  • instant (Bool) (defaults to: false)

    A boolean flag indicating whether the data should be sent instantly (‘true`) or

  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

Raises:



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/kameleoon/kameleoon_client.rb', line 236

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.

Returns:

  • (Array)

    array of active feature flag keys for a visitor

Raises:



630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
# File 'lib/kameleoon/kameleoon_client.rb', line 630

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|
    next unless feature_flag.environment_enabled

    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.

Parameters:

  • visitor_code (String)

    unique identifier of a visitor.

Returns:

  • (Hash)

    Hash of active features for a visitor

Raises:



669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/kameleoon/kameleoon_client.rb', line 669

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|
    next unless feature_flag.environment_enabled

    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_fileKameleoon::Types::DataFile

Retrieves the current SDK configuration (also known as the data file), containing all feature flags and their variations.

Returns:



804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
# File 'lib/kameleoon/kameleoon_client.rb', line 804

def get_data_file
  Logging::KameleoonLogger.info('CALL: KameleoonClient.get_data_file')

  feature_flags_map = @data_manager.data_file.feature_flags.transform_values do |source_feature_flag|
    # Collect variations
    variations_map = source_feature_flag.variations.each_with_object({}) do |variation, variations_map|
      variables_map = variation.variables.each_with_object({}) do |var, variables_map|
        variables_map[var.key] = Types::Variable.new(var.key, var.type, _parse_feature_variable(var))
      end
      variations_map[variation.key] = Types::Variation.new(variation.key, nil, nil, variables_map, variation.name)
    end

    # Collect rules
    rules = source_feature_flag.rules.map do |rule|
      rule_variations = rule.experiment.variations_by_exposition.each_with_object({}) do |var_by_exp, rule_variations|
        base_variation = variations_map[var_by_exp.variation_key]
        next unless base_variation
        rule_variations[base_variation.key] = Types::Variation.new(
          base_variation.key,
          var_by_exp.variation_id,
          rule.experiment.id,
          base_variation.variables,
          base_variation.name
        )
      end
      Types::Rule.new(rule_variations)
    end

    Types::FeatureFlag.new(
      variations_map,
      source_feature_flag.environment_enabled,
      rules,
      source_feature_flag.default_variation_key
    )
  end

  data_file = Types::DataFile.new(feature_flags_map)
  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.

Parameters:

  • visitor_code (String)

    The user’s unique identifier. This field is mandatory.

Returns:

  • (String)

    JavasScript code to be inserted in your page to send automatically



721
722
723
724
725
726
727
728
729
730
# File 'lib/kameleoon/kameleoon_client.rb', line 721

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_listArray

Returns a list of all feature flag keys

Returns:

  • (Array)

    array of all feature flag keys



615
616
617
618
619
620
# File 'lib/kameleoon/kameleoon_client.rb', line 615

def get_feature_list # rubocop:disable Naming/AccessorMethodName
  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.

Parameters:

  • visitor_code (String)
  • feature_key (String)
  • variable_name (String)
  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

Raises:



453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/kameleoon/kameleoon_client.rb', line 453

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 = _parse_feature_variable(variable)
  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.

Parameters:

  • visitor_code (String)
  • feature_key (String)
  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

Raises:



414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
# File 'lib/kameleoon/kameleoon_client.rb', line 414

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.

Parameters:

  • feature_key (String)
  • variation_key (String)

Raises:



495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/kameleoon/kameleoon_client.rb', line 495

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)
  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) }
  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.

Parameters:

  • key (String)

    Key you want to retrieve data. This field is mandatory.

  • timeout (Integer) (defaults to: @default_timeout)

    Timeout for request (in milliseconds). Equals default_timeout in a config file.

Returns:

  • (Hash)

    Hash object of the json object.



531
532
533
534
535
536
537
538
539
540
# File 'lib/kameleoon/kameleoon_client.rb', line 531

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.

Parameters:

  • visitor_code (String)

    The visitor code for which you want to retrieve the assigned data.

  • add_data (Bool) (defaults to: true)

    A boolean indicating whether the method should automatically add retrieved data

  • timeout (Integer) (defaults to: nil)

    Timeout for request (in milliseconds). Equals default_timeout in a config file.

  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

Returns:

  • (Array)

    An array of data assigned to the given visitor.



558
559
560
561
562
563
564
565
566
567
568
569
570
571
# File 'lib/kameleoon/kameleoon_client.rb', line 558

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.

Parameters:

  • visitor_code (String)

    The unique identifier of the visitor.

  • feature_key (String)

    The unique identifier of the feature flag.

  • track (Bool) (defaults to: true)

    Optional flag indicating whether tracking of the feature evaluation is enabled

Returns:

Raises:



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/kameleoon/kameleoon_client.rb', line 330

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`).

Parameters:

  • visitor_code (String)

    The unique identifier of the visitor.

  • only_active (Bool) (defaults to: false)

    Optional flag indicating whether to return only variations for active feature

  • track (Bool) (defaults to: true)

    Optional flag indicating whether tracking of the feature evaluation is enabled

Returns:

  • (Hash)

    A hash consisting of feature flag keys as keys (‘String`) and their corresponding

Raises:



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
# File 'lib/kameleoon/kameleoon_client.rb', line 364

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|
    next unless feature_flag.environment_enabled

    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

Note:

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’)

Parameters:

  • cookies (Hash)

    Cookies of the request.

  • top_level_domain (String)

    Top level domain of your website, settled while writing cookie.

  • default_visitor_code (String) (defaults to: nil)
    • Optional - Define your default visitor_code (maximum length 100 chars).

Returns:

  • (String)

    visitor code



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/kameleoon/kameleoon_client.rb', line 129

def get_visitor_code(cookies, default_visitor_code = nil)
  Logging::KameleoonLogger.info(
    "CALL: KameleoonClient.get_visitor_code(cookies: %s, default_visitor_code: '%s')", cookies, default_visitor_code
  )
  visitor_code = @cookie_manager.get_or_add(cookies, default_visitor_code)
  Logging::KameleoonLogger.info(
    "RETURN: KameleoonClient.get_visitor_code(cookies: %s, default_visitor_code: '%s') -> (visitor_code: '%s')",
    cookies, 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 <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.

Parameters:

  • visitor_code (String)

    A unique visitor identification string, can’t exceed 255 characters length.

  • custom_data_index (Integer)

    An integer representing the index of the custom data you want to use to target

  • warehouse_key (String) (defaults to: nil)

    A key to identify the warehouse data, typically your internal user ID.

  • timeout (Integer) (defaults to: nil)

    Timeout for request (in milliseconds). Equals default_timeout in a config file.

Returns:

  • (Kameleoon::CustomData)

    A ‘CustomData` instance confirming that the data has been added to the visitor.

Raises:



596
597
598
599
600
601
602
603
604
605
606
607
608
609
# File 'lib/kameleoon/kameleoon_client.rb', line 596

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

#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.

Parameters:

  • handler (Callable | NilClass)

    The handler that will be called when the configuration



708
709
710
711
# File 'lib/kameleoon/kameleoon_client.rb', line 708

def on_update_configuration(handler)
  Logging::KameleoonLogger.info('CALL/RETURN: KameleoonClient.on_update_configuration(handler)')
  @update_configuration_handler = handler
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.

Parameters:

  • visitor_code (String)

    The unique visitor code identifying the visitor.

  • experiment_id (Integer)

    The identifier of the experiment you want to set/reset the forced variation for.

  • variation_key (String | NilClass)

    The identifier of the variation you want the experiment to be evaluated

  • force_targeting (Bool) (defaults to: true)

    If ‘true`, the visitor will be targeted to the experiment regardless its

Raises:



751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/kameleoon/kameleoon_client.rb', line 751

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


141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/kameleoon/kameleoon_client.rb', line 141

def set_legal_consent(visitor_code, consent, cookies = nil)
  Logging::KameleoonLogger.info(
    "CALL: KameleoonClient.set_legal_consent(visitor_code: '%s', consent: %s, cookies: %s)",
    visitor_code, consent, cookies
  )
  Utils::VisitorCode.validate(visitor_code)
  visitor = @visitor_manager.get_or_create_visitor(visitor_code)
  visitor.legal_consent = consent ? DataManager::LegalConsent::GIVEN : DataManager::LegalConsent::NOT_GIVEN
  @cookie_manager.update(visitor_code, consent, cookies)
  Logging::KameleoonLogger.info(
    "RETURN: KameleoonClient.set_legal_consent(visitor_code: '%s', consent: %s, cookies: %s)",
    visitor_code, consent, cookies
  )
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).

Parameters:

  • visitor_code (String)

    Visitor code

  • goal_id (Integer)

    Id of the goal

  • revenue (Float) (defaults to: 0.0)

    Optional - Revenue of the conversion.

  • is_unique_identifier(DEPRECATED) (Bool)

    Parameter that specifies whether the visitorCode is a unique

  • negative (Bool) (defaults to: false)

    Defines if the revenue is positive or negative.

  • metadata (Array) (defaults to: nil)

    Metadata of the conversion. This field is optional.

Raises:



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/kameleoon/kameleoon_client.rb', line 200

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_initObject



96
97
98
99
100
101
# File 'lib/kameleoon/kameleoon_client.rb', line 96

def wait_init
  Logging::KameleoonLogger.info('CALL: KameleoonClient.wait_init')
  result = @readiness.wait
  Logging::KameleoonLogger.info('RETURN: KameleoonClient.wait_init -> (result: %s)', result)
  result
end