Browse files

fix session creation so it parses HTTP_COOKIE correctly

  • Loading branch information...
1 parent 7d90988 commit bf3628aa319c8cf0a82e3abd3a78d747738debe9 @jacquescrocker committed Jun 2, 2011
Showing with 33 additions and 15 deletions.
  1. +31 −9 lib/aarrr/session.rb
  2. +2 −6 spec/functional/aarrr/session_spec.rb
View
40 lib/aarrr/session.rb
@@ -24,6 +24,15 @@ def set_data(data)
update({"data" => {"$set" => data}})
end
+ # save a cookie to the response
+ def save(response)
+ response.set_cookie(AARRR::Config.cookie_name, {
+ :value => self.id,
+ :path => "/",
+ :expires => Time.now+AARRR::Config.cookie_expiration
+ })
+ end
+
# track event name
def track!(event_name, options = {})
@@ -46,15 +55,26 @@ def track!(event_name, options = {})
})
end
- # save a cookie to the response
- def save(response)
- response.set_cookie(AARRR::Config.cookie_name, {
- :value => self.id,
- :path => "/",
- :expires => Time.now+AARRR::Config.cookie_expiration
- })
+ # more helpers
+
+ def acquisition!(event_name, options = {})
+ options[:event_type] = :acquisition
+ track!(event_name, options)
+ end
+
+ def activation!(event_name, options = {})
+ options[:event_type] = :activation
+ track!(event_name, options)
+ end
+
+ def retention!(event_name, options = {})
+ options[:event_type] = :retention
+ track!(event_name, options)
end
+ # TODO: referral and revenue
+
+
protected
# mark update
@@ -66,9 +86,11 @@ def update(attributes, options = {})
def parse_id(env_or_object)
# check for empty case or string
- # if it's a hash, then process like a request.env
+ # if it's a hash, then process like a request and pull out the cookie
if env_or_object.is_a?(Hash)
- (env_or_object["rack.request.cookie_hash"] || {})[AARRR::Config.cookie_name]
+
+ request = Rack::Request.new(env_or_object)
+ request.cookies[AARRR::Config.cookie_name]
# if it's an object with an id, then return that
elsif env_or_object.respond_to?(:id) and env_or_object.id.is_a?(BSON::ObjectId)
View
8 spec/functional/aarrr/session_spec.rb
@@ -14,16 +14,12 @@ module AARRR
it "should create a session with a request env" do
session = Session.new
Session.new({
- "rack.request.cookie_hash" => {
- "_utmarr" => session.id
- }
+ "HTTP_COOKIE" => "_utmarr=#{session.id}; path=/;"
})
AARRR.users.count.should eq(1)
session = Session.new({
- "rack.request.cookie_hash" => {
- "_utmarr" => "x9x9x19x"
- }
+ "HTTP_COOKIE" => "_utmarr=x83y1; path=/;"
})
AARRR.users.count.should eq(2)

0 comments on commit bf3628a

Please sign in to comment.