Proxy caching can break backend tracking

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the web-development category.

Last Updated: 2024-04-18

First some context. The website had some code follows to look at some marketing parameters in the URL and store them in the session:

def store_utm_params
  tracking_params = %i[
    utm_source
    utm_medium
    utm_campaign
    utm_content
  ]

  return unless (params.keys.map(&:to_sym) & tracking_params).present?

  session['tracking_info'] = tracking_params.each_with_object({}) do |tracking_param, accum|
    accum[tracking_param] = params[tracking_param]
  end
end

Now this system was behind a HTTP proxy cache in memcached right in front of my server. This turned out to be a bad idea from the point of view of tracking these marketing parameters, since many requests never hit my actual server. Therefore the code to set session information never fired and the info was lost.

I would need either to forward these key marketing parameters to my server with a background JavaScript request or use an alternative caching system (one on my web-server) to get around this issue.