Catching UTM Params in Rails

2019-04-11 11:36发布

问题:

I am trying to catch the UTM Params in the URL to add Source, Campaign etc to a User Account.
Sadly, I can't seem to figure out how to catch those params. As of know I following the Blog Article http://www.matthuggins.com/articles/tracking-new-user-registrations-by-source-search-terms

So, in my Application Controller I have following:

ApplicationController.class_eval do
  before_filter :capture_referrer

  protected
    def capture_referrer
      session[:referrer] = request.env['HTTP_REFERER'] if !session[:referrer]
    end
end

In the create Action in the user controller

@user.referrer = session[:referrer]

and in the USer Model itself:

 def set_traffic_source
  if self.referrer
    url = URI.parse(self.referrer)
    self.source ||= uri.host.downcase.gsub(/^www\./, '')
    self.traffic_keywords ||= search_termins(uri)
  end
end

This all works fine, for catching the referer - But I actualy want to read out the UTMs passed into by the URI. How would I go about this?

回答1:

Use params to access them:

params[:utm_source]
params[:utm_campaign]
params[:utm_medium]