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
42 lines (34 loc) · 1.03 KB
/
Copy pathgoogle_analytics.rb
File metadata and controls
42 lines (34 loc) · 1.03 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
class Rack::Tracker::GoogleAnalytics < Rack::Tracker::Handler
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 Ecommerce < OpenStruct
def write
["ecommerce:#{self.type}", self.to_h.except(:type).compact.stringify_values].to_json.gsub(/\[|\]/, '')
end
end
def tracker
options[:tracker].try(:call, env) || options[:tracker]
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 self.track(name, *event)
{ name.to_s => [const_get(event.first.to_s.capitalize).new(event.last)] }
end
end