lib/vwo.rb
# This API method: Gets the variation assigned for the user
# For the campaign and send the metrics to VWO server
#
# 1. Validates the arguments being passed
# 2. Checks if user is eligible to get bucketed into the campaign,
# 3. Assigns the deterministic variation to the user(based on userId),
#    If user becomes part of campaign
#    If UserStorage is used, it will look into it for the
#    Variation and if found, no further processing is done
# 4. Sends an impression call to VWO server to track user
#
# @param[String]            :campaign_key  Unique campaign key
# @param[String]            :user_id            ID assigned to a user
# @param[Hash]              :options            Options for custom variables required for segmentation
# @return[String|None]                          If variation is assigned then variation-name
#                                               otherwise null in case of user not becoming part
This API method: Gets the variation assigned for the user
For the campaign and send the metrics to VWO server

1. Validates the arguments being passed
2. Checks if user is eligible to get bucketed into the campaign,
3. Assigns the deterministic variation to the user(based on userId),
If user becomes part of campaign
If UserStorage is used, it will look into it for the
Variation and if found, no further processing is done
4. Sends an impression call to VWO server to track user
This API method : Gets the variation assigned for the user For the campaign and send the metrics to VWO server 1 . Validates the arguments being passed 2 . Checks if user is eligible to get bucketed into the campaign 3 . Assigns the deterministic variation to the user ( based on userId ) If user becomes part of campaign If UserStorage is used it will look into it for the Variation and if found no further processing is done 4 . Sends an impression call to VWO server to track user
def activate(campaign_key, user_id, options = {})
    if is_opted_out(ApiMethods::ACTIVATE)
      return nil
    end
    unless @is_instance_valid
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::API_CONFIG_CORRUPTED,
          file: FILE,
          api_name: ApiMethods::ACTIVATE
        )
      )
      return
    end
    options = convert_to_symbol_hash(options)
    custom_variables = options[:custom_variables]
    variation_targeting_variables = options[:variation_targeting_variables]
    unless valid_string?(campaign_key) && valid_string?(user_id) && (custom_variables.nil? || valid_hash?(custom_variables)) &&
      (variation_targeting_variables.nil? || valid_hash?(variation_targeting_variables))
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::ACTIVATE_API_MISSING_PARAMS,
          api_name: ApiMethods::ACTIVATE,
          file: FILE
        )
      )
      return
    end
    campaign = get_campaign(@settings_file, campaign_key)
    unless campaign && campaign['status'] == STATUS_RUNNING
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::CAMPAIGN_NOT_RUNNING,
          file: FILE,
          campaign_key: campaign_key,
          api_name: ApiMethods::ACTIVATE
        )
      )
      return
    end
    campaign_type = campaign['type']
    if campaign_type != CampaignTypes::VISUAL_AB
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::INVALID_API,
          file: FILE,
          api_name: ApiMethods::ACTIVATE,
          user_id: user_id,
          campaign_key: campaign_key,
          campaign_type: campaign_type
        )
      )
      return
    end
    variation = @variation_decider.get_variation(
      user_id,
      campaign,
      ApiMethods::ACTIVATE,
      campaign_key,
      custom_variables,
      variation_targeting_variables
    )
    if variation.nil?
      @logger.log(
        LogLevelEnum::INFO,
        format(
          LogMessageEnum::InfoMessages::INVALID_VARIATION_KEY,
          file: FILE,
          user_id: user_id,
          campaign_key: campaign_key
        )
      )
      return
    end
    if is_eligible_to_send_impression()
      if defined?(@batch_events)
        impression = create_bulk_event_impression(
          @settings_file,
          campaign['id'],
          variation['id'],
          user_id
        )
        @batch_events_queue.enqueue(impression)
      elsif is_event_arch_enabled
        properties = get_events_base_properties(@settings_file, EventEnum::VWO_VARIATION_SHOWN, @usage_stats.usage_stats)
        payload = get_track_user_payload_data(@settings_file, user_id, EventEnum::VWO_VARIATION_SHOWN, campaign['id'], variation['id'])
        @event_dispatcher.dispatch_event_arch_post(properties, payload)
      else
        impression = create_impression(
          @settings_file,
          campaign['id'],
          variation['id'],
          user_id,
          @sdk_key,
          nil, 
          nil, 
          usage_stats: @usage_stats.usage_stats
        )
        if @event_dispatcher.dispatch(impression)
          @logger.log(
            LogLevelEnum::INFO,
            format(
              LogMessageEnum::InfoMessages::IMPRESSION_SUCCESS,
              file: FILE,
              account_id: @account_id,
              campaign_id: campaign['id'],
              variation_id: variation['id'],
              end_point: EVENTS::TRACK_USER
            )
          )
        end
      end
    else
      @logger.log(
        LogLevelEnum::INFO,
        format(
          LogMessageEnum::InfoMessages::USER_ALREADY_TRACKED,
          file: FILE,
          user_id: user_id,
          campaign_key: campaign_key,
          api_name: ApiMethods::ACTIVATE
        )
      )
    end
    variation['name']
  rescue StandardError => e
    @logger.log(
      LogLevelEnum::ERROR,
      format(
        LogMessageEnum::ErrorMessages::API_NOT_WORKING,
        file: FILE,
        api_name: ApiMethods::ACTIVATE,
        exception: e
      )
    )
    e
  end
