Class: Kameleoon::KameleoonClient

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

Overview

Client for Kameleoon

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site_code, config) ⇒ KameleoonClient

You should create KameleoonClient with the Client Factory only.



48
49
50
51
52
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
# File 'lib/kameleoon/kameleoon_client.rb', line 48

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



43
44
45
# File 'lib/kameleoon/kameleoon_client.rb', line 43

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:



163
164
165
166
167
168
169
170
171
172
# File 'lib/kameleoon/kameleoon_client.rb', line 163

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

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



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/kameleoon/kameleoon_client.rb', line 278

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:



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/kameleoon/kameleoon_client.rb', line 222

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:



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'lib/kameleoon/kameleoon_client.rb', line 613

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)
  list_keys = []
  @data_manager.data_file.feature_flags.each do |feature_key, feature_flag|
    next unless feature_flag.environment_enabled

    variation, rule, = _calculate_variation_key_for_feature(visitor_code, feature_flag)
    variation_key = _get_variation_key(variation, rule, feature_flag)
    list_keys.push(feature_key) if variation_key != Kameleoon::Configuration::VariationType::VARIATION_OFF
  end
  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:



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

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)
  map_active_features = {}

  @data_manager.data_file.feature_flags.each_value do |feature_flag|
    next unless feature_flag.environment_enabled

    var_by_exp, rule = _calculate_variation_key_for_feature(visitor_code, feature_flag)
    variation_key = _get_variation_key(var_by_exp, rule, feature_flag)

    next if variation_key == Configuration::VariationType::VARIATION_OFF

    variation = feature_flag.get_variation_by_key(variation_key)
    map_active_features[feature_flag.feature_key] =
      make_external_variation(variation, var_by_exp&.variation_id, rule&.experiment_id)
  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_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



693
694
695
696
697
698
699
700
701
702
# File 'lib/kameleoon/kameleoon_client.rb', line 693

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



598
599
600
601
602
603
# File 'lib/kameleoon/kameleoon_client.rb', line 598

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:



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/kameleoon/kameleoon_client.rb', line 436

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:



397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# File 'lib/kameleoon/kameleoon_client.rb', line 397

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:



478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/kameleoon/kameleoon_client.rb', line 478

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.



514
515
516
517
518
519
520
521
522
523
# File 'lib/kameleoon/kameleoon_client.rb', line 514

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.



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

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:



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/kameleoon/kameleoon_client.rb', line 316

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, var_by_exp, rule = get_variation_info(visitor_code, feature_flag, track)
  variation = feature_flag.get_variation_by_key(variation_key)
  external_variation = make_external_variation(variation, var_by_exp&.variation_id, rule&.experiment_id)
  @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:



350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# File 'lib/kameleoon/kameleoon_client.rb', line 350

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

    variation_key, var_by_exp, rule = get_variation_info(visitor_code, feature_flag, track)
    next if only_active && (variation_key == Configuration::VariationType::VARIATION_OFF)

    variation = feature_flag.get_variation_by_key(variation_key)
    variations[feature_flag.feature_key] =
      make_external_variation(variation, var_by_exp&.variation_id, rule&.experiment_id)
  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



124
125
126
127
128
129
130
131
132
133
# File 'lib/kameleoon/kameleoon_client.rb', line 124

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:



579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'lib/kameleoon/kameleoon_client.rb', line 579

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



680
681
682
683
# File 'lib/kameleoon/kameleoon_client.rb', line 680

def on_update_configuration(handler)
  Logging::KameleoonLogger.info('CALL/RETURN: KameleoonClient.on_update_configuration(handler)')
  @update_configuration_handler = handler
end


135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kameleoon/kameleoon_client.rb', line 135

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

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

Raises:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/kameleoon/kameleoon_client.rb', line 191

def track_conversion(visitor_code, goal_id, revenue = 0.0, is_unique_identifier: nil)
  Logging::KameleoonLogger.info(
    "CALL: KameleoonClient.track_conversion(visitor_code: '%s', goal_id: %s, revenue: %s, is_unique_identifier: %s)",
    visitor_code, goal_id, revenue, is_unique_identifier
  )
  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))
  @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)",
    visitor_code, goal_id, revenue, is_unique_identifier
  )
end

#wait_initObject



91
92
93
94
95
96
# File 'lib/kameleoon/kameleoon_client.rb', line 91

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