Class: VWO

Inherits:
Object
  • Object
show all
Includes:
Common::CONSTANTS, Common::CampaignUtils, Common::Enums, Common::ImpressionUtils, Common::Validations
Defined in:
lib/vwo.rb,
lib/vwo/common/enums.rb,
lib/vwo/common/utils.rb,
lib/vwo/get_settings.rb,
lib/vwo/user_profile.rb,
lib/vwo/custom_logger.rb,
lib/vwo/common/requests.rb,
lib/vwo/common/constants.rb,
lib/vwo/decision_service.rb,
lib/vwo/event_dispatcher.rb,
lib/vwo/bucketing_service.rb,
lib/vwo/common/uuid_utils.rb,
lib/vwo/common/validations.rb,
lib/vwo/common/campaign_utils.rb,
lib/vwo/common/function_utils.rb,
lib/vwo/project_config_manager.rb,
lib/vwo/common/impression_utils.rb,
lib/vwo/common/schemas/settings_file.rb

Overview

Utility module for manipulating VWO campaigns

Defined Under Namespace

Modules: Common Classes: BucketingService, CustomLogger, DecisionService, EventDispatcher, GetSettings, ProjectConfigManager, UserProfile

Constant Summary collapse

FILE =
FileNameEnum::VWO

Constants included from Common::CONSTANTS

Common::CONSTANTS::API_VERSION, Common::CONSTANTS::HTTPS_PROTOCOL, Common::CONSTANTS::HTTP_PROTOCOL, Common::CONSTANTS::LIBRARY_PATH, Common::CONSTANTS::MAX_TRAFFIC_PERCENT, Common::CONSTANTS::MAX_TRAFFIC_VALUE, Common::CONSTANTS::PLATFORM, Common::CONSTANTS::SDK_VERSION, Common::CONSTANTS::SEED_VALUE, Common::CONSTANTS::STATUS_RUNNING, Common::CONSTANTS::URL_NAMESPACE

Constants included from Common::UUIDUtils

Common::UUIDUtils::VWO_NAMESPACE

Constants included from Common::Validations

Common::Validations::UTILITIES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Common::ImpressionUtils

#build_event

Methods included from Common::UUIDUtils

#generate, #generator_for, parse, uuid_v5

Methods included from Common::FunctionUtils

#get_current_unix_timestamp, #get_random_number

Methods included from Common::CampaignUtils

#get_campaign, #get_campaign_goal, #set_variation_allocation

Methods included from Common::Validations

#valid_hash?, #valid_number?, #valid_settings_file?, #valid_string?, #valid_utility?, #valid_value?

Constructor Details

#initialize(account_id, sdk_key, logger = nil, user_profile_service = nil, is_development_mode = false, settings_file = nil) ⇒ VWO

VWO init method for managing custom projects. Setting various services on the instance To be accessible by its member functions

@param :account_id Account Id in VWO @param :sdk_key Unique sdk key for user,

can be retrieved from our website

@param :logger Optional component which provides a log method

to log messages. By default everything would be logged.

@param :user_profile_service Optional component which provides

methods to store and manage user profiles.

@param :is_development_mode To specify whether the request

to our server should be sent or not.

@param :settings_file Settings File Content if already present



41
42
43
44
45
46
47
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
90
91
92
93
94
95
96
97
# File 'lib/vwo.rb', line 41

