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

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

Parameters:

  • visitor_code (String)

    Visitor code

  • data (...Data)

    Data to associate with the visitor code

  • track (Bool) (defaults to: true)

    A boolean indicating whether the data should be tracked (sent to server). Default is true.

Raises:



184
185
186
187
188
189
190
191
192
193
# File 'lib/kameleoon/kameleoon_client.rb', line 184

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.

Parameters:

  • visitor_code (String)

    The unique visitor code identifying the visitor.

Raises:



799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'lib/kameleoon/kameleoon_client.rb', line 799

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:



307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/kameleoon/kameleoon_client.rb', line 307

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:



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/kameleoon/kameleoon_client.rb', line 251

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:



648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
# File 'lib/kameleoon/kameleoon_client.rb', line 648

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.

Parameters:

  • visitor_code (String)

    unique identifier of a visitor.

Returns:

  • (Hash)

    Hash of active features for a visitor

Raises:



685
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
711
712
713
# File 'lib/kameleoon/kameleoon_client.rb', line 685

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

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

Returns:



818
819
820
821
822
823
# File 'lib/kameleoon/kameleoon_client.rb', line 818

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.

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



735
736
737
738
739
740
741
742
743
744
# File 'lib/kameleoon/kameleoon_client.rb', line 735

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.

DEPRECATED. Please use get_data_file instead.

Returns:

  • (Array)

    array of all feature flag keys



630
631
632
633
634
635
636
637
638
# File 'lib/kameleoon/kameleoon_client.rb', line 630

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.

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:



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/kameleoon/kameleoon_client.rb', line 466

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.

Parameters:

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

    Parameter that specifies whether the visitorCode is a unique

Raises:



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/kameleoon/kameleoon_client.rb', line 427

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:



508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# File 'lib/kameleoon/kameleoon_client.rb', line 508

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.

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.



545
546
547
548
549
550
551
552
553
554
# File 'lib/kameleoon/kameleoon_client.rb', line 545

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.



572
573
574
575
576
577
578
579
580
581
582
583
584
585
# File 'lib/kameleoon/kameleoon_client.rb', line 572

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:



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/kameleoon/kameleoon_client.rb', line 345

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:



379
380
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
# File 'lib/kameleoon/kameleoon_client.rb', line 379

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

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



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

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

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:



610
611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/kameleoon/kameleoon_client.rb', line 610

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



722
723
724
725
# File 'lib/kameleoon/kameleoon_client.rb', line 722

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:



765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/kameleoon/kameleoon_client.rb', line 765

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


155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/kameleoon/kameleoon_client.rb', line 155

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:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/kameleoon/kameleoon_client.rb', line 215

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.

If the initial fetch fails the client keeps retrying in the background, so this method returns as soon as any fetch succeeds. The wait is always bounded by a timeout, so it never blocks indefinitely: when the timeout elapses before the configuration is loaded, the method returns false.

Parameters:

  • timeout_millisecond (Integer, nil) (defaults to: nil)

    Maximum time to wait, in milliseconds. When nil or negative, config.default_timeout_millisecond is applied.

Returns:

  • (Boolean)

    true if the SDK is ready, false if the timeout elapsed first.



107
108
109
110
111
112
113
114
115
# File 'lib/kameleoon/kameleoon_client.rb', line 107

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
  result = @readiness.wait(timeout_millisecond / 1000.0)
  Logging::KameleoonLogger.info('RETURN: KameleoonClient.wait_init -> (result: %s)', result)
  result
end