Skip to content

Instantly share code, notes, and snippets.

@migrs
Created August 18, 2010 01:54
Show Gist options
  • Save migrs/533053 to your computer and use it in GitHub Desktop.
Save migrs/533053 to your computer and use it in GitHub Desktop.
Google Analytics Mobile for Rails Metal
# 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
@migrs
Copy link
Author

migrs commented Aug 18, 2010

Ruby on RailsでGoogle Analytics携帯対応: りめも~んRC を参考にさせていただきました。
jpmobile は必要ありません。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment