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, options = {}) ⇒ 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)


13
14
15
16
17
18
19
20
# 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

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

#adapter=(adapter) ⇒ Object



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

def adapter=(adapter)
  @adapters = [new_adapter]
end

#add_adapter(adapter) ⇒ Object



26
27
28
# File 'lib/staccato/tracker.rb', line 26

def add_adapter(adapter)
  @adapters << adapter
end

#build_event(options = {}) ⇒ Event

Build an event

Parameters:

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

    options include:

    • category (optional)

    • action (optional)

    • label (optional)

    • value (optional)

Returns:



80
81
82
# File 'lib/staccato/tracker.rb', line 80

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:



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

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

#build_screenview(options = {}) ⇒ Object



64
65
66
# File 'lib/staccato/tracker.rb', line 64

def build_screenview(options = {})
  Staccato::Screenview.new(self, options)
end

#build_transaction(options = {}) ⇒ Transaction

Build an ecommerce transaction

Returns:



141
142
143
# File 'lib/staccato/tracker.rb', line 141

def build_transaction(options = {})
  Staccato::Transaction.new(self, options)
end

#build_transaction_item(options = {}) ⇒ TransactionItem

Build an item in an ecommerce transaction

Returns:



154
155
156
# File 'lib/staccato/tracker.rb', line 154

def build_transaction_item(options = {})
  Staccato::TransactionItem.new(self, options)
end

#client_idString

The unique client id

Returns:

  • (String)


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

def client_id
  @client_id ||= Staccato.build_client_id
end

#default_uriObject



170
171
172
# File 'lib/staccato/tracker.rb', line 170

def default_uri
  Staccato.ga_collection_uri(@ssl)
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



92
93
94
# File 'lib/staccato/tracker.rb', line 92

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



113
114
115
# File 'lib/staccato/tracker.rb', line 113

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

#idString?

The tracker id for GA

Returns:

  • (String, nil)


32
33
34
# File 'lib/staccato/tracker.rb', line 32

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



60
61
62
# File 'lib/staccato/tracker.rb', line 60

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

#screenview(options = {}) ⇒ Object



68
69
70
# File 'lib/staccato/tracker.rb', line 68

def screenview(options = {})
  build_screenview(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



103
104
105
# File 'lib/staccato/tracker.rb', line 103

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



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

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



166
167
168
# File 'lib/staccato/tracker.rb', line 166

def track(params={})
  post(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



147
148
149
# File 'lib/staccato/tracker.rb', line 147

def transaction(options = {})
  build_transaction(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



160
161
162
# File 'lib/staccato/tracker.rb', line 160

def transaction_item(options = {})
  build_transaction_item(options).track!
end