Class: Charted::Visitor

Inherits:
Sequel::Model
  • Object
show all
Defined in:
lib/charted/model.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Visitor

Returns a new instance of Visitor.



51
52
53
54
55
56
# File 'lib/charted/model.rb', line 51

def initialize(*args)
  super
  self.created_at ||= DateTime.now
  self.bucket ||= rand(10)
  self.secret = SecureRandom.hex(3)
end

Instance Method Details



58
59
60
61
# File 'lib/charted/model.rb', line 58

def cookie
  # TODO: raise if nil id, bucket, or secret
  "#{self.id}-#{self.bucket}-#{self.secret}"
end

#end_goals(labels) ⇒ Object



105
106
107
108
109
110
111
112
# File 'lib/charted/model.rb', line 105

def end_goals(labels)
  labels.to_s.split(';').map(&:strip).each do |label|
    exp = experiments_dataset.first(label: label)
    exp.end! if exp
    conv = conversions_dataset.first(label: label)
    conv.end! if conv
  end
end

#ip_address=(ip) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/charted/model.rb', line 70

def ip_address=(ip)
  return if ip.to_s =~ /^\s*$/ || ip == '127.0.0.1'
  name = GEOIP.country(ip).country_name

  return if name =~ /^\s*$/ || name == 'N/A'
  self.country = name
rescue SocketError
  # invalid IP address, skip setting country
end

#make_events(labels) ⇒ Object



80
81
82
83
84
# File 'lib/charted/model.rb', line 80

def make_events(labels)
  labels.to_s.split(';').map(&:strip).map do |label|
    add_event(label: label)
  end
end

#start_conversions(labels) ⇒ Object



86
87
88
89
90
# File 'lib/charted/model.rb', line 86

def start_conversions(labels)
  labels.to_s.split(';').map(&:strip).map do |label|
    conversions_dataset.first(label: label) || self.add_conversion(label: label)
  end
end

#start_experiments(labels) ⇒ Object

label:bucket;…



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/charted/model.rb', line 92

def start_experiments(labels) # label:bucket;...
  labels.to_s.split(';').map do |str|
    label, bucket = str.split(':', 2).map(&:strip)
    exp = experiments_dataset.first(label: label)
    if exp
      exp.update(bucket: bucket) if exp.bucket != bucket
      exp
    else
      self.add_experiment(label: label, bucket: bucket)
    end
  end
end

#user_agent=(user_agent) ⇒ Object



63
64
65
66
67
68
# File 'lib/charted/model.rb', line 63

def user_agent=(user_agent)
  ua = UserAgent.parse(user_agent)
  self.browser = ua.browser
  self.browser_version = ua.version
  self.platform = ua.platform == 'X11' ? 'Linux' : ua.platform
end