Sha256: 58458193ad30bc2f44a7e973e05a0a708e99cdd4ac6dece617ce61b22d15e015

Contents?: true

Size: 912 Bytes

Versions: 2

Compression:

Stored size: 912 Bytes

Contents

require "gaevents/version"
require "rest-client"
require_relative "gaevents/event"

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

	class << self
		attr_accessor :api_key

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

		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

		def track_body(*events)
			events.flatten.map { |_event| _event.payload(api_key) }.join("\n")
		end
	end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
gaevents-1.0 lib/gaevents.rb
gaevents-0.1.1 lib/gaevents.rb