def initialize(
  ,
  sdk_key,
  logger = nil,
   = nil,
  is_development_mode = false,
  settings_file = nil
)
  @account_id = 
  @sdk_key = sdk_key
  @user_profile_service = 
  @is_development_mode = is_development_mode

  # Verify and assign a/the logger (Pending)
  @logger = CustomLogger.get_instance(logger)
  # Verify the settings_file for json object and correct schema

  unless valid_settings_file?(get_settings(settings_file))
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::SETTINGS_FILE_CORRUPTED, file: FILE)
    )
    @is_valid = false
    return
  end
  @is_valid = true

  # Initialize the ProjectConfigManager if settings_file provided is valid
  @config = VWO::ProjectConfigManager.new(get_settings)

  @logger.log(
    LogLevelEnum::DEBUG,
    format(LogMessageEnum::DebugMessages::VALID_CONFIGURATION, file: FILE)
  )

  # Process the settings file
  @config.process_settings_file
  @settings_file = @config.get_settings_file

  # Assign DecisionService to vwo
  @decision_service = DecisionService.new(@settings_file, )

  # Assign event dispatcher
  if is_development_mode
    @logger.log(
      LogLevelEnum::DEBUG,
      format(LogMessageEnum::DebugMessages::SET_DEVELOPMENT_MODE, file: FILE)
    )
  end
  @event_dispatcher = EventDispatcher.new(is_development_mode)

  # Log successfully initialized SDK
  @logger.log(
    LogLevelEnum::DEBUG,
    format(LogMessageEnum::DebugMessages::SDK_INITIALIZED, file: FILE)
  )
end

Instance Attribute Details

#is_validObject

Returns the value of attribute is_valid.



16
17
18
# File 'lib/vwo.rb', line 16

def is_valid
  @is_valid
end

Instance Method Details

#activate(campaign_test_key, user_id) ⇒ Object

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 userProfileService 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 :campaign_test_key Unique campaign test key @param :user_id ID assigned to a user @return If variation is assigned then variation-name

otherwise None in case of user not becoming part


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/vwo.rb', line 123

def activate(campaign_test_key, user_id)
  # Validate input parameters
  unless valid_string?(campaign_test_key) && valid_string?(user_id)
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::ACTIVATE_API_MISSING_PARAMS, file: FILE)
    )
    return
  end

  # Validate project config manager
  unless @is_valid
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::ACTIVATE_API_CONFIG_CORRUPTED, file: FILE)
    )
    return
  end

  # Get the campaign settings
  campaign = get_campaign(@settings_file, campaign_test_key)

  # Validate campaign
  unless campaign && campaign['status'] == STATUS_RUNNING
    # Campaign is invalid
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::CAMPAIGN_NOT_RUNNING, file: FILE, campaign_test_key: campaign_test_key, api: 'activate')
    )
    return
  end

  # Once the matching RUNNING campaign is found, assign the
  # deterministic variation to the user_id provided
  variation_id, variation_name = @decision_service.get(
    user_id,
    campaign,
    campaign_test_key
  )

  # Check if variation_name has been assigned
  unless valid_value?(variation_name)
    # log invalid variation key
    @logger.log(
      LogLevelEnum::INFO,
      format(LogMessageEnum::InfoMessages::INVALID_VARIATION_KEY, file: FILE, user_id: user_id, campaign_test_key: campaign_test_key)
    )
    return
  end

  # Variation found, dispatch log to DACDN
  properties = build_event(
    @settings_file,
    campaign['id'],
    variation_id,
    user_id
  )
  @event_dispatcher.dispatch(properties)
  variation_name
end

#get_settings(settings_file = nil) ⇒ Object

VWO get_settings method to get settings for a particular account_id It will memoize the settings to avoid another http call on re-invocation of this method



101
102
103
104
105
# File 'lib/vwo.rb', line 101

def get_settings(settings_file = nil)
  @settings ||=
    settings_file || VWO::GetSettings.new(@account_id, @sdk_key).get
  @settings
end

#get_variation(campaign_test_key, user_id) ⇒ Object

This API method: Gets the variation assigned for the user for the campaign

  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 user_id), If user becomes part of campaign If userProfileService is used, it will look into it for the variation and if found, no further processing is done

@param :campaign_test_key Unique campaign test key @param :user_id ID assigned to a user

@@return If variation is assigned then variation-name

Otherwise null in case of user not becoming part


200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
# File 'lib/vwo.rb', line 200

