public
Created — forked from migrs/google_analytics.rb

Google Analytics Mobile for Rails Metal

  • Download Gist
google_analytics.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
 
class GoogleAnalytics
extend Rack::Utils
 
G_COOKIE_NAME = '__utmmobile'
GIF_DATA = "GIF89a\001\000\001\000\200\000\000\377\377\377\377\377\377!\371\004\001\n\000\001\000,\000\000\000\000\001\000\001\000\000\002\002L\001\000;"
UTM_GIF_LOCATION = "http://www.google-analytics.com/__utm.gif";
 
def self.call(env)
if env["PATH_INFO"] =~ /^\/ga$/
ga_mobile_response(env)
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
 
def self.logger
Rails.logger
end
 
def self.ga_mobile_response(env)
request = Rack::Request.new(env)
 
document_referer = request.params['utmr'].blank? ? '-' : escape(request.params['utmr'])
document_path = request.params['utmp'].blank? ? '' : escape(request.params['utmp'])
 
account = request.params['utmac']
user_agent = request.user_agent.to_s
domain_name = env['SERVER_NAME']
 
#remote_address = env['REMOTE_ADDR']
remote_address = env['HTTP_X_FORWARDED_FOR']
if remote_address.blank?
remote_address = ''
elsif remote_address =~ /^([^.]+\.[^.]+\.[^.]+\.).*/
remote_address = "#{$1}0"
else
remote_address = ''
end
 
visitor_id = request.cookies[G_COOKIE_NAME]
 
if visitor_id.blank?
guid = env['HTTP_X_DCMGUID'] || env['HTTP_X_UP_SUBNO'] || env['HTTP_X_JPHONE_UID'] || env['HTTP_X_EM_UID']
 
message = ''
if guid.blank?
message = "#{user_agent}#{Digest::SHA1.hexdigest(rand.to_s)}#{Time.now.to_i}"
else
message = "#{guid}#{account}"
end
 
md5string = Digest::MD5.hexdigest(message)
 
visitor_id = "0x#{md5string[0,16]}"
end
 
queries = []
queries << "utmwv=4.4sh"
queries << "utmn=#{rand(1000000*1000000)}"
queries << "utmhn=#{escape(domain_name)}"
queries << "utmr=#{escape(document_referer)}"
queries << "utmp=#{escape(document_path)}"
queries << "utmac=#{account}"
queries << "utmcc=__utma%3D999.999.999.999.999.1%3B"
queries << "utmvid=#{visitor_id}"
queries << "utmip=#{remote_address}"
 
uri = URI.parse(UTM_GIF_LOCATION + '?' + queries.join('&'))
 
unless ENV['RAILS_ENV'] == 'test'
Net::HTTP.version_1_2
Net::HTTP.start(uri.host, uri.port) do |http|
http.get(uri.request_uri, {
"user_agent" => request.user_agent.to_s,
"Accepts-Language" => env['HTTP_ACCEPT_LANGUAGE'].to_s
})
end
logger.debug "-----Send Request To Google Analytics-----"
logger.debug uri.to_s
logger.debug "------------------------------------------"
end
 
headers = {}
headers["X-GA-MOBILE-URL"] = uri.to_s unless request.params['utmdebug'].blank?
headers["Cache-Control"] = "private, no-cache, no-cache=Set-Cookie, proxy-revalidate"
headers["Pragma"] = "no-cache"
headers["Expires"] = "Wed, 17 Sep 1975 21:32:10 GMT"
 
options = env['rack.session.options']
cookies = {
:value => visitor_id,
:expires => 2.years.from_now,
:path => "/",
:domain => domain_name
}
 
response = Rack::Response.new(GIF_DATA, 200, headers)
response.set_cookie(G_COOKIE_NAME, cookies.merge(options))
response.finish
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.