Maybe this would be simpler:
data[:cgi_data] = @cgi_data || {}
data[:cgi_data] = data[:cgi_data].merge(:agent_class => agent.class.name) if agent