module Staccato::Hit

The `Hit` module enables a class to track the appropriate parameters

to Google Analytics given a defined set of `FIELDS` in a map between
the option name and its specified GA field name

@author Tony Pitale

Constants

BOOLEAN_FIELDS

Fields which should be converted to boolean for google

GLOBAL_OPTIONS

Hit global options may be set on any hit type options

Public Class Methods

included(model) click to toggle source

this module is included into each model hit type

to share the common behavior required to hit
the Google Analytics /collect api endpoint
# File lib/staccato/hit.rb, line 11
def self.included(model)
  model.extend Forwardable

  model.class_eval do
    attr_accessor :tracker, :options

    def_delegators :@options, *model::FIELDS.keys
  end
end
new(tracker, options = {}) click to toggle source

sets up a new hit @param tracker [Staccato::Tracker] the tracker to collect to @param options [Hash] options for the specific hit type

# File lib/staccato/hit.rb, line 91
def initialize(tracker, options = {})
  self.tracker = tracker
  self.options = OptionSet.new(convert_booleans(options))
end

Public Instance Methods

add_custom_dimension(index, value) click to toggle source

Set a custom dimension value at an index @param index [Integer] @param value

# File lib/staccato/hit.rb, line 118
def add_custom_dimension(index, value)
  self.custom_dimensions["cd#{index}"] = value
end
add_custom_metric(index, value) click to toggle source

Set a custom metric value at an index @param index [Integer] @param value

# File lib/staccato/hit.rb, line 131
def add_custom_metric(index, value)
  self.custom_metrics["cm#{index}"] = value
end
add_measurement(key, options = {}) click to toggle source

Add a measurement by its symbol name with options

@param key [Symbol] any one of the measurable classes lookup key @param options [Hash or Object] for the measurement

# File lib/staccato/hit.rb, line 145
def add_measurement(key, options = {})
  if options.is_a?(Hash)
    self.measurements << Measurement.lookup(key).new(options)
  else
    self.measurements << options
  end
end
custom_dimensions() click to toggle source

Custom dimensions for this hit @return [Hash]

# File lib/staccato/hit.rb, line 124
def custom_dimensions
  @custom_dimensions ||= {}
end
custom_metrics() click to toggle source

Custom metrics for this hit @return [Hash]

# File lib/staccato/hit.rb, line 137
def custom_metrics
  @custom_metrics ||= {}
end
fields() click to toggle source

return the fields for this hit type @return [Hash] the field definitions

# File lib/staccato/hit.rb, line 98
def fields
  self.class::FIELDS
end
measurements() click to toggle source

Measurements for this hit @return [Array<Measurable>]

# File lib/staccato/hit.rb, line 155
def measurements
  @measurements ||= []
end
params() click to toggle source

collects the parameters from options for this hit type

# File lib/staccato/hit.rb, line 103
def params
  {}.
  merge!(base_params).
  merge!(tracker_default_params).
  merge!(global_options_params).
  merge!(hit_params).
  merge!(custom_dimensions).
  merge!(custom_metrics).
  merge!(measurement_params).
  reject {|_,v| v.nil?}
end
session_control() click to toggle source

Returns the value for session control

based on options for session_start/_end

@return ['start', 'end']

# File lib/staccato/hit.rb, line 162
def session_control
  case
  when options[:session_start], options[:start_session]
    'start'
  when options[:session_end], options[:end_session], options[:stop_session]
    'end'
  end
end
track!() click to toggle source

send the hit to the tracker

# File lib/staccato/hit.rb, line 172
def track!
  tracker.track(params)
end

Private Instance Methods

base_params() click to toggle source

@private

# File lib/staccato/hit.rb, line 185
def base_params
  {
    'v' => 1, # protocol version
    'tid' => tracker.id, # tracking/web_property id
    'cid' => tracker.client_id, # unique client id
    'sc' => session_control,
    't' => type.to_s
  }
end
boolean_fields() click to toggle source

@private

# File lib/staccato/hit.rb, line 178
def boolean_fields
  BOOLEAN_FIELDS
end
global_option?(key) click to toggle source

@private

# File lib/staccato/hit.rb, line 214
def global_option?(key)
  GLOBAL_OPTIONS.keys.include?(key)
end
global_options_params() click to toggle source

@private

# File lib/staccato/hit.rb, line 196
def global_options_params
  Hash[
    options.map { |k,v|
      [GLOBAL_OPTIONS[k], v] if global_option?(k)
    }.compact
  ]
end
hit_params() click to toggle source

@private

# File lib/staccato/hit.rb, line 219
def hit_params
  Hash[
    fields.map { |field,key|
      [key, options[field]] unless options[field].nil?
    }.compact
  ]
end
measurement_params() click to toggle source

@private

# File lib/staccato/hit.rb, line 228
def measurement_params
  measurements.dup.map!(&:params).inject({}, &:merge!)
end
tracker_default_params() click to toggle source

@private

# File lib/staccato/hit.rb, line 205
def tracker_default_params
  Hash[
    tracker.hit_defaults.map { |k,v|
      [GLOBAL_OPTIONS[k], v] if global_option?(k)
    }.compact
  ]
end