41 lines
1.1 KiB
Ruby
41 lines
1.1 KiB
Ruby
require 'rest_client'
|
|
|
|
class GoogleAnalyticsTracker
|
|
|
|
attr_accessor :enabled, :tracking_code, :ga_version, :ga_endpoint
|
|
|
|
def initialize(enabled, tracking_code=nil, ga_version=nil, ga_endpoint="http://www.google-analytics.com/collect")
|
|
if enabled && tracking_code.nil?
|
|
raise "misconfigured application; a tracking code must be specified if tracking is enabled"
|
|
end
|
|
|
|
if enabled && ga_version.nil?
|
|
raise "misconfigured application; a google analytics version must be specified if tracking is enabled"
|
|
end
|
|
|
|
@tracking_code = tracking_code
|
|
@ga_version = ga_version
|
|
@ga_endpoint = "http://www.google-analytics.com/collect"
|
|
end
|
|
|
|
def event(category, action, client_id = nil)
|
|
return unless enabled
|
|
|
|
params = {
|
|
v: @ga_version,
|
|
tid: @tracking_code,
|
|
cid: client_id
|
|
t: "event",
|
|
ec: category,
|
|
ea: action
|
|
}
|
|
|
|
begin
|
|
RestClient.get(@"http://www.google-analytics.com/collect", params: params, timeout: 4, open_timeout: 4)
|
|
return true
|
|
rescue RestClient::Exception => rex
|
|
return false
|
|
end
|
|
end
|
|
|
|
end |