-- networking : (table) our http handler for making POST requests
-- trackingId : (string)
-- clientId : (string)
-- userId : (string)
-- eventArgs : (table) event specific arguments

local function sendReport(networking, trackingId, clientId, userId, eventArgs)
	local url = "https://www.google-analytics.com/collect"
	--local url = "https://www.google-analytics.com/debug/collect"

	-- sanitize some input
	local function exists(variableName, value)
		local wasFound = (value ~= nil)
		if not wasFound then
			warn("Google Analytics cannot send reports without a " .. variableName)
		end
		return wasFound
	end
	if not (exists("tracking ID", trackingId) and
		exists("userId", userId) and
		exists("clientId", clientId) and
		exists("event arguments", eventArgs)) then
		return
	end

	local params = {}
	for k,v in pairs(eventArgs) do
		table.insert(params, string.format("%s=%s",k,v))
	end

	-- add some required arguments
	table.insert(params, string.format("v=%d", VERSION))		-- version
	table.insert(params, string.format("tid=%s", trackingId))	-- property Id
	table.insert(params, string.format("ds=%s", PLACE_ID))		-- data source
	table.insert(params, string.format("cid=%s", clientId))		-- guid to identify this specific client
	table.insert(params, string.format("uid=%s", userId))		-- unique, anonymous user id. This should be last


	-- construct the post body data
	local paramString = table.concat(params, "&")

	-- report the event to GA
	return networking:POST(url, paramString)
end

