Module: Vanity::Helpers

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

Overview

Helper methods available on the Vanity module.

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, request = nil, &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



35
36
37
38
39
40
41
42
43
44
# File 'lib/vanity/helpers.rb', line 35

def ab_test(name, request = nil, &block)
  request ||= Vanity.context.respond_to?(:request) ? Vanity.context.request : nil

  alternative = Vanity.playground.experiment(name).choose(request)
  Vanity.context.vanity_add_to_active_experiments(name, alternative)

  Vanity.logger.warn("Deprecated: This method used to accept a block, however, calling it with a block would result in an exception. The block will be removed from the signature in an upcoming version.") if block

  alternative.value
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] })

Parameters:

  • count_or_options (defaults to: 1)

    Defaults to a count of 1. Also accepts a hash of options passed (eventually) to AbTest#track!.

Since:

  • 1.2.0



59
60
61
# File 'lib/vanity/helpers.rb', line 59

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