class TrailGuide::Metrics::Goal

represents a simple conversion goal

Attributes

experiment[R]
name[R]

Public Class Methods

new(experiment, name, **config, &block) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 21
def initialize(experiment, name, **config, &block)
  @experiment = experiment
  @name = name.to_s.underscore.to_sym
  configure(**config, &block)
end

Public Instance Methods

==(other) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 27
def ==(other)
  if other.is_a?(self.class)
    return name == other.name
  elsif other.is_a?(String) || other.is_a?(Symbol)
    other = other.to_s.underscore
    return name == other.to_sym || to_s == other
  # Currently unused placeholder for future functionality
  #elsif other.is_a?(Array)
  #  return to_s == other.flatten.map { |o| o.to_s.underscore }.join('/')
  #elsif other.is_a?(Hash)
  #  # TODO "flatten" it out and compare it to_s
  #  return false
  end
end
===(other) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 42
def ===(other)
  return false unless other.is_a?(self.class)
  return name == other.name && experiment == other.experiment
end
allow_conversion?(trial, variant, metadata=nil) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 47
def allow_conversion?(trial, variant, metadata=nil)
  return true if callbacks[:allow_conversion].empty?
  run_callbacks(:allow_conversion, trial, true, variant, trial.participant, metadata)
end
as_json(opts={}) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 76
def as_json(opts={})
  {
    name: name,
  }
end
configuration() click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 13
def configuration
  @configuration ||= Metrics::Config.new(self)
end
configure(*args, &block) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 17
def configure(*args, &block)
  configuration.configure(*args, &block)
end
dup(experiment) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 9
def dup(experiment)
  self.class.new(experiment, name, **configuration.to_h.map { |k,v| [k, v.try(:dup)] }.to_h)
end
run_callbacks(hook, trial, *args) click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 52
def run_callbacks(hook, trial, *args)
  return unless callbacks[hook]
  if [:allow_conversion].include?(hook)
    callbacks[hook].reduce(args.slice!(0,1)[0]) do |result, callback|
      if callback.respond_to?(:call)
        callback.call(trial, result, self, *args)
      else
        trial.send(callback, trial, result, self, *args)
      end
    end
  # Currently unused placeholder for future functionality
  #else
  #  args.unshift(self)
  #  args.unshift(trial)
  #  callbacks[hook].each do |callback|
  #    if callback.respond_to?(:call)
  #      callback.call(*args)
  #    else
  #      trial.send(callback, *args)
  #    end
  #  end
  end
end
storage_key() click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 86
def storage_key
  "#{experiment.experiment_name}:#{name}"
end
to_s() click to toggle source
# File lib/trail_guide/metrics/goal.rb, line 82
def to_s
  name.to_s
end