Module: Vanity::Helpers

Included in:
Vanity
Defined in:
lib/vanity/helpers.rb

Overview

Helper methods available on Object.

Examples:

From ERB template

<%= ab_test(:greeting) %> <%= current_user.name %>

From Rails controller

class AccountController < ApplicationController
  def create
    Vanity.track!(:signup)
    Acccount.create!(params[:account])
  end
end

From ActiveRecord

class Posts < ActiveRecord::Base
  after_create do |post|
    Vanity.track!(:images if post.type == :image)
  end
end

Instance Method Summary collapse

Instance Method Details

#ab_test(name, &block) ⇒ Object

This method returns one of the alternative values in the named A/B test.

Examples:

A/B two alternatives for a page

def index
  if Vanity.ab_test(:new_page) # true/false test
    render action: "new_page"
  else
    render action: "index"
  end
end

Similar, alternative value is page name

def index
  render action: Vanity.ab_test(:new_page)
end

Since:

  • 1.2.0



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/vanity/helpers.rb', line 36

def ab_test(name, &block)
  # TODO refactor with Vanity::Rails::Helpers#ab_test, however that's used
  # within Rails views
  request = respond_to?(:request) ? self.request : nil
  if Vanity.playground.using_js?
    value = Vanity.context.vanity_store_experiment_for_js name, Vanity.playground.experiment(name).choose(request)
  else
    value = Vanity.playground.experiment(name).choose(request).value
  end

  if block
    content = capture(value, &block)
  else
    value
  end
end

#track!(name, count_or_options = 1) ⇒ Object

Tracks an action associated with a metric. Useful for calling from a Rack handler. Note that a user should already be added to an experiment via #ab_test before this is called - otherwise, the conversion will be tracked, but the user will not be added to the experiment.

Examples:

Vanity.track!(:invitation)
Vanity.track!(:click, { :identity=>Identity.new(env['rack.session']), :values=>[1] })

Since:

  • 1.2.0



66
67
68
# File 'lib/vanity/helpers.rb', line 66

def track!(name, count_or_options = 1)
  Vanity.playground.track! name, count_or_options
end