Class: AARRR::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/aarrr/session.rb

Overview

an AARR session is used to identify a particular user in order to track events

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env_or_object = nil, attributes = nil) ⇒ Session

find or creates a session in the db based on the env or object



10
11
12
13
14
15
# File 'lib/aarrr/session.rb', line 10

def initialize(env_or_object = nil, attributes = nil)
  self.id = parse_id(env_or_object) || BSON::ObjectId.new.to_s

  # perform upsert
  update({"$set" => build_attributes(env_or_object).merge(attributes || {})}, :upsert => true)
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/aarrr/session.rb', line 7

def id
  @id
end

Instance Method Details

#acquisition!(event_name, options = {}) ⇒ Object

more helpers



62
63
64
65
# File 'lib/aarrr/session.rb', line 62

def acquisition!(event_name, options = {})
  options[:event_type] = :acquisition
  track!(event_name, options)
end

#activation!(event_name, options = {}) ⇒ Object



67
68
69
70
# File 'lib/aarrr/session.rb', line 67

def activation!(event_name, options = {})
  options[:event_type] = :activation
  track!(event_name, options)
end

#retention!(event_name, options = {}) ⇒ Object



72
73
74
75
# File 'lib/aarrr/session.rb', line 72

def retention!(event_name, options = {})
  options[:event_type] = :retention
  track!(event_name, options)
end

#save(response) ⇒ Object

save a cookie to the response



28
29
30
31
32
33
34
# File 'lib/aarrr/session.rb', line 28

def save(response)
  response.set_cookie(AARRR::Config.cookie_name, {
    :value => self.id,
    :path => "/",
    :expires => Time.now+AARRR::Config.cookie_expiration
  })
end

#set_data(data) ⇒ Object

sets some additional data



23
24
25
# File 'lib/aarrr/session.rb', line 23

def set_data(data)
  update({"data" => {"$set" => data}})
end

#track!(event_name, options = {}) ⇒ Object

track event name



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aarrr/session.rb', line 37

def track!(event_name, options = {})

  # add event tracking
  result = AARRR.events.insert({
    "aarrr_user_id" => self.id,
    "event_name" => event_name,
    "event_type" => options[:event_type],
    "in_progress" => options[:in_progress] || false,
    "data" => options[:data],
    "revenue" => options[:revenue],
    "referral_code" => options[:referral_code]
  })

  # update user with last updated time
  update({
    "$set" => {
      "last_event_at" => Time.now.getutc
    }
  })

  result
end

#userObject

returns a reference the othe AARRR user



18
19
20
# File 'lib/aarrr/session.rb', line 18

def user
  AARRR.users.find(id)
end