Class: TrailGuide::Metrics::Goal

Inherits:
Object
  • Object
show all
Defined in:
lib/trail_guide/metrics/goal.rb

Overview

represents a simple conversion goal

Direct Known Subclasses

Checkpoint, Funnel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(experiment, name, **config, &block) ⇒ Goal

Returns a new instance of Goal.



21
22
23
24
25
# 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

Instance Attribute Details

#experimentObject (readonly)

Returns the value of attribute experiment.



5
6
7
# File 'lib/trail_guide/metrics/goal.rb', line 5

def experiment
  @experiment
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/trail_guide/metrics/goal.rb', line 5

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



42
43
44
45
# 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) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
50
# File 'lib/trail_guide/metrics/goal.rb', line 47

def allow_conversion?(trial, variant, =nil)
  return true if callbacks[:allow_conversion].empty?
  run_callbacks(:allow_conversion, trial, true, variant, trial.participant, )
end

#as_json(opts = {}) ⇒ Object



76
77
78
79
80
# File 'lib/trail_guide/metrics/goal.rb', line 76

def as_json(opts={})
  {
    name: name,
  }
end

#configurationObject



13
14
15
# File 'lib/trail_guide/metrics/goal.rb', line 13

def configuration
  @configuration ||= Metrics::Config.new(self)
end

#configure(*args, &block) ⇒ Object



17
18
19
# File 'lib/trail_guide/metrics/goal.rb', line 17

def configure(*args, &block)
  configuration.configure(*args, &block)
end

#dup(experiment) ⇒ Object



9
10
11
# 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) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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_keyObject



86
87
88
# File 'lib/trail_guide/metrics/goal.rb', line 86

def storage_key
  "#{experiment.experiment_name}:#{name}"
end

#to_sObject



82
83
84
# File 'lib/trail_guide/metrics/goal.rb', line 82

def to_s
  name.to_s
end