Giter Club home page Giter Club logo

vwo-ruby-sdk's Introduction

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.

vwo-ruby-sdk's People

Contributors

abbas-khaliq avatar dependabot[bot] avatar nakult avatar rajneeshsharma9 avatar sahilbathla avatar shubhamgupta14cse avatar softvar avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vwo-ruby-sdk's Issues

Setting `is_development_mode` allows request to the API

I recently started using VWO at work. We were pointed to this gem to do A/B Fullstack testing.

We are relying on the is_development_mode parameter to ensure we were not polluting our experiments with data from development and staging environments. Unfortunately, that doesn't seem to be the case. We noticed it when we started getting errors from VWO's API when running the web application in our development environments.

vwo-ruby-sdk/lib/vwo.rb

Lines 58 to 59 in 1d897ad

# @param[Boolean] :is_development_mode To specify whether the request
# to our server should be sent or not.

We got several errors when, for example, using the VWO client to activate a campaign. Even instantiating the VWO class will fire up a request to get the settings. This is understandable, but not ideal. It makes running automated tests where this class is instantiated more complicated than necessary; we are seeing errors being logged even if the client doesn't raise an error.

In this case, following the code, starting here:

unless valid_settings_file?(get_settings(settings_file))

Which takes us here:

settings_file || @settings_file_manager.get_settings_file

Which leads us to the client making a GET request:

settings_file_response = ::VWO::Utils::Request.get(vwo_server_url, params)

Would it be possible for this behavior to be changed to as advertised? We would expect no requests to be made when is_development_mode is set to true.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.