lib/ahoy/model.rb in ahoy_matey-0.1.0 vs lib/ahoy/model.rb in ahoy_matey-0.1.1

- old
+ new

@@ -10,71 +10,80 @@ before_create :set_utm_parameters before_create :set_technology 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 landing_uri = Addressable::URI.parse(landing_page) rescue nil 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 end