analytics-ruby 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/History.md +7 -1
- data/README.md +17 -36
- data/analytics.gemspec +0 -1
- data/lib/analytics/client.rb +11 -19
- data/lib/analytics/request.rb +1 -3
- data/lib/analytics/version.rb +1 -1
- data/spec/client_spec.rb +1 -1
- data/spec/module_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -4
- metadata +11 -22
data/History.md
CHANGED
@@ -1,4 +1,10 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4 / 2013-1-17
|
2
|
+
==========
|
3
|
+
* Updated readme and install instruction courtesy of [@zeke](https://github.com/zeke)
|
4
|
+
* Removed typhoeus and reverted to default adapter
|
5
|
+
* Removing session_id in favor of a single user_id
|
6
|
+
|
7
|
+
0.0.3 / 2013-1-16
|
2
8
|
==========
|
3
9
|
* Rakefile and renaming courtesy of [@kiennt](https://github.com/kiennt)
|
4
10
|
* Updated tests with mocks
|
data/README.md
CHANGED
@@ -13,19 +13,6 @@ Setting up a new analytics solution can be a real pain. The APIs from each analy
|
|
13
13
|
|
14
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
15
|
|
16
|
-
```ruby
|
17
|
-
gem "analytics-ruby"; require "analytics"
|
18
|
-
Analytics.init "secrettoken"
|
19
|
-
Analytics.track(user_id: "ilya@segment.io",
|
20
|
-
event: "Played a Song")
|
21
|
-
```
|
22
|
-
|
23
|
-
and turn on integrations with just one click at [Segment.io](https://segment.io).
|
24
|
-
|
25
|
-
![](http://i.imgur.com/YnBWI.png)
|
26
|
-
|
27
|
-
More on integrations [here](#integrations).
|
28
|
-
|
29
16
|
### High Performance
|
30
17
|
|
31
18
|
This client uses an internal queue to efficiently send your events in aggregate, rather than making an HTTP
|
@@ -39,16 +26,18 @@ without worrying that it will make too many HTTP requests and slow down the prog
|
|
39
26
|
If you haven't yet, get an API secret [here](https://segment.io).
|
40
27
|
|
41
28
|
#### Install
|
42
|
-
|
43
|
-
|
29
|
+
|
30
|
+
Add analytics-ruby to your project's Gemfile
|
31
|
+
```ruby
|
32
|
+
gem "analytics-ruby"
|
44
33
|
```
|
45
34
|
|
46
35
|
#### Initialize the client
|
47
36
|
|
48
|
-
You can create separate analytics-ruby clients, but the easiest and recommended way is
|
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`
|
49
39
|
|
50
40
|
```ruby
|
51
|
-
gem 'analytics-ruby'; require 'analytics'
|
52
41
|
Analytics.init 'secrettoken'
|
53
42
|
```
|
54
43
|
|
@@ -57,16 +46,13 @@ Analytics.init 'secrettoken'
|
|
57
46
|
Whenever a user triggers an event, you’ll want to track it.
|
58
47
|
|
59
48
|
```ruby
|
60
|
-
Analytics.identify(
|
61
|
-
|
62
|
-
|
63
|
-
|
49
|
+
Analytics.identify(
|
50
|
+
user_id: 'ilya@segment.io',
|
51
|
+
traits: {subscription_plan: "Free", friends: 30}
|
52
|
+
)
|
64
53
|
```
|
65
54
|
|
66
|
-
**
|
67
|
-
is logged in, you can still send us the **session_id** or you can just use `nil`.
|
68
|
-
|
69
|
-
**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. Note: you must provide either a `session_id` or a `user_id`.
|
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.
|
70
56
|
|
71
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.
|
72
58
|
|
@@ -77,19 +63,14 @@ is logged in, you can still send us the **session_id** or you can just use `nil`
|
|
77
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.
|
78
64
|
|
79
65
|
```ruby
|
80
|
-
Analytics.track(
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
quantity: '4 gallons' })
|
66
|
+
Analytics.track(
|
67
|
+
user_id: 'ilya@segment.io',
|
68
|
+
event: 'Drank some milk',
|
69
|
+
properties: {fat: 0.02, quantity: '4 gallons'}
|
70
|
+
)
|
86
71
|
```
|
87
72
|
|
88
|
-
|
89
|
-
**session_id** (String) is a unique id associated with an anonymous user **before** they are logged in. Even if the user
|
90
|
-
is logged in, you can still send us the **session_id** or you can just use `nil`.
|
91
|
-
|
92
|
-
**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. Note: you must provide either a `session_id` or a `user_id`.
|
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.
|
93
74
|
|
94
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".
|
95
76
|
|
data/analytics.gemspec
CHANGED
@@ -17,7 +17,6 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.add_dependency 'faraday', ['~> 0.8', '< 0.10']
|
18
18
|
spec.add_dependency 'faraday_middleware', ['~> 0.9.0']
|
19
19
|
spec.add_dependency 'multi_json', ['~> 1.0']
|
20
|
-
spec.add_dependency 'typhoeus', ['~> 0.5.0']
|
21
20
|
|
22
21
|
spec.add_development_dependency('rake')
|
23
22
|
spec.add_development_dependency('rspec')
|
data/lib/analytics/client.rb
CHANGED
@@ -31,23 +31,21 @@ module Analytics
|
|
31
31
|
#
|
32
32
|
# options - Hash
|
33
33
|
# :event - String of event name.
|
34
|
-
# :
|
35
|
-
# :userId - String of the user id. (optional with sessionId)
|
36
|
-
# :context - Hash of context. (optional)
|
34
|
+
# :user_id - String of the user id.
|
37
35
|
# :properties - Hash of event properties. (optional)
|
38
36
|
# :timestamp - Time of when the event occurred. (optional)
|
37
|
+
# :context - Hash of context. (optional)
|
39
38
|
def track(options)
|
40
39
|
|
41
40
|
check_secret
|
42
41
|
|
43
42
|
event = options[:event]
|
44
|
-
session_id = options[:session_id]
|
45
43
|
user_id = options[:user_id]
|
46
|
-
context = options[:context] || {}
|
47
44
|
properties = options[:properties] || {}
|
48
45
|
timestamp = options[:timestamp] || Time.new
|
46
|
+
context = options[:context] || {}
|
49
47
|
|
50
|
-
ensure_user(
|
48
|
+
ensure_user(user_id)
|
51
49
|
check_timestamp(timestamp)
|
52
50
|
|
53
51
|
if event.nil? || event.empty?
|
@@ -57,7 +55,6 @@ module Analytics
|
|
57
55
|
add_context(context)
|
58
56
|
|
59
57
|
enqueue({ event: event,
|
60
|
-
sessionId: session_id,
|
61
58
|
userId: user_id,
|
62
59
|
context: context,
|
63
60
|
properties: properties,
|
@@ -68,30 +65,27 @@ module Analytics
|
|
68
65
|
# public: Identifies a user
|
69
66
|
#
|
70
67
|
# options - Hash
|
71
|
-
# :
|
72
|
-
# :userId - String of the user id. (optional with sessionId)
|
73
|
-
# :context - Hash of context. (optional)
|
68
|
+
# :user_id - String of the user id
|
74
69
|
# :traits - Hash of user traits. (optional)
|
75
70
|
# :timestamp - Time of when the event occurred. (optional)
|
71
|
+
# :context - Hash of context. (optional)
|
76
72
|
def identify(options)
|
77
73
|
|
78
74
|
check_secret
|
79
75
|
|
80
|
-
session_id = options[:session_id]
|
81
76
|
user_id = options[:user_id]
|
82
|
-
context = options[:context] || {}
|
83
77
|
traits = options[:traits] || {}
|
84
78
|
timestamp = options[:timestamp] || Time.new
|
79
|
+
context = options[:context] || {}
|
85
80
|
|
86
|
-
ensure_user(
|
81
|
+
ensure_user(user_id)
|
87
82
|
check_timestamp(timestamp)
|
88
83
|
|
89
84
|
fail ArgumentError, 'Must supply traits as a hash' unless traits.is_a? Hash
|
90
85
|
|
91
86
|
add_context(context)
|
92
87
|
|
93
|
-
enqueue({
|
94
|
-
userId: user_id,
|
88
|
+
enqueue({ userId: user_id,
|
95
89
|
context: context,
|
96
90
|
traits: traits,
|
97
91
|
timestamp: timestamp.iso8601,
|
@@ -119,14 +113,12 @@ module Analytics
|
|
119
113
|
|
120
114
|
# private: Ensures that a user id was passed in.
|
121
115
|
#
|
122
|
-
# session_id - String of the session
|
123
116
|
# user_id - String of the user id
|
124
117
|
#
|
125
|
-
def ensure_user(
|
126
|
-
message = 'Must supply
|
118
|
+
def ensure_user(user_id)
|
119
|
+
message = 'Must supply a non-empty user_id'
|
127
120
|
|
128
121
|
valid = user_id.is_a?(String) && !user_id.empty?
|
129
|
-
valid ||= session_id.is_a?(String) && !session_id.empty?
|
130
122
|
|
131
123
|
fail ArgumentError, message unless valid
|
132
124
|
end
|
data/lib/analytics/request.rb
CHANGED
@@ -4,8 +4,6 @@ require 'analytics/response'
|
|
4
4
|
require 'multi_json'
|
5
5
|
require 'faraday'
|
6
6
|
require 'faraday_middleware'
|
7
|
-
require 'typhoeus'
|
8
|
-
require 'typhoeus/adapters/faraday'
|
9
7
|
|
10
8
|
module Analytics
|
11
9
|
|
@@ -23,7 +21,7 @@ module Analytics
|
|
23
21
|
@conn = Faraday.new(options) do |faraday|
|
24
22
|
faraday.request :json
|
25
23
|
faraday.response :json, :content_type => /\bjson$/
|
26
|
-
faraday.adapter
|
24
|
+
faraday.adapter Faraday.default_adapter
|
27
25
|
end
|
28
26
|
end
|
29
27
|
|
data/lib/analytics/version.rb
CHANGED
data/spec/client_spec.rb
CHANGED
@@ -26,7 +26,7 @@ describe Analytics::Client do
|
|
26
26
|
expect { @client.track(user_id: 'user') }.to raise_error(ArgumentError)
|
27
27
|
end
|
28
28
|
|
29
|
-
it 'should error without a
|
29
|
+
it 'should error without a user_id' do
|
30
30
|
expect { @client.track(event: 'Event') }.to raise_error(ArgumentError)
|
31
31
|
end
|
32
32
|
|
data/spec/module_spec.rb
CHANGED
@@ -17,7 +17,7 @@ describe Analytics do
|
|
17
17
|
expect { Analytics.track user_id: 'user' }.to raise_error(ArgumentError)
|
18
18
|
end
|
19
19
|
|
20
|
-
it 'should error without a
|
20
|
+
it 'should error without a user_id' do
|
21
21
|
expect { Analytics.track event: 'Event' }.to raise_error(ArgumentError)
|
22
22
|
end
|
23
23
|
|
@@ -28,7 +28,7 @@ describe Analytics do
|
|
28
28
|
|
29
29
|
|
30
30
|
describe '#identify' do
|
31
|
-
it 'should error without a
|
31
|
+
it 'should error without a user_id' do
|
32
32
|
expect { Analytics.identify traits: {} }.to raise_error(ArgumentError)
|
33
33
|
end
|
34
34
|
|
data/spec/spec_helper.rb
CHANGED
@@ -21,14 +21,12 @@ module AnalyticsHelpers
|
|
21
21
|
}
|
22
22
|
|
23
23
|
USER_ID = 'Bret'
|
24
|
-
SESSION_ID = '4815162342'
|
25
24
|
|
26
25
|
# Hashes sent to the client
|
27
26
|
module Queued
|
28
27
|
TRACK = TRACK.merge({ user_id: USER_ID })
|
29
28
|
|
30
|
-
IDENTIFY = IDENTIFY.merge({ user_id: USER_ID
|
31
|
-
session_id: SESSION_ID })
|
29
|
+
IDENTIFY = IDENTIFY.merge({ user_id: USER_ID })
|
32
30
|
end
|
33
31
|
|
34
32
|
# Hashes which are sent from the consumer
|
@@ -37,7 +35,6 @@ module AnalyticsHelpers
|
|
37
35
|
action: 'track' })
|
38
36
|
|
39
37
|
IDENTIFY = IDENTIFY.merge({ userId: USER_ID,
|
40
|
-
sessionId: SESSION_ID,
|
41
38
|
action: 'identify' })
|
42
39
|
end
|
43
40
|
end
|
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.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -13,7 +13,7 @@ date: 2013-01-17 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: faraday
|
16
|
-
requirement: &
|
16
|
+
requirement: &13401940 !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: *13401940
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: faraday_middleware
|
30
|
-
requirement: &
|
30
|
+
requirement: &13401100 !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: *13401100
|
39
39
|
- !ruby/object:Gem::Dependency
|
40
40
|
name: multi_json
|
41
|
-
requirement: &
|
41
|
+
requirement: &13400440 !ruby/object:Gem::Requirement
|
42
42
|
none: false
|
43
43
|
requirements:
|
44
44
|
- - ~>
|
@@ -46,21 +46,10 @@ dependencies:
|
|
46
46
|
version: '1.0'
|
47
47
|
type: :runtime
|
48
48
|
prerelease: false
|
49
|
-
version_requirements: *
|
50
|
-
- !ruby/object:Gem::Dependency
|
51
|
-
name: typhoeus
|
52
|
-
requirement: &22784020 !ruby/object:Gem::Requirement
|
53
|
-
none: false
|
54
|
-
requirements:
|
55
|
-
- - ~>
|
56
|
-
- !ruby/object:Gem::Version
|
57
|
-
version: 0.5.0
|
58
|
-
type: :runtime
|
59
|
-
prerelease: false
|
60
|
-
version_requirements: *22784020
|
49
|
+
version_requirements: *13400440
|
61
50
|
- !ruby/object:Gem::Dependency
|
62
51
|
name: rake
|
63
|
-
requirement: &
|
52
|
+
requirement: &13399840 !ruby/object:Gem::Requirement
|
64
53
|
none: false
|
65
54
|
requirements:
|
66
55
|
- - ! '>='
|
@@ -68,10 +57,10 @@ dependencies:
|
|
68
57
|
version: '0'
|
69
58
|
type: :development
|
70
59
|
prerelease: false
|
71
|
-
version_requirements: *
|
60
|
+
version_requirements: *13399840
|
72
61
|
- !ruby/object:Gem::Dependency
|
73
62
|
name: rspec
|
74
|
-
requirement: &
|
63
|
+
requirement: &13399080 !ruby/object:Gem::Requirement
|
75
64
|
none: false
|
76
65
|
requirements:
|
77
66
|
- - ! '>='
|
@@ -79,7 +68,7 @@ dependencies:
|
|
79
68
|
version: '0'
|
80
69
|
type: :development
|
81
70
|
prerelease: false
|
82
|
-
version_requirements: *
|
71
|
+
version_requirements: *13399080
|
83
72
|
description: The Segment.io ruby analytics library
|
84
73
|
email: friends@segment.io
|
85
74
|
executables: []
|