class Staccato::Tracker
The `Tracker` class has methods to create all `Hit` types
using the tracker and client id
@author Tony Pitale
Attributes
Public Class Methods
sets up a new tracker @param id [String] the GA tracker id @param client_id
[String, nil] unique value to track user sessions @param hit_defaults
[Hash]
# File lib/staccato/tracker.rb, line 13 def initialize(id, client_id = nil, options = {}) @id = id @client_id = client_id @ssl = options.delete(:ssl) || false @adapters = [] self.hit_defaults = options end
Public Instance Methods
# File lib/staccato/tracker.rb, line 22 def adapter=(adapter) @adapters = [adapter] end
# File lib/staccato/tracker.rb, line 26 def add_adapter(adapter) @adapters << adapter end
Build an event
@param options [Hash] options include:
* category (optional) * action (optional) * label (optional) * value (optional)
@return [Event]
# File lib/staccato/tracker.rb, line 80 def build_event(options = {}) Staccato::Event.new(self, options) end
Build a pageview
@param options [Hash] options include:
* path (optional) the path of the current page view * hostname (optional) the hostname of the current page view * title (optional) the page title
@return [Pageview]
# File lib/staccato/tracker.rb, line 49 def build_pageview(options = {}) Staccato::Pageview.new(self, options) end
# File lib/staccato/tracker.rb, line 64 def build_screenview(options = {}) Staccato::Screenview.new(self, options) end
Build an ecommerce transaction
@return [Transaction]
# File lib/staccato/tracker.rb, line 141 def build_transaction(options = {}) Staccato::Transaction.new(self, options) end
Build an item in an ecommerce transaction
@return [TransactionItem]
# File lib/staccato/tracker.rb, line 154 def build_transaction_item(options = {}) Staccato::TransactionItem.new(self, options) end
The unique client id @return [String]
# File lib/staccato/tracker.rb, line 38 def client_id @client_id ||= Staccato.build_client_id end
# File lib/staccato/tracker.rb, line 170 def default_uri Staccato.ga_collection_uri(@ssl) end
Track an event
@param options [Hash] options include:
* category (optional) * action (optional) * label (optional) * value (optional)
@return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 92 def event(options = {}) build_event(options).track! end
Track an exception
@param options [Hash] options include:
* description (optional) often the class of exception, e.g., RuntimeException * fatal (optional) was the exception fatal? boolean, defaults to false
@return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 113 def exception(options = {}) Staccato::Exception.new(self, options).track! end
The tracker id for GA @return [String, nil]
# File lib/staccato/tracker.rb, line 32 def id @id end
Track a pageview
@param options [Hash] options include:
* path (optional) the path of the current page view * hostname (optional) the hostname of the current page view * title (optional) the page title
@return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 60 def pageview(options = {}) build_pageview(options).track! end
# File lib/staccato/tracker.rb, line 68 def screenview(options = {}) build_screenview(options).track! end
Track timing
@param options [Hash] options include:
* category (optional) e.g., 'runtime' * variable (optional) e.g., 'database' * label (optional) e.g., 'query' * time (recommended) the integer time in milliseconds * page_load_time (optional) * dns_time (optional) * page_download_time (optional) * redirect_response_time (optional) * tcp_connect_time (optional) * server_response_time (optional) most useful on the server-side
@param block [#call] if a block is provided, the time it takes to
run will be recorded and set as the `time` value option, no other time values will be set.
@return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 134 def timing(options = {}, &block) Staccato::Timing.new(self, options).track!(&block) end
post the hit to GA collection endpoint @return [Net::HTTPOK] the GA api always returns 200 OK
# File lib/staccato/tracker.rb, line 166 def track(params={}) post(params) end
Track an ecommerce transaction @return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 147 def transaction(options = {}) build_transaction(options).track! end
Track an item in an ecommerce transaction @return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200
# File lib/staccato/tracker.rb, line 160 def transaction_item(options = {}) build_transaction_item(options).track! end
Private Instance Methods
@private
# File lib/staccato/tracker.rb, line 197 def adapters @adapters.empty? ? [default_adapter] : @adapters end
@private
# File lib/staccato/tracker.rb, line 202 def default_adapter @default_adapter ||= Staccato.default_adapter.new(default_uri) end
@private
# File lib/staccato/tracker.rb, line 182 def post(params) single_adapter? ? post_first(params) : post_all(params) end
@private
# File lib/staccato/tracker.rb, line 192 def post_all(params) adapters.map {|adapter| adapter.post(params)} end
@private
# File lib/staccato/tracker.rb, line 187 def post_first(params) adapters.first.post(params) end
@private
# File lib/staccato/tracker.rb, line 177 def single_adapter? adapters.length == 1 end