Class: Trak
- Inherits:
-
Object
- Object
- Trak
- Defined in:
- lib/trak.rb
Constant Summary collapse
- VERSION =
'0.0.6'- HEADERS =
{ 'Content-Type' => 'application/json' }
Instance Attribute Summary collapse
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#distinct_id ⇒ Object
Returns the value of attribute distinct_id.
Instance Method Summary collapse
-
#alias(distinct_id, aliases) ⇒ Object
Alias is how you set additional distinct ids for a person.
-
#annotate(event, opts = {}) ⇒ Object
Annotate is a way of recording system wide events that affect everyone.
- #execute_request(url, data) ⇒ Object
-
#identify(distinct_id, properties = {}) ⇒ Object
Identify is how you send trak.io properties about a person like Email, Name, Account Type, Age, etc.
-
#initialize(api_key = nil) ⇒ Trak
constructor
A new instance of Trak.
-
#page_view(url, page_title = nil, opts = {}) ⇒ Object
Page view is just a wrapper for: Trak.track(‘Page view’).
-
#track(event, opts = {}) ⇒ Object
Track is how you record the actions people perform.
Constructor Details
#initialize(api_key = nil) ⇒ Trak
Returns a new instance of Trak.
11 12 13 14 15 16 |
# File 'lib/trak.rb', line 11 def initialize(api_key = nil) raise "api_key required" unless api_key || defined? API_KEY @api_key = api_key ? api_key : API_KEY api_base = 'api.trak.io' @http = Net::HTTP.new api_base end |
Instance Attribute Details
#channel ⇒ Object
Returns the value of attribute channel.
9 10 11 |
# File 'lib/trak.rb', line 9 def channel @channel end |
#distinct_id ⇒ Object
Returns the value of attribute distinct_id.
9 10 11 |
# File 'lib/trak.rb', line 9 def distinct_id @distinct_id end |
Instance Method Details
#alias(distinct_id, aliases) ⇒ Object
Alias is how you set additional distinct ids for a person. This is useful if you initially use trak.io’s automatically generated distinct id to identify or track a person but then they login or register and you want to identify them by their email address or your application’s id for them. Doing this will allow you to identify users across sessions and devices.
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/trak.rb', line 61 def alias(distinct_id, aliases) raise "distinct_id required" unless distinct_id raise "aliases cannot be empty" unless aliases raise "aliases must be a String or an Array" unless aliases.kind_of?(Array) || aliases.kind_of?(String) data = { :token => @api_key, :data => { :distinct_id => distinct_id, :alias => aliases, }, }.to_json # Set current session variable for distinct_id self.distinct_id = distinct_id execute_request('/v1/alias', data) end |
#annotate(event, opts = {}) ⇒ Object
Annotate is a way of recording system wide events that affect everyone. Annotations are similar to events except they are not associated with any one person.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/trak.rb', line 158 def annotate(event, opts = {}) defaults = { :channel => channel, :properties => {}, } opts = defaults.merge opts raise "event is required" unless event raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash) data = { :token => @api_key, :data => { :event => event, :channel => opts[:channel], :properties => opts[:properties], }, }.to_json execute_request('/v1/annotate', data) end |
#execute_request(url, data) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/trak.rb', line 18 def execute_request(url, data) unless defined?(SILENCE) && SILENCE response = @http.post(url, data, HEADERS) JSON.parse(response.body) end rescue # Do something to handle errors end |
#identify(distinct_id, properties = {}) ⇒ Object
Identify is how you send trak.io properties about a person like Email, Name, Account Type, Age, etc.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/trak.rb', line 33 def identify(distinct_id, properties = {}) raise "distinct_id required" unless distinct_id raise "properties must be a Hash" unless properties.kind_of?(Hash) data = { :token => @api_key, :data => { :distinct_id => distinct_id, :properties => properties, }, }.to_json # Set current session variable for distinct_id self.distinct_id = distinct_id execute_request('/v1/identify', data) end |
#page_view(url, page_title = nil, opts = {}) ⇒ Object
Page view is just a wrapper for: Trak.track(‘Page view’)
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/trak.rb', line 120 def page_view(url, page_title = nil, opts = {}) defaults = { :event => 'Page view', :distinct_id => self.distinct_id, :channel => self.channel, :properties => { :url => url, :page_title => page_title, }, } opts = defaults.merge opts raise "url is required" unless url raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash) raise "No distinct_id is set." if opts[:distinct_id].nil? data = { :token => @api_key, :data => { :distinct_id => opts[:distinct_id], :event => opts[:event], :channel => opts[:channel], :properties => opts[:properties], }, }.to_json # Set current session variable for distinct_id self.distinct_id = opts[:distinct_id] execute_request('/v1/track', data) end |
#track(event, opts = {}) ⇒ Object
Track is how you record the actions people perform. You can also record properties specific to those actions. For a “Purchased shirt” event, you might record properties like revenue, size, etc.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/trak.rb', line 87 def track(event, opts = {}) defaults = { :distinct_id => self.distinct_id, :channel => self.channel, :properties => {}, } opts = defaults.merge opts raise "event is required" unless event raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash) raise "No distinct_id is set." if opts[:distinct_id].nil? data = { :token => @api_key, :data => { :distinct_id => opts[:distinct_id], :event => event, :channel => opts[:channel], :properties => opts[:properties], }, }.to_json # Set current session variable for distinct_id self.distinct_id = opts[:distinct_id] execute_request('/v1/track', data) end |