Skip to content

Commit

Permalink
splittet to model and controller
Browse files Browse the repository at this point in the history
  • Loading branch information
Maik Vlcek committed Aug 23, 2011
1 parent 0237b08 commit d4734d8
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 114 deletions.
41 changes: 41 additions & 0 deletions lib/ga_controllers/tracking_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module GATracking
module TrackingController

def ga_tracker
@ga_tracker ||= begin
GATracker.new(ga_params)
end
end

protected

def ga_visitor_id
"#{request.env["HTTP_USER_AGENT"]}#{rand(0x7fffffff).to_s}#{GATracker.account_id}"
end

def ga_params
# domain specific stuff
domain_name = (request.env["SERVER_NAME"].nil? || request.env["SERVER_NAME"].blank?) ? "" : request.env["SERVER_NAME"]
referer = request.env['HTTP_REFERER']
path = request.env["REQUEST_URI"]

# Capture the first three octects of the IP address and replace the forth
# with 0, e.g. 124.455.3.123 becomes 124.455.3.0
remote_address = request.env["REMOTE_ADDR"].to_s
ip = (remote_address.nil? || remote_address.blank?) ? '' : remote_address.gsub!(/([^.]+\.[^.]+\.[^.]+\.)[^.]+/, "\\1") + "0"

visitor_uuid = Digest::MD5.hexdigest(ga_visitor_id)

{
:utmhn => CGI.escape(domain_name),
:utmr => CGI.escape(referer),
:utmp => CGI.escape(path),
:utmip => ip,
:utmcc => '__utma%3D999.999.999.999.999.1%3B',
:utmvid => visitor_uuid
}
end


end
end
143 changes: 30 additions & 113 deletions lib/ga_tracker.rb
Original file line number Diff line number Diff line change
@@ -1,138 +1,55 @@
require 'cgi'
require 'digest'
require 'open-uri'

class GATracker
class GA::Tracker

attr_accessor :params, :request, :visitor_id

def initialize(request, params, use_ssl=false)
self.request = request.dup
self.params = params.dup
self.utm_location = (use_ssl ? 'https://ssl' : 'http://www') + UTM_GIF_LOCATION
self.utm_params = utm_query_params
def initialize(params, use_ssl=false)
@utm_params = extend_with_default_params(params)
@utm_location = (use_ssl ? 'https://ssl' : 'http://www') + UTM_GIF_LOCATION
end

def set_custom_var(index, name, value, scope=nil)
# update a custom var parameter
utm_params[:utme].set_custom_variable(index, name, value, scope)
def track
utm_url = @utm_location + "?" + @utm_params.to_query

puts "--------sending request to GA-----------------------"
puts utm_url
#open(utm_url, "User-Agent" => request.env["HTTP_USER_AGENT"],
# "Header" => ("Accepts-Language: " + request.env["HTTP_ACCEPT_LANGUAGE"]))
end

def track_event(category, action, label=nil, value=nil)
# update a custom var parameter
utm_params[:utme].set_event(category, action, label, value)

# make google request
request_google utm_params
@utm_params[:utme].set_event(category, action, label, value)
track
end

def track_page_view(path=nil)
request_google utm_params
@utm_params.merge({ :utmp => path }) if path
track
end

protected

attr_accessor :utm_location, :utm_params

# Generate a visitor id for this hit.
# If there is a visitor id in the cookie, use that, otherwise
# use the guid if we have one, otherwise use a random number.
def get_visitor_id(guid, account, user_agent, cookie)
# if there was a visitor id set manually, use it
return visitor_id unless (visitor_id.nil? || visitor_id.empty?)

# If there is a value in the cookie, don't change it.
return cookie unless (cookie.nil? || cookie.empty?)

message = ""
unless (guid.nil? || guid.empty?)
# Create the visitor id using the guid.
message = guid + account
else
# otherwise this is a new user, create a new random id.
#message = useragent + uniqid(getrandomnumber(), true)
message = user_agent + get_random_number.to_s
end

md5string = Digest::MD5.hexdigest(message)
def set_custom_var(index, name, value, scope=nil)
@utm_params[:utme].set_custom_variable(index, name, value, scope)
end

"0x" + md5string[0, 16]
class << self
attr_accessor :account_id
end

private

