Redir

There are many gems that allow you to create a short url and redirect a user to another website. But there are not many store information about the requests that use Mongo which is why we created redir.

Features

  • Fully rack compliant
  • Generates a unique shortened url for you
  • Takes care of the mongo heavy lifting for you
  • Stores all of the request header information to gather analytics
  • Allows the addition to two sets of request parameters for conversion analytics

How to use it

Redir is currently under active development.

Rails

include the gem in your Gemfile in your application.rb add

config.middleware.use Rack::Redir

in your environments/ development.rb, test.rb or production.rb file add.

Redirect.host = '<your hostname>/'

add a config/mongoid.yml file. Setup a local mongodb instance or use one from a cloud provider and add those configuration settings to your mongoid.yml file as described here http://mongoid.org/en/mongoid/docs/installation.html#configuration

Redir by default uses the /a route for redirect requests. IE: http:///a/ If you have another route that is already set at a you can change this by adding to your application.rb.

Redirect.path = '<new path>/' 

To create a shortened Url in your code

Redir.create_shortened_url <your url>

The Redirect object is a Mongoid object class. Therefore you can search for redirect information based on it's attributes. In the case of Redirect those attributes are original_url and shortened_id. For example

results = Redirect.where(:original_url => www.polyglotprogramminginc.com).all  

Mongoid results a Mongoid::Criteria object. To get at the results you can use something like

results.to_a 

to get an array of results. See the mongoid documentation for further details. The shortened id is only the 8 character unique id of the shortened url. For example for http://www.springbot/a/ddffggrr ddffggrr would be the shortened_id. There is also a shortened_url and shortened_path method. See the redirect.rb docucumentation for more information. To get the full url that the user will be able to hit to be redirected use the shortened_url helper....ie

redir = Redirect.create_shortened_url <your url>
redir.shortened_url #the full shortened url that the user would hit to be directed back to <your url>

RedirectLog contains information about redirects that have been done. We are currently working on helper methods to make this easier, but for the time being you can get at that data by doing the following.

read the _id field from a redirect object that you want to get logging information for. IE:

  my_redirect = Redirect.where(:shortened_id => 'rrffttee').to_a.last

and running a query against the RedirectLog object

  redir_log = RedirectLog.where(:redirect_id => my_redirect._id)

an easier way to do this is

  Redirector.find_logs <a shortened id>

you use the redir_log like any mongo object. the headers attribute contains the raw request headers and request time has the date/time the request came in.

Other configuration options

  Redirect.append_tracking_info

adds redirect_mongo_id=&redirect_log_mongo_id= to the end of the url being redirected to for conversion tracking

  Redirect.append_google_analytics

adds utm_source=<@@host>&utm_campaign=__ to the end of the url being redirected to for google analytics tracking

Contributing

Just fork us on GitHub and send a pull request when you are ready.

License

redir is released under the MIT license: