No commit activity in last 3 years
No release in over 3 years
Store selected request parameters to cookies for use in future requests. Useful for affiliate, referral or promotion links.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 4.1.0

Runtime

< 3
 Project Readme

Rack::ParamToCookie¶ ↑

<img src=“https://secure.travis-ci.org/jdleesmiller/rack-param_to_cookie.png”/>

SYNOPSIS¶ ↑

Rack::ParamToCookie is a rack middleware that extracts request parameters from requests and stores them in cookies for use in future requests. It’s useful for affiliate, referral or promotion links, which often work something like:

  1. A referral partner puts a link on their site to somewhere on your site, and they include a request parameter like ref so you can identify traffic from them, e.g. www.example.com/some-interesting-page?ref=123ABC

  2. A user clicks through the link and comes to your site.

  3. The Rack::ParamToCookie middleware finds the ref parameter and stores it in a cookie for this user.

  4. The user browses around for a while and eventually signs up.

  5. Your sign up action recovers the referral code, 123ABC, from the cookie, and you mark it as a successful referral from your partner.

  6. $$$$!

Usage¶ ↑

In a Rails app, you can add it as middleware in config/application.rb. For a parameter called ref, the basic usage is:

config.middleware.use 'Rack::ParamToCookie', 'ref' => {}

This tells Rack::ParamToCookie to capture a request parameter called ref, store it in a cookie called ref, and make it available in your rails app as request.env['ref']. You can specify multiple parameters and configure them. Here’s a more in-depth example:

config.middleware.use 'Rack::ParamToCookie',
  'ref' => {cookie_name: 'referral_code',
            env_name: 'referral.code',
            ttl: 14*24*60*60,
            max_length: 12},
  'aff' => {cookie_name: 'affiliate_code',
            env_name: 'affiliate.code'}

The first cookie, referral_code for parameter ref, has a 14 day time to live, can be a maximum of 12 characters and is accessible in request.env ['referral.code']. The second, for parameter aff, has the default time to live, which is 30 days and can be the default maximum length, which is 64 characters.

Installation¶ ↑

Add to your Gemfile

gem 'rack-param_to_cookie'

Or install directly

gem install rack-param_to_cookie

You can load it with

require 'rack/param_to_cookie'

Credits¶ ↑

This middleware is mostly based on Rack::Affiliates (github.com/alexlevin/rack-affiliates); this middleware is just a bit more generic and configurable.

License¶ ↑

(The MIT License)

Copyright © 2014 John Lees-Miller

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.