More flexibility w/ fields and performance hack for referer-parser
Andrew Kane
committed Mar 20, 2014
commit 4a8486f78218d71d35562a787a6812ffa2f12ac0
Showing 4
changed files with
67 additions
and 49 deletions
app/controllers/ahoy/visits_controller.rb
+5
-5
| @@ | @@ -7,11 +7,11 @@ module Ahoy |
| Ahoy.visit_model.new do |v| | |
| v.visit_token = params[:visit_token] | |
| v.visitor_token = params[:visitor_token] | |
| - | v.ip = request.remote_ip |
| - | v.user_agent = request.user_agent |
| - | v.referrer = params[:referrer] |
| - | v.landing_page = params[:landing_page] |
| - | v.user = current_user if respond_to?(:current_user) |
| + | v.ip = request.remote_ip if v.respond_to?(:ip=) |
| + | v.user_agent = request.user_agent if v.respond_to?(:user_agent=) |
| + | v.referrer = params[:referrer] if v.respond_to?(:referrer=) |
| + | v.landing_page = params[:landing_page] if v.respond_to?(:landing_page=) |
| + | v.user = current_user if respond_to?(:current_user) and v.respond_to?(:user=) |
| end | |
| visit.save! | |
ahoy/model.rb b/lib/ahoy/model.rb
+52
-43
| @@ | @@ -12,10 +12,12 @@ module Ahoy |
| before_create :set_location | |
| def set_traffic_source | |
| - | referring_uri = Addressable::URI.parse(referrer) rescue nil |
| - | self.referring_domain = referring_uri.try(:host) |
| - | search_keyword = RefererParser::Referer.new(referrer).search_term rescue nil |
| - | self.search_keyword = search_keyword.present? ? search_keyword : nil |
| + | referring_domain = Addressable::URI.parse(referrer).host.first(255) rescue nil |
| + | self.referring_domain = referring_domain if respond_to?(:referring_domain=) |
| + | # performance hack for referer-parser |
| + | search_keyword = Ahoy.referrer_parser.parse(referrer)[1].first(255) rescue nil |
| + | self.search_keyword = search_keyword.presence if respond_to?(:search_keyword=) |
| + | true |
| end | |
| def set_utm_parameters | |
| @@ | @@ -23,57 +25,64 @@ module Ahoy |
| if landing_uri | |
| query_values = landing_uri.query_values || {} | |
| %w[utm_source utm_medium utm_term utm_content utm_campaign].each do |name| | |
| - | self[name] = query_values[name] |
| + | self[name] = query_values[name] if respond_to?(:"#{name}=") |
| end | |
| end | |
| + | true |
| end | |
| def set_technology | |
| - | browser = Browser.new(ua: user_agent) |
| + | if respond_to?(:user_agent) |
| + | browser = Browser.new(ua: user_agent) |
| - | self.browser = browser.name |
| + | self.browser = browser.name if respond_to?(:browser=) |
| - | # TODO add more |
| - | self.os = |
| - | if browser.android? |
| - | "Android" |
| - | elsif browser.ios? |
| - | "iOS" |
| - | elsif browser.windows_phone? |
| - | "Windows Phone" |
| - | elsif browser.blackberry? |
| - | "Blackberry" |
| - | elsif browser.chrome_os? |
| - | "Chrome OS" |
| - | elsif browser.mac? |
| - | "Mac" |
| - | elsif browser.windows? |
| - | "Windows" |
| - | elsif browser.linux? |
| - | "Linux" |
| - | end |
| + | # TODO add more |
| + | self.os = |
| + | if browser.android? |
| + | "Android" |
| + | elsif browser.ios? |
| + | "iOS" |
| + | elsif browser.windows_phone? |
| + | "Windows Phone" |
| + | elsif browser.blackberry? |
| + | "Blackberry" |
| + | elsif browser.chrome_os? |
| + | "Chrome OS" |
| + | elsif browser.mac? |
| + | "Mac" |
| + | elsif browser.windows? |
| + | "Windows" |
| + | elsif browser.linux? |
| + | "Linux" |
| + | end if respond_to?(:os=) |
| - | self.device_type = |
| - | if browser.tv? |
| - | "TV" |
| - | elsif browser.console? |
| - | "Console" |
| - | elsif browser.tablet? |
| - | "Tablet" |
| - | elsif browser.mobile? |
| - | "Mobile" |
| - | else |
| - | "Desktop" |
| - | end |
| + | self.device_type = |
| + | if browser.tv? |
| + | "TV" |
| + | elsif browser.console? |
| + | "Console" |
| + | elsif browser.tablet? |
| + | "Tablet" |
| + | elsif browser.mobile? |
| + | "Mobile" |
| + | else |
| + | "Desktop" |
| + | end if respond_to?(:device_type=) |
| + | end |
| + | true |
| end | |
| def set_location | |
| - | location = Geocoder.search(ip).first rescue nil |
| - | if location |
| - | self.country = location.country.presence |
| - | self.region = location.state.presence |
| - | self.city = location.city.presence |
| + | if respond_to?(:ip) and [:country=, :region=, :city=].any?{|method| respond_to?(method) } |
| + | location = Geocoder.search(ip).first rescue nil |
| + | if location |
| + | self.country = location.country.presence if respond_to?(:country=) |
| + | self.region = location.state.presence if respond_to?(:region=) |
| + | self.city = location.city.presence if respond_to?(:city=) |
| + | end |
| end | |
| + | true |
| end | |
| end # end class_eval | |
ahoy_matey.rb b/lib/ahoy_matey.rb
+6
-0
| @@ | @@ -14,6 +14,12 @@ module Ahoy |
| ::Visit | |
| end | |
| + | # TODO private |
| + | # performance hack for referer-parser |
| + | def self.referrer_parser |
| + | @referrer_parser ||= RefererParser::Referer.new("https://github.com/ankane/ahoy") |
| + | end |
| + | |
| end | |
| ActionController::Base.send :include, Ahoy::Controller | |
generators/ahoy/templates/install.rb b/lib/generators/ahoy/templates/install.rb
+4
-1
| @@ | @@ -1,10 +1,13 @@ |
| class <%= migration_class_name %> < ActiveRecord::Migration | |
| def change | |
| create_table :visits do |t| | |
| - | # cookies |
| + | # cookies *required* |
| t.string :visit_token | |
| t.string :visitor_token | |
| + | # the rest are recommended but optional |
| + | # simply remove the columns you don't want |
| + | |
| # standard | |
| t.string :ip | |
| t.text :user_agent | |