class Staccato::Tracker

The `Tracker` class has methods to create all `Hit` types

using the tracker and client id

@author Tony Pitale

Attributes

hit_defaults[RW]

Public Class Methods

new(id, client_id = nil, options = {}) click to toggle source

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

adapter=(adapter) click to toggle source
# File lib/staccato/tracker.rb, line 22
def adapter=(adapter)
  @adapters = [adapter]
end
add_adapter(adapter) click to toggle source
# File lib/staccato/tracker.rb, line 26
def add_adapter(adapter)
  @adapters << adapter
end
build_event(options = {}) click to toggle source

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_pageview(options = {}) click to toggle source

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
build_screenview(options = {}) click to toggle source
# File lib/staccato/tracker.rb, line 64
def build_screenview(options = {})
  Staccato::Screenview.new(self, options)
end
build_transaction(options = {}) click to toggle source

Build an ecommerce transaction

@return [Transaction]

# File lib/staccato/tracker.rb, line 141
def build_transaction(options = {})
  Staccato::Transaction.new(self, options)
end
build_transaction_item(options = {}) click to toggle source

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
client_id() click to toggle source

The unique client id @return [String]

# File lib/staccato/tracker.rb, line 38
def client_id
  @client_id ||= Staccato.build_client_id
end
default_uri() click to toggle source
# File lib/staccato/tracker.rb, line 170
def default_uri
  Staccato.ga_collection_uri(@ssl)
end
event(options = {}) click to toggle source

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
exception(options = {}) click to toggle source

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
id() click to toggle source

The tracker id for GA @return [String, nil]

# File lib/staccato/tracker.rb, line 32
def id
  @id
end
pageview(options = {}) click to toggle source

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
screenview(options = {}) click to toggle source
# File lib/staccato/tracker.rb, line 68
def screenview(options = {})
  build_screenview(options).track!
end
social(options = {}) click to toggle source

Track a social event such as a Facebook Like or Twitter Share

@param options [Hash] options include:

* action (required) the action taken, e.g., 'like'
* network (required) the network used, e.g., 'facebook'
* target (required) the target page path, e.g., '/blog/something-awesome'

@return [<Net::HTTPOK] the GA `/collect` endpoint always returns a 200

# File lib/staccato/tracker.rb, line 103
def social(options = {})
  Staccato::Social.new(self, options).track!
end
timing(options = {}, &block) click to toggle source

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
track(params={}) click to toggle source

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
transaction(options = {}) click to toggle source

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
transaction_item(options = {}) click to toggle source

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

adapters() click to toggle source

@private

# File lib/staccato/tracker.rb, line 197
def adapters
  @adapters.empty? ? [default_adapter] : @adapters
end
default_adapter() click to toggle source

@private

# File lib/staccato/tracker.rb, line 202
def default_adapter
  @default_adapter ||= Staccato.default_adapter.new(default_uri)
end
post(params) click to toggle source

@private

# File lib/staccato/tracker.rb, line 182
def post(params)
  single_adapter? ? post_first(params) : post_all(params)
end
post_all(params) click to toggle source

@private

# File lib/staccato/tracker.rb, line 192
def post_all(params)
  adapters.map {|adapter| adapter.post(params)}
end
post_first(params) click to toggle source

@private

# File lib/staccato/tracker.rb, line 187
def post_first(params)
  adapters.first.post(params)
end
single_adapter?() click to toggle source

@private

# File lib/staccato/tracker.rb, line 177
def single_adapter?
  adapters.length == 1
end