Separa heatmap mobile/desktop, opt-out analytics per operatori, dashboard costi con KPI e trend mensili. Co-authored-by: Cursor <cursoragent@cursor.com>
46 lines
1.2 KiB
Ruby
46 lines
1.2 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Analytics
|
|
class EventsController < ActionController::API
|
|
include ActionController::Cookies
|
|
|
|
def create
|
|
if analytics_suppressed?
|
|
return render json: { accepted: 0, rejected: 0, suppressed: true }, status: :accepted
|
|
end
|
|
|
|
payload = parse_payload
|
|
result = Analytics::Ingest.new(events: payload, remote_ip: request.remote_ip).call
|
|
|
|
if result.rate_limited
|
|
return head :too_many_requests
|
|
end
|
|
|
|
render json: { accepted: result.accepted, rejected: result.rejected }, status: :accepted
|
|
end
|
|
|
|
private
|
|
|
|
def analytics_suppressed?
|
|
Analytics::Suppress.active?(cookies[Analytics::Suppress::COOKIE_NAME])
|
|
end
|
|
|
|
def parse_payload
|
|
body = request.request_parameters
|
|
return body["events"] if body.is_a?(Hash) && body["events"].is_a?(Array)
|
|
return body if body.is_a?(Array)
|
|
|
|
raw = request.raw_post
|
|
return [] if raw.blank?
|
|
|
|
parsed = JSON.parse(raw)
|
|
return parsed["events"] if parsed.is_a?(Hash) && parsed["events"].is_a?(Array)
|
|
return parsed if parsed.is_a?(Array)
|
|
|
|
[]
|
|
rescue JSON::ParserError
|
|
[]
|
|
end
|
|
end
|
|
end
|