# seems to be the current version
# just search for 'utmwv' in http://www.google-analytics.com/ga.js
# search for 'utmwv' in http://www.google-analytics.com/ga.js
VERSION = "5.1.5"
COOKIE_NAME = "__utmmobile"
COOKIE_PATH = "/"

# Two years in seconds.
COOKIE_PERSISTENCE = 63072000
UTM_GIF_LOCATION = ".google-analytics.com/__utm.gif"

# The last octect of the IP address is removed to anonymize the user.
def get_ip(remote_address)
return '' if (remote_address.nil? || remote_address.blank?)

# Capture the first three octects of the IP address and replace the forth
# with 0, e.g. 124.455.3.123 becomes 124.455.3.0
remote_address.to_s.gsub!(/([^.]+\.[^.]+\.[^.]+\.)[^.]+/, "\\1") + "0"
end

def utm_query_params
#timestamp = Time.now.utc.strftime("%H%M%S").to_i

domain_name = (request.env["SERVER_NAME"].nil? || request.env["SERVER_NAME"].blank?) ? "" : request.env["SERVER_NAME"]

# Get the referrer from the utmr parameter, this is the referrer to the
# page that contains the tracking pixel, not the referrer for tracking
# pixel.
document_referer = params[:utmr]
if (document_referer.nil? || (document_referer.empty? && document_referer != "0"))
document_referer = "-"
else
document_referer = CGI.unescape(document_referer)
end
document_path = params[:utmp].blank? ? "" : CGI.unescape(params[:utmp])

account = params[:utmac].blank? ? "ua-1" : params[:utmac]
user_agent = (request.env["HTTP_USER_AGENT"].nil? || request.env["HTTP_USER_AGENT"].empty?) ? "" : request.env["HTTP_USER_AGENT"]

# Try and get visitor cookie from the request.
cookie = nil #cookies[COOKIE_NAME]

visitor_id = get_visitor_id(request.env["HTTP_X_DCMGUID"], account, user_agent, cookie)

# Always try and add the cookie to the response.
#request.cookies[COOKIE_NAME] = { :value => visitor_id, :expires => COOKIE_PERSISTENCE.to_i + timestamp, :path => COOKIE_PATH }

# Construct the gif hit url params
{
:utmwv => VERSION,
:utmn => rand(0x7fffffff).to_s,
:utmhn => CGI.escape(domain_name),
:utme => UTME.new,
:utmr => CGI.escape(document_referer),
:utmp => CGI.escape(document_path),
:utmac => account,
:utmcc => '__utma%3D999.999.999.999.999.1%3B',
:utmvid => visitor_id,
:utmip => get_ip(request.env["REMOTE_ADDR"])
}
end

# Make a tracking request to Google Analytics from this server.
# Copies the headers from the original request to the new one.
# If request containg utmdebug parameter, exceptions encountered
# communicating with Google Analytics are thown.
def request_google(params)
utm_url = @utm_location + "?" + params.to_query

puts "--------sending request to GA-----------------------"
puts utm_url
#open(utm_url, "User-Agent" => request.env["HTTP_USER_AGENT"],
# "Header" => ("Accepts-Language: " + request.env["HTTP_ACCEPT_LANGUAGE"]))
# adds default params
def extend_with_default_params(params)
params.reverse_merge({
:utme => Utme.parse(params[:utme]),
:utmwv => VERSION,
:utmn => rand(0x7fffffff).to_s,
:utmac => GATracker.account_id
})
params
end

end
Expand Down
20 changes: 19 additions & 1 deletion lib/ga_tracker/utme.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class GATracker::UTME
class GATracker::Utme

def initialize
@custom_variables = CustomVariables.new
Expand All @@ -23,6 +23,24 @@ def to_s
@event.to_s + @custom_variables.to_s
end

class << self
def parse(args)
case args.class
when String
utma_obj = self.from_string(args)
when GATracker::Utma
utma_obj = args
else
raise ArgumentError('Cannot parse objects other than of type String or GATracker::Utma')
end
utma_obj
end

def from_string(str)
raise NotImplementedError("Would like to parse #{str}, but no one implemented this so far")
end
end

private

Event = Struct.new(:category, :action, :opt_label, :opt_value) do
Expand Down

0 comments on commit d4734d8

Please sign in to comment.