Class: GAEvents

Inherits:
Object
  • Object
show all
Defined in:
lib/gaevents.rb,
lib/gaevents/event.rb

Overview

collection of events

Defined Under Namespace

Classes: Event

Constant Summary collapse

BULK_URI =
'https://www.google-analytics.com/batch'.freeze
COLLECT_URI =
'https://www.google-analytics.com/collect'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.api_keyObject

Returns the value of attribute api_key.



11
12
13
# File 'lib/gaevents.rb', line 11

def api_key
  @api_key
end

Class Method Details

.send_event(event) ⇒ Object



13
14
15
16
# File 'lib/gaevents.rb', line 13

def send_event(event)
	 resource = RestClient::Resource.new COLLECT_URI
	 resource.post(track_body([event]), content_type: 'text/plain')
end

.track(events) ⇒ Object

takes Array of Event as parameter



19
20
21
22
23
24
25
26
27
28
# File 'lib/gaevents.rb', line 19

def track(events)
	 # as per GA: A maximum of 20 hits can be specified per request
	 # https://developers.google.com/analytics/devguides/collection/protocol/v1/devguide#batch
	 res = []
	 events.each_slice(20) do |events_slice|
			 resource = RestClient::Resource.new BULK_URI
			 res << resource.post(track_body(events_slice.compact), content_type: 'text/plain')
		end
	 res
end

.track_body(*events) ⇒ Object



30
31
32
# File 'lib/gaevents.rb', line 30

def track_body(*events)
	 events.flatten.map(&:payload).join("\n")
end