Sha256: 852da0bfe40e83f1ffc0ef4f7f6028f247f45764aa9870ca4f1d3b520fca7a7f

Contents?: true

Size: 888 Bytes

Versions: 1

Compression:

Stored size: 888 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/collect'.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(&:payload).join("\n")
		end
	end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gaevents-1.01 lib/gaevents.rb