Module: Optimizely::Helpers::EventTagUtils

Defined in:
lib/optimizely/helpers/event_tag_utils.rb

Constant Summary collapse

REVENUE_EVENT_METRIC_NAME =
'revenue'
NUMERIC_EVENT_METRIC_NAME =
'value'

Class Method Summary collapse

Class Method Details

.get_numeric_value(event_tags, logger) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/optimizely/helpers/event_tag_utils.rb', line 79

def get_numeric_value(event_tags, logger)
  # Grab the numeric event value from the event tags. "value" is a reserved keyword.
  # The value of 'value' can be a float or a numeric string
  #
  # event_tags - +Hash+ representing metadata associated with the event.
  # logger - Optional component which provides a log method to log messages.
  # Returns  +Number+ | +nil+ if value can't be retrieved from the event tags.

  if event_tags.nil?
    logger.log(Logger::DEBUG, 'Event tags is undefined.')
    return nil
  end

  unless Helpers::Validator.event_tags_valid?(event_tags)
    logger.log(Logger::DEBUG, 'Event tags is not a dictionary.')
    return nil
  end

  unless event_tags.key?(NUMERIC_EVENT_METRIC_NAME)
    logger.log(Logger::DEBUG, 'The numeric metric key is not defined in the event tags.')
    return nil
  end

  if event_tags[NUMERIC_EVENT_METRIC_NAME].nil?
    logger.log(Logger::DEBUG, 'The numeric metric key is null.')
    return nil
  end

  raw_value = event_tags[NUMERIC_EVENT_METRIC_NAME]

  if raw_value.is_a?(TrueClass) || raw_value.is_a?(FalseClass)
    logger.log(Logger::DEBUG, 'Provided numeric value is a boolean, which is an invalid format.')
    return nil
  end

  if raw_value.is_a?(Array) || raw_value.is_a?(Hash) || raw_value.to_f.nan? || raw_value.to_f.infinite?
    logger.log(Logger::DEBUG, 'Provided numeric value is in an invalid format.')
    return nil
  end

  unless Helpers::Validator.string_numeric?(raw_value)
    logger.log(Logger::DEBUG, 'Provided numeric value is not a numeric string.')
    return nil
  end

  raw_value = raw_value.to_f

  logger.log(Logger::INFO, "The numeric metric value #{raw_value} will be sent to results.")

  raw_value
end

.get_revenue_value(event_tags, logger) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/optimizely/helpers/event_tag_utils.rb', line 29

def get_revenue_value(event_tags, logger)
  # Grab the revenue value from the event tags. "revenue" is a reserved keyword.
  # The value will be parsed to an integer if possible.
  # Example:
  #   4.0 or "4.0" will be parsed to int(4).
  #   4.1 will not be parsed and the method will return nil.
  # event_tags - Hash representing metadata associated with the event.
  # logger - Optional component which provides a log method to log messages.
  #
  # Returns revenue value as an integer number
  # Returns nil if revenue can't be retrieved from the event tags.

  if event_tags.nil?
    logger.log(Logger::DEBUG, 'Event tags is undefined.')
    return nil
  end

  unless Helpers::Validator.event_tags_valid?(event_tags)
    logger.log(Logger::DEBUG, 'Event tags is not a hash.')
    return nil
  end

  unless event_tags.key?(REVENUE_EVENT_METRIC_NAME)
    logger.log(Logger::DEBUG, 'The revenue key is not defined in the event tags.')
    return nil
  end

  if event_tags[REVENUE_EVENT_METRIC_NAME].nil?
    logger.log(Logger::DEBUG, 'The revenue key is nil.')
    return nil
  end

  raw_value = event_tags[REVENUE_EVENT_METRIC_NAME]

  unless Helpers::Validator.string_numeric?(raw_value)
    logger.log(Logger::WARN, 'Revenue value is not an integer or float, or is not a numeric string.')
    return nil
  end

  raw_value = raw_value.to_f if raw_value.is_a? String

  unless raw_value == raw_value.to_i
    logger.log(Logger::WARN, "Failed to parse revenue value #{raw_value} from event tags.")
    return nil
  end

  logger.log(Logger::INFO, "Parsed revenue value #{raw_value.to_i} from event tags.")
  raw_value.to_i
end