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
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
-
.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.
Instance Method Summary collapse
-
#add_custom_dimension(index, value) ⇒ Object
Set a custom dimension value at an index.
-
#add_custom_metric(index, value) ⇒ Object
Set a custom metric value at an index.
-
#add_measurement(key, options = {}) ⇒ Object
Add a measurement by its symbol name with options.
-
#custom_dimensions ⇒ Hash
Custom dimensions for this hit.
-
#custom_metrics ⇒ Hash
Custom metrics for this hit.
-
#fields ⇒ Hash
return the fields for this hit type.
-
#initialize(tracker, options = {}) ⇒ Object
sets up a new hit.
-
#measurements ⇒ Array<Measurable>
Measurements for this hit.
-
#params ⇒ Object
collects the parameters from options for this hit type.
-
#session_control ⇒ 'start', 'end'
Returns the value for session control based on options for session_start/_end.
-
#track! ⇒ Object
send the hit to the tracker.
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
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
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
145 146 147 148 149 150 151 |
# File 'lib/staccato/hit.rb', line 145 def add_measurement(key, = {}) if .is_a?(Hash) self.measurements << Measurement.lookup(key).new() else self.measurements << end end |
#custom_dimensions ⇒ Hash
Custom dimensions for this hit
124 125 126 |
# File 'lib/staccato/hit.rb', line 124 def custom_dimensions @custom_dimensions ||= {} end |
#custom_metrics ⇒ Hash
Custom metrics for this hit
137 138 139 |
# File 'lib/staccato/hit.rb', line 137 def custom_metrics @custom_metrics ||= {} end |
#fields ⇒ Hash
return the fields for this hit type
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
91 92 93 94 |
# File 'lib/staccato/hit.rb', line 91 def initialize(tracker, = {}) self.tracker = tracker self. = OptionSet.new(convert_booleans()) end |
#measurements ⇒ Array<Measurable>
Measurements for this hit
155 156 157 |
# File 'lib/staccato/hit.rb', line 155 def measurements @measurements ||= [] end |
#params ⇒ Object
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!(). 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
162 163 164 165 166 167 168 169 |
# File 'lib/staccato/hit.rb', line 162 def session_control case when [:session_start], [:start_session] 'start' when [:session_end], [:end_session], [: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 |