Class: Staccato::Tracker

Inherits:
Object
  • Object
show all
Defined in:
lib/staccato/tracker.rb

Overview

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

using the tracker and client id

Author:

  • Tony Pitale

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, client_id = nil, hit_defaults = {}) ⇒ Tracker

sets up a new tracker

Parameters:

  • id (String)

    the GA tracker id

  • client_id (String, nil) (defaults to: nil)

    unique value to track user sessions

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


13
14
15
16
17
18
# File 'lib/staccato/tracker.rb', line 13

def initialize(id, client_id = nil, hit_defaults = {})
  @id = id
  @client_id = client_id

  self.hit_defaults = hit_defaults
end

Instance Attribute Details

#hit_defaultsObject

Returns the value of attribute hit_defaults.



7
8
9
# File 'lib/staccato/tracker.rb', line 7

def hit_defaults
  @hit_defaults
end

Instance Method Details

#build_event(options = {}) ⇒ Event

Build an event

Parameters:

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

    options include:

    • category (optional)

    • action (optional)

    • label (optional)

    • value (optional)

Returns:



62
63
64
# File 'lib/staccato/tracker.rb', line 62

def build_event(options = {})
  Staccato::Event.new(self, options)
end

#build_pageview(options = {}) ⇒ Pageview

Build a pageview

Parameters:

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

    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

Returns:



39
40
41
# File 'lib/staccato/tracker.rb', line 39

def build_pageview(options = {})
  Staccato::Pageview.new(self, options)
end

#client_idString

The unique client id

Returns:

  • (String)


28
29
30
# File 'lib/staccato/tracker.rb', line 28

def client_id
  @client_id ||= Staccato.build_client_id
end

#event(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track an event

Parameters:

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

    options include:

    • category (optional)

    • action (optional)

    • label (optional)

    • value (optional)

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



74
75
76
# File 'lib/staccato/tracker.rb', line 74

def event(options = {})
  build_event(options).track!
end

#exception(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track an exception

Parameters:

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

    options include:

    • description (optional) often the class of exception, e.g., RuntimeException

    • fatal (optional) was the exception fatal? boolean, defaults to false

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



95
96
97
# File 'lib/staccato/tracker.rb', line 95

def exception(options = {})
  Staccato::Exception.new(self, options).track!
end

#idString?

The tracker id for GA

Returns:

  • (String, nil)


22
23
24
# File 'lib/staccato/tracker.rb', line 22

def id
  @id
end

#pageview(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track a pageview

Parameters:

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

    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

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



50
51
52
# File 'lib/staccato/tracker.rb', line 50

def pageview(options = {})
  build_pageview(options).track!
end

#social(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

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

Parameters:

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

    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’

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



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

def social(options = {})
  Staccato::Social.new(self, options).track!
end

#timing(options = {}, &block) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track timing

Parameters:

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

    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

  • 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.

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



116
117
118
# File 'lib/staccato/tracker.rb', line 116

def timing(options = {}, &block)
  Staccato::Timing.new(self, options).track!(&block)
end

#track(params = {}) ⇒ Net::HTTPOK

post the hit to GA collection endpoint

Returns:

  • (Net::HTTPOK)

    the GA api always returns 200 OK



134
135
136
# File 'lib/staccato/tracker.rb', line 134

def track(params={})
  post(Staccato.tracking_uri, params)
end

#transaction(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track an ecommerce transaction

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



122
123
124
# File 'lib/staccato/tracker.rb', line 122

def transaction(options = {})
  Staccato::Transaction.new(self, options).track!
end

#transaction_item(options = {}) ⇒ <Net::HTTPOK] the GA `/collect` endpoint always returns a 200

Track an item in an ecommerce transaction

Returns:

  • (<Net::HTTPOK] the GA `/collect` endpoint always returns a 200)

    <Net::HTTPOK] the GA ‘/collect` endpoint always returns a 200



128
129
130
# File 'lib/staccato/tracker.rb', line 128

def transaction_item(options = {})
  Staccato::TransactionItem.new(self, options).track!
end