Class: Rack::Tracker::GoogleAnalytics::Send

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/rack/tracker/google_analytics/google_analytics.rb

Instance Method Summary collapse

Methods inherited from OpenStruct

#to_h

Constructor Details

#initialize(attrs = {}) ⇒ Send

Returns a new instance of Send.



3
4
5
6
# File 'lib/rack/tracker/google_analytics/google_analytics.rb', line 3

def initialize(attrs = {})
  attrs.reverse_merge!(type: 'event')
  super
end

Instance Method Details

#attributesObject



16
17
18
# File 'lib/rack/tracker/google_analytics/google_analytics.rb', line 16

def attributes
  Hash[to_h.slice(:category, :action, :label, :value).map { |k,v| [self.type.to_s + k.to_s.capitalize, v] }]
end

#eventObject



12
13
14
# File 'lib/rack/tracker/google_analytics/google_analytics.rb', line 12

def event
  { hitType: self.type }.merge(attributes.stringify_values).compact
end

#writeObject



8
9
10
# File 'lib/rack/tracker/google_analytics/google_analytics.rb', line 8

def write
  ['send', event].to_json.gsub(/\[|\]/, '')
end