Provisional and Repo Man: Automated Bootstrapping
If we’ve said it once, we’ve said it many times: Don’t Repeat Yourself.
Continue reading "Provisional and Repo Man: Automated Bootstrapping"
If we’ve said it once, we’ve said it many times: Don’t Repeat Yourself.
Continue reading "Provisional and Repo Man: Automated Bootstrapping"
A few months ago we released simplest_auth as a stripped-down alternative to authentication plugins such as restful auth. After using it in a few projects we came to the realization that there was no particular reason for it to be a plugin and even less of a reason for it to work only with ActiveRecord.
Continue reading "SimplestAuth: Gem-ified, with DataMapper Support"
Our marketing team wanted better insight into exactly how PPC traffic was behaving for a catering client. Information is set when a user clicks a Google AdWords advertisement, or similar ads on MSN and Yahoo!. Normally, when a user arrives at a Google Analytics tracked site, this information is placed in a cookie. This cookie expires at the completion of a user's session on the site (or for other reasons). We wished to retain the knowledge that a user had arrived at the site via advertising, at some point, in order to see if that user eventually signed up, or made a purchase, or some other goal.
To accomplish this, we chose to use Google's "user defined variables". These are set using the 'setVar' method, in javascript.
First, we had to determine if an inbound user was, in fact, coming from an advertisement. For MSN and Yahoo! this was a simple matter of checking for 'cpc' as a part of the utmz cookie that GA defines. For AdWords we had to detect the existence of a 'gclid', the ID used by Google Analytics. Here's the javascript we include after a new pageTracker is made:
document.cookie.split(';').each(function(cookie) {
if(cookie.indexOf('__utmz') != '-1') utmz = cookie;
});
if(utmz) {
var cpc_regex = new RegExp(".*?utmcmd=([a-z]+)\|");
medium = utmz.match(cpc_regex)[1];
var gclid_regex = new RegExp(".*?utmgclid=([\_0-9A-Za-z]+)\|");
adwords = utmz.match(gclid_regex)[1];
if(medium == 'cpc' || adwords) pageTracker._setVar('CPC');
}
This, very specifically, looks for the medium and gclid and always requires the existence of the cookies set by GA. It is the simplest thing that works for us. It could easily be adapted to check for other information (like referrer) and set a variable or do any number of other things with javascript.
If you have seen this done before, or have an alternative method, please leave a comment as we would love to hear about it.
To find out why we needed this information and how it is useful please read the companion post from Josh, on our Engage Blog.
As we all learned in Rails 101, named routes come in two varieties: path, and URL. When should we use each variety?
There are a number of plugins and gems that will provide you with various ways to define configuration for your application, search google for "rails configuration plugin". Alternatively, you can use OpenStruct (in Ruby standard library) to create a rich constant with various attributes. Simply create a file called something like app_config.rb in config/initializers and do something like this:
require "ostruct"
App = OpenStruct.new({
:name => "My App",
:subtitle => "Hotter than Ebay",
:admin => OpenStruct.new({
:username => "admin",
:password => "secret"
})
})
So App is created as a constant that you can use anywhere in your application. Each key given to OpenStruct becomes a method on the constant. For example:
class ApplicationController < ActionController::Base
def authenticated?(username, password)
username == App.admin.username &&
password == App.admin.password
end
end
It's not the most flexible solution, but if you are in need of some quick and easy configuration, it's not a bad option.
at Viget Labs. We write about web development trends, tips, best practices, industry events, and our projects — all with an emphasis on Ruby on Rails.
Have any questions, comments, ideas, or secrets to share? Let us know.
Recent Comments
For translating strings you can use Rails I18n backend instead of using inflectors.
The `typus_human_name` is a patch to fix a problem in `human_name` [1].
[1] https://rails.lighthouseapp.com/projects/8994/tickets/2120-humanize-and-human_name-dont-separate-words