Skip to content

Cado-Labs/vwo-ruby-sdk

 
 

Repository files navigation

VWO Ruby SDK

Gem version CI codecov License

This open source library allows you to A/B Test your Website at server-side.

Requirements

  • Works with 2.2.10 onwards

Installation

gem install vwo-sdk

Basic usage

Importing and Instantiation

require 'vwo'

# Initialize client
vwo_client_instance = VWO.new(account_id, sdk_key)

# Initialize client with all parameters(explained in next section)
vwo_client_instance = VWO.new(account_id, sdk_key, custom_logger, UserStorage.new, true, settings_file)

# Get Settings
vwo_client_instance.get_settings

# Activate API
# With Custom Variables
options = { "custom_variable" => { "a" => "x"}}
variation_name = vwo_client_instance.activate(campaign_key, user_id, options)

# Without Custom Variables
options = {}
variation_name = vwo_client_instance.activate(campaign_key, user_id, options)

# GetVariation
# With Custom Variables
options = { "custom_variable" => { "a" => "x"}}
variation_name = vwo_client_instance.get_variation_name(campaign_key, user_id, options)

#Without Custom Variables
options = {}
variation_name = vwo_client_instance.get_variation_name(campaign_key, user_id, options)

# Track API
# With Custom Variables
options = { "custom_variable" => { "a" => "x"}}
is_successful = vwo_client_instance.track(campaign_key, user_id, goal_identifier, options)

# With Revenue Value
options = { "revenue_value" => 10.23}
is_successful = vwo_client_instance.track(campaign_key, user_id, goal_identifier, options)

# With both Custom Variables and Revenue Value
options = { "custom_variable" => { "a" => "x"}, "revenue_value" => 10.23}
is_successful = vwo_client_instance.track(campaign_key, user_id, goal_identifier, options)

# FeatureEnabled? API
# With Custom Varibles
options = { "custom_variable" => { "a" => "x"}}
is_successful = vwo_client_instance.feature_enabled?(campaign_key, user_id, options)

# Without Custom Variables
options = {}
is_successful = vwo_client_instance.feature_enabled?(campaign_key, user_id, options)

# GetFeatureVariableValue API
# With Custom Variables
options = { "custom_variable" => { "a" => "x"}}
variable_value = vwo_client_instance.get_feature_variable_value(campaign_key, variable_key, user_id, options)

# Without Custom Variables
options = {}
variable_value = vwo_client_instance.get_feature_variable_value(campaign_key, variable_key, user_id, options)

# Push API
is_successful = vwo_client_instance.push(tag_key, tag_value, user_id)
  1. account_id - Account for which sdk needs to be initialized
  2. sdk_key - SDK key for that account
  3. logger - If you need to pass your own logger. Check documentation below
  4. UserStorage.new - An object allowing get and set for maintaining user storage
  5. development_mode - on/off (true/false). Default - false
  6. settings_file - Settings file if already present during initialization. Its stringified JSON format.

API usage

User Defined Logger

There are two ways you can use your own custom logging

  1. Override Existing Logging

      class VWO::Logger
        def initialize(logger_instance)
          # Override this two create your own logging instance
          # Make sure log method is defined on it
          # i.e @@logger_instance = MyLogger.new(STDOUT)
          @@logger_instance = logger_instance || Logger.new(STDOUT)
        end
    
        # Override this method to handle logs in a custom manner
        def log(level, message)
          # Modify level & Message here
          # i.e message = "Custom message #{message}"
          @@logger_instance.log(level, message)
        end
      end
  2. Pass your own logger during client initialization

vwo_client_instance = VWO.new(account_id, sdk_key, user_defined_logger)

Note - Make sure your logger instance has log method which takes (level, message) as arguments.

User Storage

Use custom UserStorage

```ruby
class VWO
  # Abstract class encapsulating user storage service functionality.
  # Override with your own implementation for storing
  # And retrieving the user.

  class UserStorage

    # Abstract method, must be defined to fetch the
    # User storage dict corresponding to the user_id.
    #
    # @param[String]        :user_id            ID for user that needs to be retrieved.
    # @return[Hash]         :user_storage_obj   Object representing the user.
    #
    def get(user_id, campaign_key)
      # example code to fetch it from DB column
      JSON.parse(User.find_by(vwo_id: user_id).vwo_user)
    end

    # Abstract method, must be to defined to save
    # The user dict sent to this method.
    # @param[Hash]    :user_storage_obj     Object representing the user.
    #
    def set(user_data)
        # example code to save it in DB
       User.update_attributes(vwo_id: user_data.userId, vwo_user: JSON.generate(user_data))
    end
  end
end

# Now use it to initiate VWO client instance
vwo_client_instance = VWO.new(account_id, sdk_key, custom_logger, UserStorage.new)
```

Documentation

Refer Official VWO Documentation

Code syntax check

bundle exec rubocop lib

Setting up Local development environment

chmod +x ./start-dev.sh
bash start-dev.sh
gem install

Running Unit Tests

ruby tests/test_all_tests.rb

Third-party Resources and Credits

Refer third-party-attributions.txt

Authors

Changelog

Refer CHANGELOG.md

Contributing

Please go through our contributing guidelines

Code of Conduct

Code of Conduct

License

Apache License, Version 2.0

Copyright 2019-2022 Wingify Software Pvt. Ltd.

Packages

No packages published

Languages

  • Ruby 96.2%
  • JavaScript 3.8%