def get_variation(campaign_test_key, user_id)
  # Check for valid arguments
  unless valid_string?(campaign_test_key) && valid_string?(user_id)
    # log invalid params
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::GET_VARIATION_API_MISSING_PARAMS, file: FILE)
    )
    return
  end

  # Validate project config manager
  unless @is_valid
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::ACTIVATE_API_CONFIG_CORRUPTED, file: FILE)
    )
    return
  end

  # Get the campaign settings
  campaign = get_campaign(@settings_file, campaign_test_key)

  # Validate campaign
  if campaign.nil? || campaign['status'] != STATUS_RUNNING
    # log campaigns invalid
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::CAMPAIGN_NOT_RUNNING, file: FILE, campaign_test_key: campaign_test_key, api: 'get_variation')
    )
    return
  end

  _variation_id, variation_name = @decision_service.get(
    user_id,
    campaign,
    campaign_test_key
  )

  # Check if variation_name has been assigned
  unless valid_value?(variation_name)
    # log invalid variation key
    @logger.log(
      LogLevelEnum::INFO,
      format(LogMessageEnum::InfoMessages::INVALID_VARIATION_KEY, file: FILE, user_id: user_id, campaign_test_key: campaign_test_key)
    )
    return
  end

  variation_name
end

#track(campaign_test_key, user_id, goal_identifier, *args) ⇒ Object

This API method: Marks the conversion of the campaign for a particular goal

  1. validates the arguments being passed

  2. Checks if user is eligible to get bucketed into the campaign,

  3. Gets the assigned deterministic variation to the

    user(based on user_d), if user becomes part of campaign
    
  4. Sends an impression call to VWO server to track goal data

@param :campaign_test_key Unique campaign test key @param :user_id ID assigned to a user @param :goal_identifier Unique campaign’s goal identifier @param :revenue_value Revenue generated on triggering the goal



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/vwo.rb', line 265

def track(campaign_test_key, user_id, goal_identifier, *args)
  if args.is_a?(Array)
    revenue_value = args[0]
  elsif args.is_a?(Hash)
    revenue_value = args['revenue_value']
  end

  # Check for valid args
  unless valid_string?(campaign_test_key) && valid_string?(user_id) && valid_string?(goal_identifier)
    # log invalid params
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::TRACK_API_MISSING_PARAMS, file: FILE)
    )
    return false
  end

  unless @is_valid
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::ACTIVATE_API_CONFIG_CORRUPTED, file: FILE)
    )
    return false
  end

  # Get the campaign settings
  campaign = get_campaign(@settings_file, campaign_test_key)

  # Validate campaign
  if campaign.nil? || campaign['status'] != STATUS_RUNNING
    # log error
    @logger.log(
      LogLevelEnum::ERROR,
      format(LogMessageEnum::ErrorMessages::CAMPAIGN_NOT_RUNNING, file: FILE, campaign_test_key: campaign_test_key, api: 'track')
    )
    return false
  end

  campaign_id = campaign['id']
  variation_id, variation_name = @decision_service.get_variation_allotted(user_id, campaign)

  if variation_name
    goal = get_campaign_goal(@settings_file, campaign['key'], goal_identifier)

    if goal.nil?
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::TRACK_API_GOAL_NOT_FOUND,
          file: FILE, goal_identifier: goal_identifier,
          user_id: user_id,
          campaign_test_key: campaign_test_key
        )
      )
      return false
    elsif goal['type'] == GOALTYPES::REVENUE && !valid_value?(revenue_value)
      @logger.log(
        LogLevelEnum::ERROR,
        format(
          LogMessageEnum::ErrorMessages::TRACK_API_REVENUE_NOT_PASSED_FOR_REVENUE_GOAL,
          file: FILE,
          user_id: user_id,
          goal_identifier: goal_identifier,
          campaign_test_key: campaign_test_key
        )
      )
      return false
    end

    revenue_value = nil if goal['type'] == GOALTYPES::CUSTOM

    properties = build_event(
      @settings_file,
      campaign_id,
      variation_id,
      user_id,
      goal['id'],
      revenue_value
    )
    @event_dispatcher.dispatch(properties)
    return true
  end
  false
end