Quantcast
Viewing all articles
Browse latest Browse all 34

Rails Metal Example #7: Tracking Analytics

Last month, I posted 9 Ways to Use Rails Metal. This is the seventh way to use Rails Metal.

A week ago, I posted Ruby on Rails: Polymorphic Associations with Mixin Modules which included an example of tracking impressions on different objects.


For this Rails Metal example, we’ll use that mixin to track page impressions.

class Page < ActiveRecord::Base
  include ImpressionableMixin
end

Now that we have the impressions association set up, let’s get on with the Metal.

# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)

class Pages
  def self.call(env)
    page = Page.find_or_create_by_path(env["PATH_INFO"])
    session = env['rack.session']
    visitor = Visitor.find_by_id(session["visitor_id"])
    page.add_impression(visitor)
    [404, {"Content-Type" => "text/html"}, ["Not Found"]]
  end
end

As you can see in this case, we aren’t actually returning a response here. We’re saving the impression and the passing the request up the chain. The example is pretty simple, but it demonstrates that Rails Metal doesn’t need to provide an end point your request.


Viewing all articles
Browse latest Browse all 34

Trending Articles