Skip to content

wingify/vwo-ruby-sdk-ror-example

Repository files navigation

VWO Ruby SDK Example (Ruby on Rails)

This repository provides a basic demo of how server-side works with VWO Ruby SDK in ruby on rails app.

Documentation

Refer VWO Official Server-side Documentation

Setup

  1. Install dependencies
# Assuming ruby is installed
gem install bundler (sudo if required)
bundle install
  1. Update your app with your settings present

Run EDITOR=vi bin/rails credentials:edit

Add following -

account_id: REPLACE_THIS_WITH_CORRECT_VALUE
sdk_key: REPLACE_THIS_WITH_CORRECT_VALUE
  1. Run application
rails s

Basic usage

Importing and Instantiation

require 'vwo'

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

# Get Settings
vwo_client_instance.get_settings

# Activate API
variation_name = vwo_client_instance.activate(campaign_key, user_id)

# GetVariation API
variation_name = vwo_client_instance.get_variation(campaign_key, user_id)

# Track API
vwo_client_instance.track(campaign_key, user_id, goal_identified, revenue_value)

API usage

User Define Logger

Override Existing Logging

```
class VWO
  class Logger
    def initialize(logger_instance)
      # Only log info logs and above, no debug
      @@logger_instance = logger_instance || Logger.new(STDOUT, level: :info)
    end

    def log(level, message)
      # Basic Modification
      message = "#{Time.now} #{message}"
      @@logger_instance.log(level, message)
    end
  end
end
```

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

User Storage

To save a user you can override UserStorage methods. i.e -

```
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_profile)
    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_obj)
        # example code to save it in DB
       User.update_attributes(vwo_id: user_obj.userId, vwo_profile: JSON.generate(user_obj))
    end
  end
end

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

License

Apache License, Version 2.0

Copyright 2019 Wingify Software Pvt. Ltd.

About

Provides a basic demo of ROR app how server-side works with VWO Ruby SDK

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published