Class: GAEvents
- Inherits:
-
Object
- Object
- GAEvents
- Defined in:
- lib/gaevents.rb,
lib/gaevents/event.rb
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
-
.api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
- .send_event(event) ⇒ Object
-
.track(events) ⇒ Object
takes Array of Event as parameter.
- .track_body(*events) ⇒ Object
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
10 11 12 |
# File 'lib/gaevents.rb', line 10 def api_key @api_key end |
Class Method Details
.send_event(event) ⇒ Object
12 13 14 15 |
# File 'lib/gaevents.rb', line 12 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
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gaevents.rb', line 18 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) { |_events| resource = RestClient::Resource.new BULK_URI res << resource.post(track_body(_events.compact), {:content_type => 'text/plain'}) } res end |
.track_body(*events) ⇒ Object
29 30 31 |
# File 'lib/gaevents.rb', line 29 def track_body(*events) events.flatten.map(&:payload).join("\n") end |