Clearance and Rack Middleware

Has any one made or come across Rack middleware that handles identifying Clearance users for Segment analytics?

I like that Clearance exposes env[:clearance] and I’m imaging middleware that does something like:

  • Generates a unique anonymous_id for logged out users and stores it in the visitor’s session
  • Uses Segment’s identify method to track logged out users by anonymous_id
  • Uses Segment’s alias method to track visitors with a real user_id once they’ve logged in
  • Continues to identify logged in users by their user_id from then on

Then our Rails layer only has to worry about Segment track related logic which would be nice.