analytics-ruby 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +6 -2
- data/README.md +5 -110
- data/{analytics.gemspec → analytics-ruby.gemspec} +2 -2
- data/lib/{analytics.rb → analytics-ruby.rb} +7 -4
- data/lib/{analytics → analytics-ruby}/client.rb +6 -6
- data/lib/{analytics → analytics-ruby}/consumer.rb +5 -5
- data/lib/analytics-ruby/defaults.rb +18 -0
- data/lib/{analytics → analytics-ruby}/request.rb +8 -8
- data/lib/{analytics → analytics-ruby}/response.rb +1 -1
- data/lib/analytics-ruby/version.rb +3 -0
- data/spec/client_spec.rb +1 -1
- data/spec/consumer_spec.rb +1 -1
- data/spec/module_spec.rb +1 -1
- metadata +20 -20
- data/lib/analytics/defaults.rb +0 -18
- data/lib/analytics/version.rb +0 -3
data/History.md
CHANGED
@@ -1,10 +1,14 @@
|
|
1
|
+
0.0.5 / 2013-1-21
|
2
|
+
===========
|
3
|
+
* Renaming of all the files for proper bundling usage
|
4
|
+
|
1
5
|
0.0.4 / 2013-1-17
|
2
|
-
|
6
|
+
===========
|
3
7
|
* Updated readme and install instruction courtesy of [@zeke](https://github.com/zeke)
|
4
8
|
* Removed typhoeus and reverted to default adapter
|
5
9
|
* Removing session_id in favor of a single user_id
|
6
10
|
|
7
11
|
0.0.3 / 2013-1-16
|
8
|
-
|
12
|
+
===========
|
9
13
|
* Rakefile and renaming courtesy of [@kiennt](https://github.com/kiennt)
|
10
14
|
* Updated tests with mocks
|
data/README.md
CHANGED
@@ -1,120 +1,15 @@
|
|
1
1
|
analytics-ruby
|
2
2
|
==============
|
3
3
|
|
4
|
-
analytics-ruby is a ruby client for [Segment.io](https://segment.io).
|
5
|
-
|
6
4
|
[![Build Status](https://travis-ci.org/segmentio/analytics-ruby.png?branch=master)](https://travis-ci.org/segmentio/analytics-ruby)
|
7
5
|
|
8
|
-
|
9
|
-
|
10
|
-
[Segment.io](https://segment.io) is the cleanest, simplest API for recording analytics data.
|
11
|
-
|
12
|
-
Setting up a new analytics solution can be a real pain. The APIs from each analytics provider are slightly different in odd ways, code gets messy, and developers waste a bunch of time fiddling with long-abandoned client libraries. We want to save you that pain and give you an clean, efficient, extensible analytics setup.
|
13
|
-
|
14
|
-
[Segment.io](https://segment.io) wraps all those APIs in one beautiful, simple API. Then we route your analytics data wherever you want, whether it's Google Analytics, Mixpanel, Customer io, Chartbeat, or any of our other integrations. After you set up Segment.io you can swap or add analytics providers at any time with a single click. You won't need to touch code or push to production. You'll save valuable development time so that you can focus on what really matters: your product.
|
15
|
-
|
16
|
-
### High Performance
|
17
|
-
|
18
|
-
This client uses an internal queue to efficiently send your events in aggregate, rather than making an HTTP
|
19
|
-
request every time. It is also non-blocking and asynchronous, meaning it makes batch requests on another thread. This allows your code to call `analytics.track` or `analytics.identify` without incurring a large performance cost on the calling thread. Because of this, analytics-ruby is safe to use in your high scale web server controllers, or in your backend services
|
20
|
-
without worrying that it will make too many HTTP requests and slow down the program. You also no longer need to use a message queue to have analytics.
|
21
|
-
|
22
|
-
[Feedback is very welcome!](mailto:friends@segment.io)
|
23
|
-
|
24
|
-
## Quick-start
|
25
|
-
|
26
|
-
If you haven't yet, get an API secret [here](https://segment.io).
|
27
|
-
|
28
|
-
#### Install
|
29
|
-
|
30
|
-
Add analytics-ruby to your project's Gemfile
|
31
|
-
```ruby
|
32
|
-
gem "analytics-ruby"
|
33
|
-
```
|
34
|
-
|
35
|
-
#### Initialize the client
|
36
|
-
|
37
|
-
You can create separate analytics-ruby clients, but the easiest and recommended way is
|
38
|
-
to just use the module. If you're using Rails, put this in `config/initializers/analytics.rb`
|
39
|
-
|
40
|
-
```ruby
|
41
|
-
Analytics.init 'secrettoken'
|
42
|
-
```
|
43
|
-
|
44
|
-
#### Identify a User
|
45
|
-
|
46
|
-
Whenever a user triggers an event, you’ll want to track it.
|
47
|
-
|
48
|
-
```ruby
|
49
|
-
Analytics.identify(
|
50
|
-
user_id: 'ilya@segment.io',
|
51
|
-
traits: {subscription_plan: "Free", friends: 30}
|
52
|
-
)
|
53
|
-
```
|
54
|
-
|
55
|
-
**user_id** (String) is the user's id **after** they are logged in. It's the same id you'd use to identify a signed-in user in your system.
|
56
|
-
|
57
|
-
**traits** (Hash) is a Hash with keys like `subscriptionPlan` or `favoriteGenre`. This argument is optional, but highly recommended—you’ll find these properties extremely useful later.
|
58
|
-
|
59
|
-
**timestamp** (Time, optional) is a Time object representing when the identify took place. If the event just happened, don't bother adding a time and we'll use the server's time. If you are importing data from the past, make sure you provide this argument.
|
60
|
-
|
61
|
-
#### Track an Action
|
62
|
-
|
63
|
-
Whenever a user triggers an event on your site, you’ll want to track it so that you can analyze and segment by those events later.
|
64
|
-
|
65
|
-
```ruby
|
66
|
-
Analytics.track(
|
67
|
-
user_id: 'ilya@segment.io',
|
68
|
-
event: 'Drank some milk',
|
69
|
-
properties: {fat: 0.02, quantity: '4 gallons'}
|
70
|
-
)
|
71
|
-
```
|
72
|
-
|
73
|
-
**user_id** (String) is the user's id **after** they are logged in. It's the same id as which you would recognize a signed-in user in your system.
|
74
|
-
|
75
|
-
**event** (String) describes what this user just did. It's a human readable description like "Played a Song", "Printed a Report" or "Updated Status".
|
76
|
-
|
77
|
-
**properties** (Hash) is a hash with items that describe the event in more detail. This argument is optional, but highly recommended—you’ll find these properties extremely useful later.
|
78
|
-
|
79
|
-
**timestamp** (Time, optional) is a Time object representing when the identify took place. If the event just happened, leave it `nil` and we'll use the server's time. If you are importing data from the past, make sure you provide this argument.
|
80
|
-
|
81
|
-
That's it, just two functions!
|
82
|
-
|
83
|
-
## Integrations
|
84
|
-
|
85
|
-
There are two main modes of analytics integration: client-side and server-side. You can use just one, or both.
|
86
|
-
|
87
|
-
#### Client-side vs. Server-side
|
88
|
-
|
89
|
-
* **Client-side analytics** - (via [analytics.js](https://github.com/segmentio/analytics.js)) works by loading in other integrations
|
90
|
-
in the browser.
|
91
|
-
|
92
|
-
* **Server-side analytics** - (via [analytics-node](https://github.com/segmentio/analytics-node), [analytics-python](https://github.com/segmentio/analytics-python) and other server-side libraries) works
|
93
|
-
by sending the analytics request to [Segment.io](https://segment.io). Our servers then route the message to your desired integrations.
|
94
|
-
|
95
|
-
Some analytics services have REST APIs while others only support client-side integrations.
|
96
|
-
|
97
|
-
You can learn which integrations are supported server-side vs. client-side on your [project's integrations]((https://segment.io) page.
|
98
|
-
|
99
|
-
## Advanced
|
100
|
-
|
101
|
-
#### Batching Behavior
|
102
|
-
|
103
|
-
By default, the client will flush:
|
104
|
-
|
105
|
-
1. the first time it gets a message
|
106
|
-
1. whenever messages are queued and there is no outstanding request
|
107
|
-
|
108
|
-
The queue consumer runs in a different thread for each client to avoid blocking your webserver process. However, the consumer makes only a single outbound request at a time to avoid saturating your server's resources. If multiple messages are in the queue, they are sent together in a batch call.
|
6
|
+
analytics-ruby is a ruby client for [Segment.io](https://segment.io)
|
109
7
|
|
110
|
-
|
8
|
+
## Documentation
|
111
9
|
|
112
|
-
|
113
|
-
Time) to the identify / track calls. Note: if you are tracking
|
114
|
-
things that are happening now, we prefer that you leave the timestamp out and
|
115
|
-
let our servers timestamp your requests.
|
10
|
+
Documentation is available at [https://segment.io/libraries/ruby](https://segment.io/libraries/ruby).
|
116
11
|
|
117
|
-
|
12
|
+
## License
|
118
13
|
|
119
14
|
```
|
120
15
|
WWWWWW||WWWWWW
|
@@ -137,4 +32,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
137
32
|
|
138
33
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
139
34
|
|
140
|
-
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
35
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
@@ -1,11 +1,11 @@
|
|
1
1
|
$:.push File.expand_path('../lib', __FILE__)
|
2
2
|
|
3
|
-
require 'analytics/version'
|
3
|
+
require 'analytics-ruby/version'
|
4
4
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
7
7
|
spec.name = 'analytics-ruby'
|
8
|
-
spec.version =
|
8
|
+
spec.version = AnalyticsRuby::VERSION
|
9
9
|
spec.files = Dir.glob('**/*')
|
10
10
|
spec.require_paths = ['lib']
|
11
11
|
spec.summary = 'Segment.io analytics library'
|
@@ -1,16 +1,19 @@
|
|
1
1
|
|
2
2
|
require 'forwardable'
|
3
|
-
require 'analytics/version'
|
4
|
-
require 'analytics/client'
|
3
|
+
require 'analytics-ruby/version'
|
4
|
+
require 'analytics-ruby/client'
|
5
5
|
|
6
|
-
module
|
6
|
+
module AnalyticsRuby
|
7
7
|
extend SingleForwardable
|
8
8
|
|
9
9
|
def_delegators :@client, :track, :identify
|
10
10
|
|
11
11
|
# By default use a single client for the module
|
12
12
|
def self.init(options = {})
|
13
|
-
@client =
|
13
|
+
@client = AnalyticsRuby::Client.new(options)
|
14
14
|
end
|
15
15
|
|
16
16
|
end
|
17
|
+
|
18
|
+
# Alias for AnalyticsRuby
|
19
|
+
Analytics = AnalyticsRuby
|
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
require 'time'
|
3
3
|
require 'thread'
|
4
|
-
require 'analytics/defaults'
|
5
|
-
require 'analytics/consumer'
|
6
|
-
require 'analytics/request'
|
4
|
+
require 'analytics-ruby/defaults'
|
5
|
+
require 'analytics-ruby/consumer'
|
6
|
+
require 'analytics-ruby/request'
|
7
7
|
|
8
|
-
module
|
8
|
+
module AnalyticsRuby
|
9
9
|
|
10
10
|
class Client
|
11
11
|
|
@@ -19,11 +19,11 @@ module Analytics
|
|
19
19
|
|
20
20
|
@queue = Queue.new
|
21
21
|
@secret = options[:secret]
|
22
|
-
@max_queue_size = options[:max_queue_size] ||
|
22
|
+
@max_queue_size = options[:max_queue_size] || AnalyticsRuby::Defaults::Queue::MAX_SIZE
|
23
23
|
|
24
24
|
check_secret
|
25
25
|
|
26
|
-
@consumer =
|
26
|
+
@consumer = AnalyticsRuby::Consumer.new(@queue, @secret, options)
|
27
27
|
Thread.new { @consumer.run }
|
28
28
|
end
|
29
29
|
|
@@ -1,8 +1,8 @@
|
|
1
1
|
|
2
|
-
require 'analytics/defaults'
|
3
|
-
require 'analytics/request'
|
2
|
+
require 'analytics-ruby/defaults'
|
3
|
+
require 'analytics-ruby/request'
|
4
4
|
|
5
|
-
module
|
5
|
+
module AnalyticsRuby
|
6
6
|
|
7
7
|
class Consumer
|
8
8
|
|
@@ -20,7 +20,7 @@ module Analytics
|
|
20
20
|
def initialize(queue, secret, options = {})
|
21
21
|
@queue = queue
|
22
22
|
@secret = secret
|
23
|
-
@batch_size = options[:batch_size] ||
|
23
|
+
@batch_size = options[:batch_size] || AnalyticsRuby::Defaults::Queue::BATCH_SIZE
|
24
24
|
@on_error = options[:on_error] || Proc.new { |status, error| }
|
25
25
|
|
26
26
|
@current_batch = []
|
@@ -45,7 +45,7 @@ module Analytics
|
|
45
45
|
@current_batch << @queue.pop()
|
46
46
|
end
|
47
47
|
|
48
|
-
req =
|
48
|
+
req = AnalyticsRuby::Request.new
|
49
49
|
res = req.post(@secret, @current_batch)
|
50
50
|
@on_error.call(res.status, res.error) unless res.status == 200
|
51
51
|
@current_batch = []
|
@@ -0,0 +1,18 @@
|
|
1
|
+
|
2
|
+
module AnalyticsRuby
|
3
|
+
module Defaults
|
4
|
+
|
5
|
+
module Request
|
6
|
+
BASE_URL = 'https://api.segment.io' unless defined? AnalyticsRuby::Defaults::Request::BASE_URL
|
7
|
+
PATH = '/v1/import' unless defined? AnalyticsRuby::Defaults::Request::PATH
|
8
|
+
SSL = { verify: false } unless defined? AnalyticsRuby::Defaults::Request::SSL
|
9
|
+
HEADERS = { accept: 'application/json' } unless defined? AnalyticsRuby::Defaults::Request::HEADERS
|
10
|
+
end
|
11
|
+
|
12
|
+
module Queue
|
13
|
+
BATCH_SIZE = 100 unless defined? AnalyticsRuby::Defaults::Queue::BATCH_SIZE
|
14
|
+
MAX_SIZE = 10000 unless defined? AnalyticsRuby::Defaults::Queue::MAX_SIZE
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
|
-
require 'analytics/defaults'
|
3
|
-
require 'analytics/response'
|
2
|
+
require 'analytics-ruby/defaults'
|
3
|
+
require 'analytics-ruby/response'
|
4
4
|
require 'multi_json'
|
5
5
|
require 'faraday'
|
6
6
|
require 'faraday_middleware'
|
7
7
|
|
8
|
-
module
|
8
|
+
module AnalyticsRuby
|
9
9
|
|
10
10
|
class Request
|
11
11
|
|
@@ -13,10 +13,10 @@ module Analytics
|
|
13
13
|
#
|
14
14
|
def initialize(options = {})
|
15
15
|
|
16
|
-
options[:url] ||=
|
17
|
-
options[:ssl] ||=
|
18
|
-
options[:headers] ||=
|
19
|
-
@path = options[:path] ||
|
16
|
+
options[:url] ||= AnalyticsRuby::Defaults::Request::BASE_URL
|
17
|
+
options[:ssl] ||= AnalyticsRuby::Defaults::Request::SSL
|
18
|
+
options[:headers] ||= AnalyticsRuby::Defaults::Request::HEADERS
|
19
|
+
@path = options[:path] || AnalyticsRuby::Defaults::Request::PATH
|
20
20
|
|
21
21
|
@conn = Faraday.new(options) do |faraday|
|
22
22
|
faraday.request :json
|
@@ -45,7 +45,7 @@ module Analytics
|
|
45
45
|
error = "Connection error: #{err}"
|
46
46
|
end
|
47
47
|
|
48
|
-
|
48
|
+
AnalyticsRuby::Response.new(status, error)
|
49
49
|
end
|
50
50
|
end
|
51
51
|
end
|
data/spec/client_spec.rb
CHANGED
data/spec/consumer_spec.rb
CHANGED
data/spec/module_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: analytics-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-01-
|
12
|
+
date: 2013-01-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
16
|
-
requirement: &
|
16
|
+
requirement: &22763500 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -24,10 +24,10 @@ dependencies:
|
|
24
24
|
version: '0.10'
|
25
25
|
type: :runtime
|
26
26
|
prerelease: false
|
27
|
-
version_requirements: *
|
27
|
+
version_requirements: *22763500
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: faraday_middleware
|
30
|
-
requirement: &
|
30
|
+
requirement: &22762720 !ruby/object:Gem::Requirement
|
31
31
|
none: false
|
32
32
|
requirements:
|
33
33
|
- - ~>
|
@@ -35,10 +35,10 @@ dependencies:
|
|
35
35
|
version: 0.9.0
|
36
36
|
type: :runtime
|
37
37
|
prerelease: false
|
38
|
-
version_requirements: *
|
38
|
+
version_requirements: *22762720
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: multi_json
|
41
|
-
requirement: &
|
41
|
+
requirement: &21362180 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
@@ -46,10 +46,10 @@ dependencies:
|
|
46
46
|
version: '1.0'
|
47
47
|
type: :runtime
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
49
|
+
version_requirements: *21362180
|
50
50
|
- !ruby/object:Gem::Dependency
|
51
51
|
name: rake
|
52
|
-
requirement: &
|
52
|
+
requirement: &21361140 !ruby/object:Gem::Requirement
|
53
53
|
none: false
|
54
54
|
requirements:
|
55
55
|
- - ! '>='
|
@@ -57,10 +57,10 @@ dependencies:
|
|
57
57
|
version: '0'
|
58
58
|
type: :development
|
59
59
|
prerelease: false
|
60
|
-
version_requirements: *
|
60
|
+
version_requirements: *21361140
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
|
-
requirement: &
|
63
|
+
requirement: &21360000 !ruby/object:Gem::Requirement
|
64
64
|
none: false
|
65
65
|
requirements:
|
66
66
|
- - ! '>='
|
@@ -68,24 +68,24 @@ dependencies:
|
|
68
68
|
version: '0'
|
69
69
|
type: :development
|
70
70
|
prerelease: false
|
71
|
-
version_requirements: *
|
71
|
+
version_requirements: *21360000
|
72
72
|
description: The Segment.io ruby analytics library
|
73
73
|
email: friends@segment.io
|
74
74
|
executables: []
|
75
75
|
extensions: []
|
76
76
|
extra_rdoc_files: []
|
77
77
|
files:
|
78
|
-
- analytics.gemspec
|
79
78
|
- Gemfile
|
80
79
|
- README.md
|
81
|
-
- lib/analytics.rb
|
82
|
-
- lib/analytics/response.rb
|
83
|
-
- lib/analytics/defaults.rb
|
84
|
-
- lib/analytics/consumer.rb
|
85
|
-
- lib/analytics/request.rb
|
86
|
-
- lib/analytics/version.rb
|
87
|
-
- lib/analytics/client.rb
|
80
|
+
- lib/analytics-ruby.rb
|
81
|
+
- lib/analytics-ruby/response.rb
|
82
|
+
- lib/analytics-ruby/defaults.rb
|
83
|
+
- lib/analytics-ruby/consumer.rb
|
84
|
+
- lib/analytics-ruby/request.rb
|
85
|
+
- lib/analytics-ruby/version.rb
|
86
|
+
- lib/analytics-ruby/client.rb
|
88
87
|
- History.md
|
88
|
+
- analytics-ruby.gemspec
|
89
89
|
- Rakefile
|
90
90
|
- spec/client_spec.rb
|
91
91
|
- spec/module_spec.rb
|
data/lib/analytics/defaults.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
|
2
|
-
module Analytics
|
3
|
-
module Defaults
|
4
|
-
|
5
|
-
module Request
|
6
|
-
BASE_URL = 'https://api.segment.io' unless defined? Analytics::Defaults::Request::BASE_URL
|
7
|
-
PATH = '/v1/import' unless defined? Analytics::Defaults::Request::PATH
|
8
|
-
SSL = { verify: false } unless defined? Analytics::Defaults::Request::SSL
|
9
|
-
HEADERS = { accept: 'application/json' } unless defined? Analytics::Defaults::Request::HEADERS
|
10
|
-
end
|
11
|
-
|
12
|
-
module Queue
|
13
|
-
BATCH_SIZE = 100 unless defined? Analytics::Defaults::Queue::BATCH_SIZE
|
14
|
-
MAX_SIZE = 10000 unless defined? Analytics::Defaults::Queue::MAX_SIZE
|
15
|
-
end
|
16
|
-
|
17
|
-
end
|
18
|
-
end
|
data/lib/analytics/version.rb
DELETED