def activate ( campaign_key , user_id , options = { } ) if is_opted_out ( ApiMethods :: ACTIVATE ) return nil end unless @is_instance_valid @logger . log ( LogLevelEnum :: ERROR , format ( LogMessageEnum :: ErrorMessages :: API_CONFIG_CORRUPTED , file : FILE , api_name : ApiMethods :: ACTIVATE ) ) return end options = convert_to_symbol_hash ( options ) custom_variables = options [ :custom_variables ] variation_targeting_variables = options [ :variation_targeting_variables ] unless valid_string? ( campaign_key ) && valid_string? ( user_id ) && ( custom_variables . nil? || valid_hash? ( custom_variables ) ) && ( variation_targeting_variables . nil? || valid_hash? ( variation_targeting_variables ) ) @logger . log ( LogLevelEnum :: ERROR , format ( LogMessageEnum :: ErrorMessages :: ACTIVATE_API_MISSING_PARAMS , api_name : ApiMethods :: ACTIVATE , file : FILE ) ) return end campaign = get_campaign ( @settings_file , campaign_key ) unless campaign && campaign [ 'status' ] == STATUS_RUNNING @logger . log ( LogLevelEnum :: ERROR , format ( LogMessageEnum :: ErrorMessages :: CAMPAIGN_NOT_RUNNING , file : FILE , campaign_key : campaign_key , api_name : ApiMethods :: ACTIVATE ) ) return end campaign_type = campaign [ 'type' ] if campaign_type != CampaignTypes :: VISUAL_AB @logger . log ( LogLevelEnum :: ERROR , format ( LogMessageEnum :: ErrorMessages :: INVALID_API , file : FILE , api_name : ApiMethods :: ACTIVATE , user_id : user_id , campaign_key : campaign_key , campaign_type : campaign_type ) ) return end variation = @variation_decider . get_variation ( user_id , campaign , ApiMethods :: ACTIVATE , campaign_key , custom_variables , variation_targeting_variables ) if variation . nil? @logger . log ( LogLevelEnum :: INFO , format ( LogMessageEnum :: InfoMessages :: INVALID_VARIATION_KEY , file : FILE , user_id : user_id , campaign_key : campaign_key ) ) return end if is_eligible_to_send_impression ( ) if defined? ( @batch_events ) impression = create_bulk_event_impression ( @settings_file , campaign [ 'id' ] , variation [ 'id' ] , user_id ) @batch_events_queue . enqueue ( impression ) elsif is_event_arch_enabled properties = get_events_base_properties ( @settings_file , EventEnum :: VWO_VARIATION_SHOWN , @usage_stats . usage_stats ) payload = get_track_user_payload_data ( @settings_file , user_id , EventEnum :: VWO_VARIATION_SHOWN , campaign [ 'id' ] , variation [ 'id' ] ) @event_dispatcher . dispatch_event_arch_post ( properties , payload ) else impression = create_impression ( @settings_file , campaign [ 'id' ] , variation [ 'id' ] , user_id , @sdk_key , nil , nil , usage_stats : @usage_stats . usage_stats ) if @event_dispatcher . dispatch ( impression ) @logger . log ( LogLevelEnum :: INFO , format ( LogMessageEnum :: InfoMessages :: IMPRESSION_SUCCESS , file : FILE , account_id : @account_id , campaign_id : campaign [ 'id' ] , variation_id : variation [ 'id' ] , end_point : EVENTS :: TRACK_USER ) ) end end else @logger . log ( LogLevelEnum :: INFO , format ( LogMessageEnum :: InfoMessages :: USER_ALREADY_TRACKED , file : FILE , user_id : user_id , campaign_key : campaign_key , api_name : ApiMethods :: ACTIVATE ) ) end variation [ 'name' ] rescue StandardError => e @logger . log ( LogLevelEnum :: ERROR , format ( LogMessageEnum :: ErrorMessages :: API_NOT_WORKING , file : FILE , api_name : ApiMethods :: ACTIVATE , exception : e ) ) e end
This API method: Gets the variation assigned for the user
For the campaign and send the metrics to VWO server
This API method : Gets the variation assigned for the user For the campaign and send the metrics to VWO server
# Retrieve custom variables # Validate input parameters # Get the campaign settings # Validate campaign # Log Campaign as invalid # Get campaign type # Validate valid api call # Once the matching RUNNING campaign is found, assign the # deterministic variation to the user_id provided # Check if variation_name has been assigned # Variation found, dispatch it to server # goal_id # revenue
campaign_key
<not_specific>
user_id
<not_specific>
options
<not_specific>
param[String]
:campaign_key Unique campaign key
: campaign_key Unique campaign key
param[String]
:user_id ID assigned to a user
: user_id ID assigned to a user
param[Hash]
:options Options for custom variables required for segmentation
: options Options for custom variables required for segmentation
return[String|None]
If variation is assigned then variation-name otherwise null in case of user not becoming part
If variation is assigned then variation - name otherwise null in case of user not becoming part