Module: Staccato::Hit

Included in:
Event, Exception, Pageview, 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 =
{
  anonymize_ip: 'aip', # boolean
  queue_time: 'qt', # integer
  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'
}
BOOLEAN_FIELDS =
[
  :non_interactive,
  :anonymize_ip,
  :java_enabled
]

Class Method Summary collapse

Instance Method Summary collapse

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(position, value) ⇒ Object



100
101
102
# File 'lib/staccato/hit.rb', line 100

def add_custom_dimension(position, value)
  self.custom_dimensions["cd#{position}"] = value
end

#add_custom_metric(position, value) ⇒ Object



108
109
110
# File 'lib/staccato/hit.rb', line 108

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

#custom_dimensionsObject



104
105
106
# File 'lib/staccato/hit.rb', line 104

def custom_dimensions
  @custom_dimensions ||= {}
end

#custom_metricsObject



112
113
114
# File 'lib/staccato/hit.rb', line 112

def custom_metrics
  @custom_metrics ||= {}
end

#fieldsHash

return the fields for this hit type

Returns:

  • (Hash)

    the field definitions



85
86
87
# File 'lib/staccato/hit.rb', line 85

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



78
79
80
81
# File 'lib/staccato/hit.rb', line 78

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

#paramsObject

collects the parameters from options for this hit type



90
91
92
93
94
95
96
97
98
# File 'lib/staccato/hit.rb', line 90

def params
  base_params.
    merge(tracker_default_params).
    merge(global_options_params).
    merge(hit_params).
    merge(custom_dimensions).
    merge(custom_metrics).
    reject {|_,v| v.nil?}
end

#session_controlObject



116
117
118
119
120
121
122
123
# File 'lib/staccato/hit.rb', line 116

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



126
127
128
# File 'lib/staccato/hit.rb', line 126

def track!
  tracker.track(params)
end