Setting a Hidden Field based On Landing Page

This little devil took a lot of Googling, but is very useful when it comes to setting up analytics...

Tags: Parameters, Analytics, Rails

Back to Blogs

Say you want to include a field in a form that says which link your user clicked to get to that form. Did they come from your homepage? From a blog entry?

Who knows? You do. At least now you do.

Here's an example. On a real estate site I was making, I wanted people to click a "buy a house" button or a "buy an apartment" button. Either button would send them to the same survey, but I wanted the survey to record how they got there.

For the two buttons, I added a boolean value, that got passed to the survey in the url.

The links were:

<%= link_to new_quick_path(house: true) do %>House<% end %>
<%= link_to new_quick_path(house: false) do %>Apartment<% end %>.

This translated into a url like this bad boy:


Then in the form, I included a hidden field which grabbed this parameter:
<%= f.hidden_field :house, value: params[:house] %>

Bippity boppity boop.

Back to Blogs