0.0
No commit activity in last 3 years
No release in over 3 years
If the user visits via a Google Analytics Campaign link, this middleware will track utm_source, utm_content, utm_term, utm_medium, utm_campaign, and time.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.14.6, ~> 1.14
>= 5.10.1, ~> 5.10
>= 0.6.2, ~> 0.6
>= 0.14, ~> 0.14
>= 0.7.1, ~> 0.7

Runtime

< 3, > 1
 Project Readme

Rack::GaTrack

Gem Version Code Climate

Rack::GaTrack is a rack middleware that extracts Google Analytics Campaign params from the request. Specifically, it looks for a param utm_source in the request. If found, it creates a cookie with utm_source, utm_content, utm_term, utm_campaign,landing_page (path), referer, and time. Anytime utm_source is found in the request the cookie will be updated with the new Google Analytics Campaign params.

Use Case

Let's say you want to track signups in your app from a specific campaign.

  1. A user clicks a link on an external site that has your Google Analytics Campaign params in the request.
  2. Rack::GaTrack finds utm_source param in the request and parses the other Google Analytics Campaign params from the request and saves them in a cookie.
  3. If a user signs up now or in the future you can read the Google Analytics Campaign params from the env variables that Rack::GaTrack creates and save them to the created user record.

Installation

gem install rack-ga-track

Rails 4 Example Usage

Add the Rack::GaTrack to your application stack:

#Rails 4 in config/application.rb
class Application < Rails::Application
  config.middleware.use Rack::GaTrack
end

You can now access your Google Analytics Campaign params in env['ga_track.source'] env['ga_track.term'] env['ga_track.content'] env['ga_track.campaign'] env['ga_track.medium'] env['ga_track.referer'] env['ga_track.landing_page'] env['ga_track.time']

Customization

By default cookie is set for 30 days, you can extend time to live with :ttl option (default is 30 days).

#Rails 4 in config/application.rb
class Application < Rails::Application
  config.middleware.use Rack::GaTrack, :ttl => 3.months
end

The :domain option allows to customize cookie domain.

#Rails 4 in config/application.rb
class Application < Rails::Application
  config.middleware.use Rack::GaTrack, :domain => '.example.org'
end

Rack::GaTrack will set cookie on .example.org so it's accessible on www.example.org, app.example.org etc.