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: {})


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

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

  self.hit_defaults = hit_defaults
end

Instance Attribute Details

#adapter=(value) ⇒ Object

Sets the attribute adapter

Parameters:

  • value

    the value to set the attribute adapter to.



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

def adapter=(value)
  @adapter = value
end

#hit_defaultsObject

Returns the value of attribute hit_defaults.



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

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:



71
72
73
# File 'lib/staccato/tracker.rb', line 71

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:



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

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

#build_screenview(options = {}) ⇒ Object



55
56
57
# File 'lib/staccato/tracker.rb', line 55

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

#build_transaction(options = {}) ⇒ Transaction

Build an ecommerce transaction

Returns:



132
133
134
# File 'lib/staccato/tracker.rb', line 132

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

#build_transaction_item(options = {}) ⇒ TransactionItem

Build an item in an ecommerce transaction

Returns:



145
146
147
# File 'lib/staccato/tracker.rb', line 145

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

#client_idString

The unique client id

Returns:

  • (String)


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

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



83
84
85
# File 'lib/staccato/tracker.rb', line 83

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



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

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

#idString?

The tracker id for GA

Returns:

  • (String, nil)


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

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



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

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

#screenview(options = {}) ⇒ Object



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

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



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

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



125
126
127
# File 'lib/staccato/tracker.rb', line 125

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



157
158
159
# File 'lib/staccato/tracker.rb', line 157

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



138
139
140
# File 'lib/staccato/tracker.rb', line 138

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



151
152
153
# File 'lib/staccato/tracker.rb', line 151

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