Module: Staccato::Hit

Includes:
BooleanHelpers
Included in:
Event, Exception, Pageview, Screenview, Social, Timing, Transaction, TransactionItem
Defined in:
lib/staccato/hit.rb

Overview

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

Constant Summary collapse

GLOBAL_OPTIONS =

Hit global options may be set on any hit type options

{
  anonymize_ip: 'aip', # boolean
  queue_time: 'qt', # integer
  data_source: 'ds',
  cache_buster: 'z',
  user_id: 'uid', # a known user's id

  # Session, works with session control
  user_ip: 'uip',
  user_agent: 'ua',

  # Traffic Sources
  referrer: 'dr',
  campaign_name: 'cn',
  campaign_source: 'cs',
  campaign_medium: 'cm',
  campaign_keyword: 'ck',
  campaign_content: 'cc',
  campaign_id: 'ci',
  adwords_id: 'gclid',
  display_ads_id: 'dclid',

  # System Info
  screen_resolution: 'sr',
  viewport_size: 'vp',
  screen_colors: 'sd',
  user_language: 'ul',
  java_enabled: 'je', # boolean
  flash_version: 'fl',
  non_interactive: 'ni', # boolean
  document_location: 'dl',
  document_encoding: 'de', # duplicate of encoding
  document_hostname: 'dh', # duplicate of hostname
  document_path: 'dp', # duplicate of path
  document_title: 'dt', # duplicate of title
  screen_name: 'cd', # screen name is not related to custom dimensions
  link_id: 'linkid',

  # App Tracking
  application_name: 'an',
  application_id: 'aid',
  application_installer_id: 'aiid',
  application_version: 'av',

  # Content Experiments
  experiment_id: 'xid',
  experiment_variant: 'xvar',

  # Product
  product_action: 'pa',
  product_action_list: 'pal',

  # Promotion
  promotion_action: 'promoa',

  # Location
  geographical_id: 'geoid'
}.freeze
BOOLEAN_FIELDS =

Fields which should be converted to boolean for google

[
  :non_interactive,
  :anonymize_ip,
  :java_enabled
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BooleanHelpers

#boolean_field?, #convert_boolean, #convert_booleans, #integer_for

Class Method Details

.included(model) ⇒ Object

this module is included into each model hit type

to share the common behavior required to hit
the Google Analytics /collect api endpoint

11
12
13
14
15
16
17
18
19
# 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

Instance Method Details

#add_custom_dimension(index, value) ⇒ Object

Set a custom dimension value at an index

Parameters:

  • index (Integer)
  • value

118
119
120
# 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) ⇒ Object

Set a custom metric value at an index

Parameters:

  • index (Integer)
  • value

131
132
133
# File 'lib/staccato/hit.rb', line 131

def add_custom_metric(index, value)
  self.custom_metrics["cm#{index}"] = value
end

#add_measurement(key, options = {}) ⇒ Object

Add a measurement by its symbol name with options

Parameters:

  • key (Symbol)

    any one of the measurable classes lookup key

  • options (Hash or Object) (defaults to: {})

    for the measurement


145
146
147
148
149
150
151
# 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_dimensionsHash

Custom dimensions for this hit

Returns:

  • (Hash)

124
125
126
# File 'lib/staccato/hit.rb', line 124

def custom_dimensions
  @custom_dimensions ||= {}
end

#custom_metricsHash

Custom metrics for this hit

Returns:

  • (Hash)

137
138
139
# File 'lib/staccato/hit.rb', line 137

def custom_metrics
  @custom_metrics ||= {}
end

#fieldsHash

return the fields for this hit type

Returns:

  • (Hash)

    the field definitions


98
99
100
# File 'lib/staccato/hit.rb', line 98

def fields
  self.class::FIELDS
end

#initialize(tracker, options = {}) ⇒ Object

sets up a new hit

Parameters:

  • tracker (Staccato::Tracker)

    the tracker to collect to

  • options (Hash) (defaults to: {})

    options for the specific hit type


91
92
93
94
# File 'lib/staccato/hit.rb', line 91

def initialize(tracker, options = {})
  self.tracker = tracker
  self.options = OptionSet.new(convert_booleans(options))
end

#measurementsArray<Measurable>

Measurements for this hit

Returns:


155
156
157
# File 'lib/staccato/hit.rb', line 155

def measurements
  @measurements ||= []
end

#paramsObject

collects the parameters from options for this hit type


103
104
105
106
107
108
109
110
111
112
113
# 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'start', 'end'

Returns the value for session control

based on options for session_start/_end

Returns:

  • ('start', 'end')

162
163
164
165
166
167
168
169
# 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!Object

send the hit to the tracker


172
173
174
# File 'lib/staccato/hit.rb', line 172

def track!
  tracker.track(params)
end