Class: PiwikTracker::Piwik::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/piwik_tracker/piwik.rb

Constant Summary collapse

VERSION =

Piwik API version

1
VISITOR_ID_LENGTH =

length of Piwik visitor ids

16

Instance Method Summary collapse

Constructor Details

#initialize(piwik, auth_token = nil) ⇒ Request

Returns a new instance of Request.



82
83
84
85
86
87
# File 'lib/piwik_tracker/piwik.rb', line 82

def initialize(piwik, auth_token = nil)
  @piwik = piwik
  @custom_variables = []
  @data = {}
  @data[:token_auth] = auth_token if auth_token
end

Instance Method Details

#attribution_info(info) ⇒ Object

Sets the attribution information to the visit, so that subsequent Goal conversions are properly attributed to the right Referrer URL, timestamp, Campaign Name & Keyword. info - 4 element array of [campaign name, campaign keyword, Timestamp at which the referrer was set, Referrer URL]



107
108
109
110
111
# File 'lib/piwik_tracker/piwik.rb', line 107

def attribution_info(info)
  info = JSON.parse(info) if String === info
  @data[:_rcn], @data[:_rck], @data[:_refts], @data[:_ref] = info
  self
end

#browser_language(lang) ⇒ Object



124
125
126
127
# File 'lib/piwik_tracker/piwik.rb', line 124

def browser_language(lang)
  @data[:browser_language] = lang
  self
end

#custom_variable(slot_id, name, value) ⇒ Object

Sets Visit Custom Variable. See piwik.org/docs/custom-variables/ slot_id - Custom variable slot ID from 1-5 name - Custom variable name value - Custom variable value



118
119
120
121
122
# File 'lib/piwik_tracker/piwik.rb', line 118

def custom_variable(slot_id, name, value)
  raise "invalid slot id, has to be between 1 and 5" unless (1..5).include?(slot_id)
  @custom_variables[slot_id - 1] = [name, value]
  self
end

#forced_date_time(time) ⇒ Object

Overrides server date and time for the tracking requests. By default Piwik will track requests for the “current datetime” but this function allows you to track visits in the past. time - ruby Time instance



138
139
140
141
# File 'lib/piwik_tracker/piwik.rb', line 138

def forced_date_time(time)
  @data[:cdt] = time.utc.to_i
  self
end

#ip(client_ip) ⇒ Object

Overrides IP address client_ip - IP address string



145
146
147
148
# File 'lib/piwik_tracker/piwik.rb', line 145

def ip(client_ip)
  @data[:cip] = client_ip
  self
end

#referrer(url) ⇒ Object

Sets the URL referrer used to track Referrers details for new visits.



99
100
101
102
# File 'lib/piwik_tracker/piwik.rb', line 99

def referrer(url)
  @data[:urlref] = url
  self
end

#track_action(action_url, action_type) ⇒ Object

Tracks a download or outlink action_url - URL of the download or outlink action_type Type of the action: :download or :link



182
183
184
# File 'lib/piwik_tracker/piwik.rb', line 182

def track_action(action_url, action_type)
  @piwik.track request_params.merge(action_type => action_url, :redirect => '0')
end

#track_goal(goal_id, revenue = nil) ⇒ Object

Records a Goal conversion goal_id - Id of the goal to record revenue - revenue for this conversion



173
174
175
176
177
# File 'lib/piwik_tracker/piwik.rb', line 173

def track_goal(goal_id, revenue = nil)
  params = request_params.merge :idgoal => goal_id
  params[:revenue] = revenue if revenue
  @piwik.track params
end

#track_pageview(document_title) ⇒ Object

Tracks a page view document_title is the page title as it will appear in the Actions > Page titles report



166
167
168
# File 'lib/piwik_tracker/piwik.rb', line 166

def track_pageview(document_title)
  @piwik.track request_params.merge(:action_name => document_title)
end

#url(url) ⇒ Object

Sets the current URL being tracked



93
94
95
96
# File 'lib/piwik_tracker/piwik.rb', line 93

def url(url)
  @data[:url] = url
  self
end

#user_agent(name) ⇒ Object



129
130
131
132
# File 'lib/piwik_tracker/piwik.rb', line 129

def user_agent(name)
  @data[:user_agent] = name
  self
end

#visitor_id(id) ⇒ Object

Forces the requests to be recorded for the specified Visitor ID rather than using the heuristics based on IP and other attributes. id - 16 hexadecimal characters visitor ID, eg. “33c31e01394bdc63”



153
154
155
156
157
# File 'lib/piwik_tracker/piwik.rb', line 153

def visitor_id(id)
  raise "visitor_id must be exactly #{VISITOR_ID_LENGTH} characters long" unless id.to_s.length == VISITOR_ID_LENGTH
  @data[:cid] = id
  self
end