forked from railslove/rack-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_analytics.rb
More file actions
84 lines (68 loc) · 2.05 KB
/
google_analytics.rb
File metadata and controls
84 lines (68 loc) · 2.05 KB
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
class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler
ALLOWED_TRACKER_OPTIONS = [:cookie_domain, :user_id]
class Send < OpenStruct
def initialize(attrs = {})
attrs.reverse_merge!(type: 'event')
super
end
def write
['send', event].to_json.gsub(/\[|\]/, '')
end
def event
{ hitType: self.type }.merge(attributes.stringify_values).compact
end
def attributes
Hash[to_h.slice(:category, :action, :label, :value).map { |k,v| [self.type.to_s + k.to_s.capitalize, v] }]
end
end
class EnhancedEcommerce < OpenStruct
def write
hash = self.to_h
label = hash[:label]
attributes = hash.except(:label, :type).compact.stringify_values
[
"ec:#{self.type}",
label,
attributes.empty? ? nil : attributes
].compact.to_json.gsub(/\[|\]/, '')
end
end
class Ecommerce < OpenStruct
def write
attributes = self.to_h.except(:type).compact.stringify_values
[
"ecommerce:#{self.type}",
attributes
].to_json.gsub(/\[|\]/, '')
end
end
class Parameter < OpenStruct
def write
["set", self.to_h.to_a].flatten.map {|s| "'#{s}'" }.join(', ')
end
end
def tracker
options[:tracker].respond_to?(:call) ? options[:tracker].call(env) : options[:tracker]
end
def tracker_options
@tracker_options ||= {}.tap do |tracker_options|
options.slice(*ALLOWED_TRACKER_OPTIONS).each do |key, value|
if option_value = value.respond_to?(:call) ? value.call(env) : value
tracker_options[key.to_s.camelize(:lower).to_sym] = option_value.to_s
end
end
end
end
def render
Tilt.new( File.join( File.dirname(__FILE__), 'template', 'google_analytics.erb') ).render(self)
end
def ecommerce_events
events.select {|e| e.kind_of?(Ecommerce) }
end
def enhanced_ecommerce_events
events.select {|e| e.kind_of?(EnhancedEcommerce) }
end
def self.track(name, *event)
{ name.to_s => [event.last.merge('class_name' => event.first.to_s.classify)] }
end
end