UrlJob¶ ↑
Need to track if an email was opened? If a link was clicked? If a page was viewed? Too lazy to setup cron (or is it impossible?)?
Easy – Just create a url_job that when visited does things (like the above) for you.
This is a rails gem that provides an engine which allows for creating urls that execute jobs.
Install¶ ↑
gem install url_job
Usage¶ ↑
rails g url_job rake db:migrate
Now lets assume you have a class like:
class Redirector < Struct.new(:redirect_to) def initialize(redirect) self.redirect_to = redirect end def perform(args={}) self end end
By creating a UrlJob using the following:
uj = UrlJob::Job.from_object(Redirector.new("http://frison.ca"))
And visiting:
http://localhost:3000/uj/#{uj.token} or http://localhost:3000/uj/#{uj.path} or #{uj.url} (assuming default_url_options[:host] is setup for ActionMailer)
The result will be deserializing the Redirector job, and calling it’s perform method. The perform method must return an object that responds to either: ‘redirect_to’ or ‘render’, and the corresponding action will occur. The perform method will be passed in a dictionary with the following keys:
:ip_address => The ip address of the url visitor :user_agent => The user agent of the url visitor :referrer => The referrer of the url visitor :token => The token used to visit the url.
A more interesting example might be an object like:
class Tracker def initialize end def perform(params={}) @url_job = UrlJob::Job.find_by_token(params[:token]) UrlTrack.create(:url_job => @url_job, :referrer => params[:referrer], :ip_address => params[:ip_address], :user_agent => params[:user_agent]) OpenStruct.new({:render => " "}) end end
This way you could do something like:
uj = UrlJob::Job.from_object(Tracker.new)
And embed a image tag like:
<img src="http://localhost:3000/uj/#{uj.token}" height="1px" width="1px">
Inside some emails to track some email opens
Thanks¶ ↑
UrlJob::Job almost exactly a stripped down version of Delayed::Job with a controller
Licence¶ ↑
Copyright © 2011 Timothy Frison
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.