# Vanity [](https://rubygems.org/gems/vanity) [](https://github.com/assaf/vanity/actions) [](https://www.ruby-toolbox.com/projects/vanity) Vanity is an A/B testing framework for Rails that is datastore agnostic. * All about Vanity: http://vanity.labnotes.org * On Github: http://github.com/assaf/vanity [](http://github.com/assaf/vanity) - [Installation](#installation) - [Setup](#setup) * [Datastore](#datastore) + [Redis Setup](#redis-setup) + [MongoDB Setup](#mongodb-setup) + [SQL Database Setup](#sql-database-setup) + [Forking servers and reconnecting](#forking-servers-and-reconnecting) * [Initialization](#initialization) * [User identification](#user-identification) + [Rails](#rails) + [Other](#other) * [Define a A/B test](#define-a-ab-test) * [Present the different options to your users](#present-the-different-options-to-your-users) * [Measure conversion](#measure-conversion) * [Check the report](#check-the-report) + [Rails report dashboard](#rails-report-dashboard) - [Registering participants with Javascript](#registering-participants-with-javascript) - [Compatibility](#compatibility) - [Testing](#testing) - [Updating documentation](#updating-documentation) - [Contributing](#contributing) - [Credits/License](#creditslicense) ## Installation Add to your Gemfile: ```ruby gem "vanity" ``` (For support for older versions of Rails and Ruby 1.8, please see the [1.9.x branch](https://github.com/assaf/vanity/tree/1-9-stable).) ## Setup ### Datastore Choose a datastore that best fits your needs and preferences for storing experiment results. Choose one of: Redis, MongoDB or an SQL database. While Redis is usually faster, it may add additional complexity to your stack. Datastores should be configured using a `config/vanity.yml`. #### Redis Setup Add to your Gemfile: ```ruby gem "redis", ">= 3.2" gem "redis-namespace", ">= 1.1.0" ``` By default Vanity is configured to use Redis on localhost port 6379 with database 0. A sample `config/vanity.yml` might look like: ```yaml test: collecting: false production: adapter: redis url: redis://<%= ENV["REDIS_USER"] %>:<%= ENV["REDIS_PASSWORD"] %>@<%= ENV["REDIS_HOST"] %>:<%= ENV["REDIS_PORT"] %>/0 ``` If you want to use your test environment with RSpec you will need to add an adapter to test: ```yaml test: adapter: redis collecting: false ``` To re-use an existing redis connection, you can call `Vanity.connect!` explicitly, for example: ```ruby Vanity.connect!( adapter: :redis, redis: $redis ) ``` #### MongoDB Setup Add to your Gemfile: ```ruby gem "mongo", "~> 2.0" # For Mongo 1.x support see Vanity versions 2.1 and below. ``` A sample `config/vanity.yml` might look like: ```yaml development: adapter: mongodb database: analytics test: collecting: false production: adapter: mongodb database: analytics ``` #### SQL Database Setup Vanity supports multiple SQL stores (like MySQL, MariaDB, Postgres, Sqlite, etc.) using ActiveRecord, which is built into Rails. If you're using DataMapper, Sequel or another persistence framework, add to your Gemfile: ```ruby gem "active_record" ``` A sample `config/vanity.yml` might look like: ```yaml development: adapter: active_record active_record_adapter: sqlite3 database: db/development.sqlite3 test: adapter: active_record active_record_adapter: default collecting: false production: adapter: active_record active_record_adapter: postgresql <% uri = URI.parse(ENV['DATABASE_URL']) %> host: <%= uri.host %> username: <%= uri.user%> password: <%= uri.password %> port: <%= uri.port %> database: <%= uri.path.sub('/', '') %> ``` If you're going to store data in the database, run the generator and migrations to create the database schema: ```sh $ rails generate vanity $ rake db:migrate ``` #### Forking servers and reconnecting If you're using a forking server (like Passenger or Unicorn), you should reconnect after a new worker is created: ```ruby # unicorn.rb after_fork do |server, worker| defined?(Vanity) && Vanity.reconnect! end # an initializer if defined?(PhusionPassenger) PhusionPassenger.on_event(:starting_worker_process) do |forked| # We're in smart spawning mode. if forked defined?(Vanity) && Vanity.reconnect! end end end ``` If you're using explicit options with `Vanity.connect!`, you should call `disconnect!` first, for example: ```ruby Vanity.disconnect! Vanity.connect!( adapter: 'redis', redis: $redis ) ``` ### Initialization If you're using Rails, this is done automagically. Otherwise, some manual setup is required, for example on an app's booting: ``` $redis = Redis.new # or from elsewhere Vanity.configure do |config| # ... any config end Vanity.connect!( adapter: :redis, redis: $redis ) Vanity.load! ``` ### User identification #### Rails Turn Vanity on, and pass a reference to a method that identifies a user. For example: ```ruby class ApplicationController < ActionController::Base use_vanity :current_user end ``` For more information, please see the [identity documentation](http://vanity.labnotes.org/identity.html). #### Other Vanity pulls the identity from a "context" object that responds to `vanity_identity`, so we need to define a `Vanity.context` (this is how the [ActionMailer integration](https://github.com/assaf/vanity/blob/master/lib/vanity/frameworks/rails.rb#L107-L133) works): ``` class AVanityContext def vanity_identity "123" end end Vanity.context = AVanityContext.new() # Any object that responds to `#vanity_identity` ``` If you're using plain ruby objects, you could also alias something in your identity model to respond similarly and then set that as the vanity context: ``` class User alias_method :vanity_identity, :id end ``` ### Define a A/B test This experiment goes in the file `experiments/price_options.rb`: ```ruby ab_test "Price options" do description "Mirror, mirror on the wall, who's the better price of all?" alternatives 19, 25, 29 metrics :signups end ``` If the experiment uses a metric as above ("signups"), there needs to be a corresponding ruby file for that metric, `experiments/metrics/signups.rb`. ```ruby metric "Signup (Activation)" do description "Measures how many people signed up for our awesome service." end ``` ### Present the different options to your users In Rails' templates, this is straightforward: ```erb