trak-ruby 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Brian Sewell
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,185 @@
1
+ # trak.io Ruby API Wrapper
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/trak-ruby.png)](http://badge.fury.io/rb/trak-ruby)
4
+
5
+ This is the gem that allows Ruby bindings for the trak.io V1 API.
6
+
7
+ ## Rails Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'trak-ruby'
12
+
13
+ Create an initilizer `config/initializers/trak.rb` with the following code:
14
+
15
+ ```ruby
16
+ Trak::API_KEY = "YOUR API KEY HERE"
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install 'trak-ruby'
26
+
27
+ ===
28
+
29
+ ## API Methods
30
+
31
+ ### Identify
32
+
33
+ ```ruby
34
+ Trak.identify(distinct_id, properties)
35
+ ```
36
+
37
+ Identify is how you send trak.io properties about a person like Email, Name, Account Type, Age, etc.
38
+
39
+ Learn more about implementation of `identify` on trak.io's [API documentation](http://docs.trak.io/identify.html)
40
+
41
+ ```ruby
42
+ # Initilaize Trak object
43
+ t = Trak.new
44
+
45
+ t.identify(1, {:name => 'John Smith', :email => 'john@test.com'})
46
+ ```
47
+
48
+ The `identify` method will set the class variable `@distinct_id` to whatever you pass as your first parameter.
49
+
50
+ ### Alias
51
+
52
+ ```ruby
53
+ Trak.alias(distinct_id, aliases)
54
+ ```
55
+
56
+ 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. But of course you want to tie their activity before and after they logged in together, Trak.alias() will do this for you.
57
+
58
+ Learn more about implementation of `alias` on trak.io's [API documentation](http://docs.trak.io/alias.html)
59
+
60
+ ```ruby
61
+ # Initilaize Trak object
62
+ t = Trak.new
63
+
64
+ # Pass a string to add a new alias for a person with distinct_id = 1
65
+ t.alias(1, 'Johnny')
66
+
67
+ # Pass an array of aliases for a person with distinct_id = 1
68
+ t.alias(1, ['Johnny', 'john'])
69
+ ```
70
+
71
+ The `alias` method will set the class variable `@distinct_id` to whatever you pass as your first parameter.
72
+
73
+ ### Track
74
+
75
+ ```ruby
76
+ Trak.track(event, opts = {})
77
+ ```
78
+
79
+ 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.
80
+
81
+ Learn more about implementation of `track` on trak.io's [API documentation](http://docs.trak.io/track.html)
82
+
83
+ ```ruby
84
+ # Initilaize Trak object
85
+ t = Trak.new
86
+
87
+ # Log the event 'played video'
88
+ t.track('played video')
89
+
90
+ # Log the event 'played video' for a person with distinct_id = 1
91
+ t.track('played video', {:distinct_id => 1})
92
+
93
+ # Log the event 'played video' on the 'Blog' channel
94
+ t.track('played video', {:channel => 'Blog'})
95
+
96
+ # Log an event with custom properties attached:
97
+ # Log the event 'search' on the 'Web site' channel along with
98
+ # any related properties you wish to track
99
+ t.track('search', {
100
+ :channel => 'Web site',
101
+ :properties => {
102
+ :search_term => 'iPad Air',
103
+ :search_category => 'Electronics',
104
+ },
105
+ })
106
+ ```
107
+
108
+ The `track` method will set the class variable `@distinct_id` if you pass it a distinct_id in the `opts` hash.
109
+
110
+ ### Page View
111
+
112
+ ```ruby
113
+ Trak.page_view(url, page_title, opts = {})
114
+ ```
115
+
116
+ Page view is just a wrapper for: `Trak.track('Page View')`
117
+
118
+ Learn more about implementation of `page_view` on trak.io's [API documentation](http://docs.trak.io/page_view.html)
119
+
120
+ ```ruby
121
+ # Initilaize Trak object
122
+ t = Trak.new
123
+
124
+ # Log a user visiting the settings page
125
+ t.page_view('http://mysite.com/settings', 'Settings')
126
+ ```
127
+
128
+ The `page_view` method will set the class variable `@distinct_id` if you pass it a distinct_id in the `opts` hash.
129
+
130
+ ### Annotate
131
+
132
+ ```ruby
133
+ Trak.annotate(event, opts = {})
134
+ ```
135
+
136
+ 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.
137
+
138
+ Learn more about implementation of `annotate` on trak.io's [API documentation](http://docs.trak.io/annotate.html)
139
+
140
+ ```ruby
141
+ # Initilaize Trak object
142
+ t = Trak.new
143
+
144
+ # Annotate a deploy
145
+ t.annotate('Deployed update')
146
+
147
+ # Annotate a deploy for a specific channel
148
+ t.annotate('Deployed update', {:channel => 'Web site'})
149
+ ```
150
+
151
+ ### Distinct ID
152
+
153
+ ```ruby
154
+ # Initilaize Trak object
155
+ t = Trak.new
156
+
157
+ # Returns the current distinct id
158
+ t.distinct_id
159
+
160
+ # Sets the current distinct_id
161
+ t.distinct_id = 1
162
+ ```
163
+
164
+ ### Channel
165
+
166
+ ```ruby
167
+ # Initilaize Trak object
168
+ t = Trak.new
169
+
170
+ # Returns the current channel
171
+ t.channel
172
+
173
+ # Sets the current channel
174
+ t.channel = 'Web site'
175
+ ```
176
+
177
+ ===
178
+
179
+ ## Contributing
180
+
181
+ 1. Fork it
182
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
183
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
184
+ 4. Push to the branch (`git push origin my-new-feature`)
185
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.3
Binary file
@@ -0,0 +1,174 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ class Trak
5
+
6
+ VERSION = '0.0.3'
7
+ HEADERS = { 'Content-Type' => 'application/json' }
8
+
9
+ attr_accessor :distinct_id, :channel
10
+
11
+ def initialize(api_key = nil)
12
+ raise "api_key required" unless api_key || defined? API_KEY
13
+ @api_key = api_key ? api_key : API_KEY
14
+ api_base = 'api.trak.io'
15
+ @http = Net::HTTP.new api_base
16
+ end
17
+
18
+ def execute_request(url, data)
19
+ response = @http.post(url, data, HEADERS)
20
+ JSON.parse(response.body)
21
+ end
22
+
23
+ # Identify is how you send trak.io properties about a person like Email, Name, Account Type, Age, etc.
24
+ #
25
+ # @param distinct_id [String] A unique identifier for a user (visitor)
26
+ # @param properties [Hash] A hash of properties about a person (example: `{:name => 'John Smith', :age => 40}`)
27
+ # @return [Object]
28
+ #
29
+ def identify(distinct_id, properties = {})
30
+ raise "distinct_id required" unless distinct_id
31
+ raise "properties must be a Hash" unless aliases.kind_of?(Hash)
32
+ data = {
33
+ :token => @api_key,
34
+ :data => {
35
+ :distinct_id => distinct_id,
36
+ :properties => properties,
37
+ },
38
+ }.to_json
39
+
40
+ # Set current session variable for distinct_id
41
+ self.distinct_id = distinct_id
42
+
43
+ execute_request('/v1/identify', data)
44
+ end
45
+
46
+ # Alias is how you set additional distinct ids for a person.
47
+ # This is useful if you initially use trak.io's automatically generated
48
+ # distinct id to identify or track a person but then they login or
49
+ # register and you want to identify them by their email address or your
50
+ # application's id for them. Doing this will allow you to identify users
51
+ # across sessions and devices.
52
+ #
53
+ # @param distinct_id [String] The unique identifier of a user
54
+ # @param aliases [String, Array] An array of distinct ids, you would like to add.
55
+ # @return [Object]
56
+ #
57
+ def alias(distinct_id, aliases)
58
+ raise "distinct_id required" unless distinct_id
59
+ raise "aliases cannot be empty" unless aliases
60
+ raise "aliases must be a String or an Array" unless aliases.kind_of?(Array) || aliases.kind_of?(String)
61
+ data = {
62
+ :token => @api_key,
63
+ :data => {
64
+ :distinct_id => distinct_id,
65
+ :alias => aliases,
66
+ },
67
+ }.to_json
68
+
69
+ # Set current session variable for distinct_id
70
+ self.distinct_id = distinct_id
71
+
72
+ execute_request('/v1/alias', data)
73
+ end
74
+
75
+ # Track is how you record the actions people perform. You can also record
76
+ # properties specific to those actions. For a "Purchased shirt" event,
77
+ # you might record properties like revenue, size, etc.
78
+ #
79
+ # @param event [String] The key for this event, this value will be standardized server side.
80
+ # @param opts [Hash] options to pass to track call (distinct_id [String], channel [String], properties [Hash])
81
+ # @return [Object]
82
+ #
83
+ def track(event, opts = {})
84
+ defaults = {
85
+ :distinct_id => self.distinct_id,
86
+ :channel => self.channel,
87
+ :properties => {},
88
+ }
89
+ opts = defaults.merge opts
90
+ raise "event is required" unless event
91
+ raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash)
92
+ raise "No distinct_id is set. Use 'identify' or 'alias' to set current session distinct_id" if opts[:distinct_id].nil?
93
+ data = {
94
+ :token => @api_key,
95
+ :data => {
96
+ :distinct_id => opts[:distinct_id],
97
+ :event => event,
98
+ :channel => opts[:channel],
99
+ :properties => opts[:properties],
100
+ },
101
+ }.to_json
102
+
103
+ # Set current session variable for distinct_id
104
+ self.distinct_id = opts[:distinct_id]
105
+
106
+ execute_request('/v1/track', data)
107
+ end
108
+
109
+ # Page view is just a wrapper for: Trak.track('Page view')
110
+ #
111
+ # @param url [String] The key for this event, this value will be standardized server side.
112
+ # @param page_title [String] The distinct id of the person you wish to register this event against. When ommited the current session's distinct id is used.
113
+ # @param opts [Hash] options to pass to track call (distinct_id [String], channel [String])
114
+ # @return [Object]
115
+ #
116
+ def page_view(url, page_title, opts = {})
117
+ defaults = {
118
+ :event => 'Page view',
119
+ :distinct_id => self.distinct_id,
120
+ :channel => self.channel,
121
+ :properties => {
122
+ :url => url,
123
+ :page_title => page_title,
124
+ },
125
+ }
126
+ opts = defaults.merge opts
127
+ raise "url" unless url
128
+ raise "page_title" unless page_title
129
+ raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash)
130
+ raise "No distinct_id is set. Use 'identify' or 'alias' to set current session distinct_id" if opts[:distinct_id].nil?
131
+ data = {
132
+ :token => @api_key,
133
+ :data => {
134
+ :distinct_id => opts[:distinct_id],
135
+ :event => opts[:event],
136
+ :channel => opts[:channel],
137
+ :properties => opts[:properties],
138
+ },
139
+ }.to_json
140
+
141
+ # Set current session variable for distinct_id
142
+ self.distinct_id = opts[:distinct_id]
143
+
144
+ execute_request('/v1/track', data)
145
+ end
146
+
147
+ # Annotate is a way of recording system wide events that affect everyone.
148
+ # Annotations are similar to events except they are not associated with
149
+ # any one person.
150
+ #
151
+ # @param event [String] The key for this annotation, this value will be standardized server side.
152
+ # @param opts [Hash] options to pass to annotate call (channel [String], properties [Hash])
153
+ # @return [Object]
154
+ #
155
+ def annotate(event, opts = {})
156
+ defaults = {
157
+ :channel => channel,
158
+ :properties => {},
159
+ }
160
+ opts = defaults.merge opts
161
+ raise "event is required" unless event
162
+ raise "properties must be a Hash" unless defaults[:properties].kind_of?(Hash)
163
+ data = {
164
+ :token => @api_key,
165
+ :data => {
166
+ :event => event,
167
+ :channel => opts[:channel],
168
+ :properties => opts[:properties],
169
+ },
170
+ }.to_json
171
+
172
+ execute_request('/v1/annotate', data)
173
+ end
174
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ $:.unshift(File.join(File.dirname(__FILE__), 'lib'))
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = %q{trak-ruby}
6
+ s.version = '0.0.3'
7
+ s.authors = ["Brian Sewell"]
8
+ s.email = ["bwsewell@gmail.com"]
9
+ s.description = %q{trak.io helps you track the metrics that actually matter to your startup}
10
+ s.summary = %q{Ruby bindings for the trak.io API}
11
+ s.homepage = %q{http://docs.trak.io/}
12
+ s.license = "MIT"
13
+ s.default_executable = %q{trak}
14
+
15
+ s.files = `git ls-files`.split($/)
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_development_dependency "bundler", "~> 1.3"
21
+ s.add_development_dependency "rake"
22
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trak-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Sewell
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-20 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: &70310242273160 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70310242273160
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ requirement: &70310242848120 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70310242848120
36
+ description: trak.io helps you track the metrics that actually matter to your startup
37
+ email:
38
+ - bwsewell@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .DS_Store
44
+ - .gitignore
45
+ - Gemfile
46
+ - LICENSE.txt
47
+ - README.md
48
+ - Rakefile
49
+ - VERSION
50
+ - lib/.DS_Store
51
+ - lib/trak.rb
52
+ - trak-ruby.gemspec
53
+ homepage: http://docs.trak.io/
54
+ licenses:
55
+ - MIT
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.8.15
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Ruby bindings for the trak.io API
78
+ test_files: []