Sha256: a951ee53c161874655b2b9df1fbc7dc06442b3111ce192dbaec168d171de19e3

Contents?: true

Size: 925 Bytes

Versions: 1

Compression:

Stored size: 925 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

		# takes Array of Event as parameter
		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.02 lib/gaevents.rb