Catching UTM Params in Rails - ruby-on-rails

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?

Use params to access them:
params[:utm_source]
params[:utm_campaign]
params[:utm_medium]

Related

Rails routes url take out user_id

I currently have the standard
get 'profile/:id/view', to: 'profile#view', as: 'view'
which produces http://localhost:3000/profile/233/view
the controller is
def view
#user = User.find(params[:id])
end
How can I make a path that the url is http://localhost:3000/profile/view - leaving out the URL?
I was trying to do something like
def view(user_id)
#user = User.find(user_id)
end
But I'm not sure how to write that in the routes, or what the link_to path would look like for that?
I have been looking for a straight forward answer, but have not been able to find one.
Thanks in advance!
You can change the route to
get 'profile/view', to: 'profile#view'
And then in the controller, you can user current_user method which devise provides.
def view
#user = current_user
end

how to post to form from within an action

I have a link that goes to an action, so if someone clicks:
localhost/cart/checkout?pid=123
It goes to the CartController checkout action which then displays a form.
But in some circumstances (depending on when I load the Product with id 123) I may not need to display the form, I can just load the data and then post to the form's action.
How can I programatically post to where my form was going to post with data.
class CartController < ApplicationController
def checkout
pid = params[:pid]
product = Product.find(pid)
if product....
# no need to display view, just post to handleCheckout
end
end
# checkout form posts to this action
def handleCheckout
end
end
I have not done something like this before but I have some idea so please note that none of the is tested.
If your handleCheckout action is meant to be used as a Get request then you can redirect to this action with the params. like:
class CartController < ApplicationController
def checkout
pid = params[:pid]
product = Product.find(pid)
if product....
redirect_to action: "handleCheckout", params: params
# Not sure whether you will get it as 'params' or params[:params] in handleCheckout action
end
end
# checkout form posts to this action
def handleCheckout
end
end
And if handleCheckout is meant to be used as post Then above method might not work since redirect_to will create a new http Get request to that action. so you may try something like this:
def checkout
pid = params[:pid]
product = Product.find(pid)
if product....
handleCheckout
# params since is a global hash and above method has access to it
end
end
# checkout form posts to this action
def handleCheckout
# your other code
redirect_to 'some_action' and return
# in above line you have to return with a render or redirect
# Otherwise it will render 'checkout' template with render and redirect or
# it will throw double render error if you have a simple render or redirect without explicit return
end
As I mentioned, I have not tried any of above code. There might be errors. I hope it helps.

(Rails) How to get 'id' out of edit url

I have a model called studies.
After action redirect redirect_to edit_study_path(#new_study),
URL: http://localhost:3000/studies/2/edit.
Is there anyway to customize an url after passing id ?
For example, http://localhost:3000/study
(still going to the edit path, and still with the :id in the params)
I guess what you want is to edit the current study?
In this case, it's possible, using ressource instead of ressources in the routes.
Let's have an example:
#in routes.rb
resources :studies
resource :study
Both of them will by default link to the StudiesController and call the same actions (eg. edit in your case) but in two different routes
get "/studies/:id/edit" => "studies#edit"
get "/study/edit" => "studies#edit"
in your edit action, you should then setup to handle correctly the parameters:
def edit
#study = params[:id].nil? ? current_study : Study.find(params[:id])
end
Note you need a current_study method somewhere, and store the current_study in cookies/sessions to make it works.
Example:
# In application_controller.rb
def current_study
#current_study ||= Study.find_by(id: session[:current_study_id]) #using find_by doesn't raise exception if doesn't exists
end
def current_study= x
#current_study = x
session[:current_study_id] = x.id
end
#... And back to study controller
def create
#...
#Eg. setup current_study and go to edit after creation
if study.save
self.current_study = study
redirect_to study_edit_path #easy peesy
end
end
Happy coding,
Yacine.

Is it possible to retrieve query string params in ActiveAdmin?

I have an ActiveAdmin project that the user needs to select a category from a main page. They are then sent to a new product page with the category_id in the url (...blah?category_id=1) I want to be able to retrieve that category_id from the url. I keep getting an ActiveAdmin DSL error saying I can't params method
...blah?category_id=1
ActiveAdmin.register Product do
controller do
puts params[:category_id]
def scoped_collection
ItemsDesign.includes(:categories, :colors)
end
end
end
This what the AA controller should look like in order to get access to params.
controller do
def new
#blah = Blah.new
puts params[:category_id]
end
end
Figured it out. I the params needs to be inside an overriding method. In my case new as above.

Redirect after the sign up

Im trying to redirect the user after the sign up by saving the referer in case when user came to sign up through clicking on any specific page. But its not working properly.
In my app controller
def save_referer
unless user_signed_in?
unless session['referer']
session['referer'] = request.referer || 'none'
end
end
end
In user model
def save_with(referer)
referer = referer unless referer == "null"
self.save
end
Here im saving it
if current_user.sign_in_count <= 1
if current_user.save_with(session[:referer])
redirect_to session[:referer]
else
redirect_to any_other
end
In User model, I guess you are not explicitly referring the attribute correctly.
def save_with(referer)
self.referer = referer unless referer == "null"
self.save
end
Generally, I take this strategy:
Anywhere I have a signup button I link to signup like so:
Signup!
Then in my signup's Action I do this:
def signup
session[:signup_refer] = params[:signup_refer] if params[:signup_refer] # put the refer in a session
redirect_to(signup_path) if session[:signup_refer] && params[:signup_refer] # this clears params if you want to keep your url nice an clean for the user
# do signup stuffs
signup_refer = session[:signup_refer] # get the refer info in a variable
session[:signup_refer] = nil # clear the session before redirecting
redirect_to(signup_refer ? signup_refer : "/default_post_signup")
end
I know it could be messy, but it totally works for me (well works in Merb, where it's "redirect() not redirect_to()) but you get the idea.

Resources