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
|
# File 'lib/ahoy/model.rb', line 4
def ahoy_visit
class_eval do
belongs_to :user, polymorphic: true
before_create :set_traffic_source
before_create :set_utm_parameters
before_create :set_technology
before_create :set_location
def set_traffic_source
referring_domain = Addressable::URI.parse(referrer).host.first(255) rescue nil
self.referring_domain = referring_domain if respond_to?(:referring_domain=)
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
%w[utm_source utm_medium utm_term utm_content utm_campaign].each do |name|
self[name] = landing_params[name] if respond_to?(:"#{name}=")
end
true
end
def set_technology
if respond_to?(:user_agent)
agent = Ahoy.user_agent_parser.parse(user_agent)
self.browser = agent.name if respond_to?(:browser=)
self.os = agent.os.name if respond_to?(:os=)
browser = Browser.new(ua: user_agent)
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
if respond_to?(:ip) and [:country=, :region=, :city=].any?{|method| respond_to?(method) }
location =
begin
Geocoder.search(ip).first
rescue => e
$stderr.puts e.message
nil
end
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
def landing_params
@landing_params ||= begin
landing_uri = Addressable::URI.parse(landing_page) rescue nil
ActiveSupport::HashWithIndifferentAccess.new((landing_uri && landing_uri.query_values) || {})
end
end
end
end
|