analytics-ruby 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +1 -0
- data/README.md +361 -0
- data/analytics.gemspec +10 -0
- data/lib/analytics.rb +15 -0
- data/lib/analytics/client.rb +144 -0
- data/lib/analytics/consumer.rb +58 -0
- data/lib/analytics/defaults.rb +18 -0
- data/lib/analytics/request.rb +37 -0
- data/spec/client.rb +21 -0
- data/spec/module.rb +29 -0
- metadata +54 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/README.md
ADDED
@@ -0,0 +1,361 @@
|
|
1
|
+
analytics-ruby
|
2
|
+
==============
|
3
|
+
|
4
|
+
analytics-ruby is a ruby client for [Segment.io](https://segment.io). If you're using multiple languages, check out our clients for [client-side javascript](https://github.com/segmentio/analytics.js) and [node](https://github.com/segmentio/analytics-node).
|
5
|
+
|
6
|
+
### Python Analytics Made Simple
|
7
|
+
|
8
|
+
[Segment.io](https://segment.io) is the cleanest, simplest API for recording analytics data.
|
9
|
+
|
10
|
+
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.
|
11
|
+
|
12
|
+
[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.
|
13
|
+
|
14
|
+
```python
|
15
|
+
import analytics
|
16
|
+
analytics.init('MY_API_SECRET')
|
17
|
+
analytics.track(user_id='ilya@segment.io', event='Played a Song')
|
18
|
+
```
|
19
|
+
|
20
|
+
and turn on integrations with just one click at [Segment.io](https://segment.io).
|
21
|
+
|
22
|
+
![](http://i.imgur.com/YnBWI.png)
|
23
|
+
|
24
|
+
More on integrations [here](#integrations).
|
25
|
+
|
26
|
+
### High Performance
|
27
|
+
|
28
|
+
This client uses an internal queue to efficiently send your events in aggregate, rather than making an HTTP
|
29
|
+
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-python is safe to use in your high scale web server controllers, or in your backend services
|
30
|
+
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.
|
31
|
+
|
32
|
+
[Feedback is very welcome!](mailto:friends@segment.io)
|
33
|
+
|
34
|
+
## Quick-start
|
35
|
+
|
36
|
+
If you haven't yet, get an API secret [here](https://segment.io).
|
37
|
+
|
38
|
+
#### Install
|
39
|
+
```bash
|
40
|
+
pip install analytics-python
|
41
|
+
```
|
42
|
+
|
43
|
+
#### Initialize the client
|
44
|
+
|
45
|
+
You can create separate analytics-python clients, but the easiest and recommended way is to just use the module:
|
46
|
+
|
47
|
+
```python
|
48
|
+
import analytics
|
49
|
+
analytics.init('MY_API_SECRET')
|
50
|
+
```
|
51
|
+
|
52
|
+
#### Identify a User
|
53
|
+
|
54
|
+
Whenever a user triggers an event, you’ll want to track it.
|
55
|
+
|
56
|
+
```python
|
57
|
+
analytics.identify(session_id='ajsk2jdj29fj298', user_id='ilya@segment.io', traits={
|
58
|
+
"subscriptionPlan": "Free",
|
59
|
+
"friends": 30
|
60
|
+
})
|
61
|
+
```
|
62
|
+
|
63
|
+
**session_id** (string) is a unique id associated with an anonymous user **before** they are logged in. Even if the user
|
64
|
+
is logged in, you can still send us the **session_id** or you can just use `null`.
|
65
|
+
|
66
|
+
**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`.
|
67
|
+
|
68
|
+
**traits** (dict) is a dictionary with keys like `subscriptionPlan` or `favoriteGenre`. This argument is optional, but highly recommended—you’ll find these properties extremely useful later.
|
69
|
+
|
70
|
+
**timestamp** (datetime, optional) is a datetime object representing when the identify took place. If the event just happened, leave it `None` and we'll use the server's time. If you are importing data from the past, make sure you provide this argument.
|
71
|
+
|
72
|
+
#### Track an Action
|
73
|
+
|
74
|
+
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.
|
75
|
+
|
76
|
+
```python
|
77
|
+
analytics.track(session_id='skdj2jj2dj2j3i5', user_id='calvin@segment.io', event='Made a Comment', properties={
|
78
|
+
"thatAided": "No-One",
|
79
|
+
"comment": "its 4AM!"
|
80
|
+
})
|
81
|
+
|
82
|
+
```
|
83
|
+
|
84
|
+
|
85
|
+
**session_id** (string) is a unique id associated with an anonymous user **before** they are logged in. Even if the user
|
86
|
+
is logged in, you can still send us the **session_id** or you can just use `null`.
|
87
|
+
|
88
|
+
**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`.
|
89
|
+
|
90
|
+
**event** (string) describes what this user just did. It's a human readable description like "Played a Song", "Printed a Report" or "Updated Status".
|
91
|
+
|
92
|
+
**properties** (dict) is a dictionary with items that describe the event in more detail. This argument is optional, but highly recommended—you’ll find these properties extremely useful later.
|
93
|
+
|
94
|
+
**timestamp** (datetime, optional) is a datetime object representing when the identify took place. If the event just happened, leave it `None` and we'll use the server's time. If you are importing data from the past, make sure you provide this argument.
|
95
|
+
|
96
|
+
That's it, just two functions!
|
97
|
+
|
98
|
+
## Integrations
|
99
|
+
|
100
|
+
There are two main modes of analytics integration: client-side and server-side. You can use just one, or both.
|
101
|
+
|
102
|
+
#### Client-side vs. Server-side
|
103
|
+
|
104
|
+
* **Client-side analytics** - (via [analytics.js](https://github.com/segmentio/analytics.js)) works by loading in other integrations
|
105
|
+
in the browser.
|
106
|
+
|
107
|
+
* **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
|
108
|
+
by sending the analytics request to [Segment.io](https://segment.io). Our servers then route the message to your desired integrations.
|
109
|
+
|
110
|
+
Some analytics services have REST APIs while others only support client-side integrations.
|
111
|
+
|
112
|
+
You can learn which integrations are supported server-side vs. client-side on your [project's integrations]((https://segment.io) page.
|
113
|
+
|
114
|
+
## Advanced
|
115
|
+
|
116
|
+
#### Batching Behavior
|
117
|
+
|
118
|
+
By default, the client will flush:
|
119
|
+
|
120
|
+
1. the first time it gets a message
|
121
|
+
1. every 20 messages (control with ```flush_at```)
|
122
|
+
1. if 10 seconds passes without a flush (control with ```flush_after```)
|
123
|
+
|
124
|
+
#### Turn Off Batching
|
125
|
+
|
126
|
+
When debugging or in short-lived programs, you might the client to make the
|
127
|
+
request right away. In this case, you can turn off batching by setting the
|
128
|
+
flush_at argument to 1.
|
129
|
+
|
130
|
+
```python
|
131
|
+
analytics.init('secret', flush_at=1)
|
132
|
+
```
|
133
|
+
|
134
|
+
|
135
|
+
#### Turn Off Asynchronous Flushing
|
136
|
+
|
137
|
+
By default, the client will create a new thread to flush the messages to the server.
|
138
|
+
This is so the calling thread doesn't block, [as is important in server side
|
139
|
+
environments](http://ivolo.me/batching-rest-apis/).
|
140
|
+
|
141
|
+
If you're not running a server or writing performance sensitive code ,
|
142
|
+
you might want to flush on the same thread that calls identify/track.
|
143
|
+
|
144
|
+
In this case, you can disable asynchronous flushing like so:
|
145
|
+
```python
|
146
|
+
analytics.init('secret', async=False)
|
147
|
+
```
|
148
|
+
|
149
|
+
#### Calling Flush Before Program End
|
150
|
+
|
151
|
+
If you're using the batching, it's a good idea to call
|
152
|
+
```python
|
153
|
+
analytics.flush(async=False)
|
154
|
+
```
|
155
|
+
before your program ends. This prevents your program from turning off with
|
156
|
+
items still in the queue.
|
157
|
+
|
158
|
+
#### Logging
|
159
|
+
|
160
|
+
analytics-python client uses the standard python logging module. By default, logging
|
161
|
+
is enabled and set at the logging.INFO level. If you want it to talk more,
|
162
|
+
|
163
|
+
```python
|
164
|
+
import logging
|
165
|
+
analytics.init('secret', log_level=logging.DEBUG)
|
166
|
+
```
|
167
|
+
|
168
|
+
If you hate logging with an undying passion, try this:
|
169
|
+
|
170
|
+
```python
|
171
|
+
analytics.init('secret', log=False)
|
172
|
+
```
|
173
|
+
|
174
|
+
#### Troubleshooting
|
175
|
+
|
176
|
+
**Turn off Async / Batching**
|
177
|
+
|
178
|
+
If you're having trouble sending messages to Segment.io, the first thing to try
|
179
|
+
is to turn off asynchronous flushing and disable batching, like so:
|
180
|
+
|
181
|
+
```python
|
182
|
+
analytics.init('secret', async=False, flush_at=1)
|
183
|
+
```
|
184
|
+
|
185
|
+
Now the client will flush on every message, and every time you call identify or
|
186
|
+
track.
|
187
|
+
|
188
|
+
**Enable Debug Logging**
|
189
|
+
|
190
|
+
```python
|
191
|
+
analytics.init('secret', async=False, flush_at=1, log_level=logging.DEBUG)
|
192
|
+
```
|
193
|
+
|
194
|
+
**Success / Failure Events**
|
195
|
+
|
196
|
+
Use events to receive successful or failed events.
|
197
|
+
```python
|
198
|
+
def on_success(data, response):
|
199
|
+
print 'Success', response
|
200
|
+
|
201
|
+
|
202
|
+
def on_failure(data, error):
|
203
|
+
print 'Failure', error
|
204
|
+
|
205
|
+
analytics.on_success(on_success)
|
206
|
+
analytics.on_failure(on_failure)
|
207
|
+
```
|
208
|
+
|
209
|
+
If there's an error, you should receive it as the second argument on the
|
210
|
+
on_failure event callback.
|
211
|
+
|
212
|
+
#### Importing Historical Data
|
213
|
+
|
214
|
+
You can import historical data by adding the timestamp argument (of type
|
215
|
+
datetime.datetime) to the identify / track calls. Note: if you are tracking
|
216
|
+
things that are happening now, we prefer that you leave the timestamp out and
|
217
|
+
let our servers timestamp your requests.
|
218
|
+
|
219
|
+
##### Example
|
220
|
+
|
221
|
+
```python
|
222
|
+
import datetime
|
223
|
+
from dateutil.tz import tzutc
|
224
|
+
|
225
|
+
when = datetime.datetime(2538, 10, 17, 0, 0, 0, 0, tzinfo=tzutc())
|
226
|
+
analytics.track(user_id=user_id, timestamp=when, event='Bought a game', properties={
|
227
|
+
"game": "Duke Nukem Forever",
|
228
|
+
})
|
229
|
+
```
|
230
|
+
|
231
|
+
##### Python and Timezones
|
232
|
+
|
233
|
+
Python's standard datetime object is broken because it
|
234
|
+
[loses timezone information](http://stackoverflow.com/questions/2331592/datetime-datetime-utcnow-why-no-tzinfo).
|
235
|
+
|
236
|
+
```python
|
237
|
+
>>> import datetime
|
238
|
+
>>> print datetime.datetime.now().isoformat()
|
239
|
+
2012-10-17T11:51:17.351481
|
240
|
+
>>> print datetime.datetime.utcnow().isoformat()
|
241
|
+
2012-10-17T18:51:17.919517
|
242
|
+
>>> print datetime.datetime.now().tzinfo
|
243
|
+
None
|
244
|
+
>>> print datetime.datetime.utcnow().tzinfo
|
245
|
+
None
|
246
|
+
````
|
247
|
+
|
248
|
+
You'll notice that a utcnow() and a now() date are very different (since I'm
|
249
|
+
in PDT, they are exactly -7:00 hours different). However, by default, Python
|
250
|
+
doesn't retain timezone information with the datetime object. This means that
|
251
|
+
our code can only guess about what timezone you were referring to.
|
252
|
+
|
253
|
+
If you have an ISO format timestamp string that contains timezone information, you
|
254
|
+
can do the following:
|
255
|
+
```python
|
256
|
+
>>> import dateutil.parser
|
257
|
+
>>> dateutil.parser.parse('2012-10-17T18:58:57.911Z')
|
258
|
+
datetime.datetime(2012, 10, 17, 18, 58, 57, 911000, tzinfo=tzutc())
|
259
|
+
```
|
260
|
+
|
261
|
+
Or if you're not parsing a string, make sure to
|
262
|
+
supply timezone information using [pytz](http://pytz.sourceforge.net/):
|
263
|
+
```python
|
264
|
+
from pytz import timezone
|
265
|
+
eastern = timezone('US/Eastern')
|
266
|
+
loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
|
267
|
+
```
|
268
|
+
|
269
|
+
Whatever your method, please include the timezone information in your datetime objects or
|
270
|
+
else your data may be in the incorrect time.
|
271
|
+
```python
|
272
|
+
# checks that dt has a timezone
|
273
|
+
assert dt.tzinfo
|
274
|
+
```
|
275
|
+
|
276
|
+
##### Server Logs Example
|
277
|
+
|
278
|
+
```python
|
279
|
+
|
280
|
+
import dateutil.parser
|
281
|
+
|
282
|
+
import analytics
|
283
|
+
analytics.init('MY_API_SECRET', async=False)
|
284
|
+
|
285
|
+
log = [
|
286
|
+
'2012-10-17T18:58:57.911Z ilya@segment.io /purchased/tshirt'
|
287
|
+
]
|
288
|
+
|
289
|
+
for entry in log:
|
290
|
+
|
291
|
+
(timestamp_str, user_id, url) = entry.split(' ')
|
292
|
+
|
293
|
+
timestamp = dateutil.parser.parse(timestamp_str) # datetime.datetime object has a timezone
|
294
|
+
|
295
|
+
# have a timezone? check yo'self
|
296
|
+
assert timestamp.tzinfo
|
297
|
+
|
298
|
+
analytics.track(user_id=user_id, timestamp=timestamp, event='Bought a shirt', properties={
|
299
|
+
"color": "Blue",
|
300
|
+
"revenue": 17.90
|
301
|
+
})
|
302
|
+
|
303
|
+
analytics.flush(async=False)
|
304
|
+
|
305
|
+
|
306
|
+
```
|
307
|
+
|
308
|
+
#### Full Client Configuration
|
309
|
+
|
310
|
+
If you hate defaults, than you'll love how configurable the Segment.io client is.
|
311
|
+
Check out these gizmos:
|
312
|
+
|
313
|
+
```python
|
314
|
+
|
315
|
+
import analytics
|
316
|
+
analytics.init('MY_API_SECRET',
|
317
|
+
log_level=logging.INFO, log=True,
|
318
|
+
flush_at=20, flush_after=datetime.timedelta(0, 10),
|
319
|
+
async=True
|
320
|
+
max_queue_size=100000)
|
321
|
+
|
322
|
+
```
|
323
|
+
|
324
|
+
|
325
|
+
* **log_level** (logging.LOG_LEVEL): The logging log level for the client talks to. Use log_level=logging.DEBUG to troubleshoot.
|
326
|
+
* **log** (bool): False to turn off logging completely, True by default
|
327
|
+
* **flush_at** (int): Specicies after how many messages the client will flush to the server. Use flush_at=1 to disable batching
|
328
|
+
* **flush_after** (datetime.timedelta): Specifies after how much time of no flushing that the server will flush. Used in conjunction with the flush_at size policy
|
329
|
+
* **async** (bool): True to have the client flush to the server on another thread, therefore not blocking code (this is the default). False to enable blocking and making the request on the calling thread.
|
330
|
+
* **max_queue_size** (int): Maximum number of elements allowed in the queue. If this condition is ever reached, that means you're identifying / tracking faster than you can flush. If this happens, let us know!
|
331
|
+
|
332
|
+
#### Testing
|
333
|
+
|
334
|
+
```bash
|
335
|
+
python test.py
|
336
|
+
```
|
337
|
+
|
338
|
+
#### License
|
339
|
+
|
340
|
+
```
|
341
|
+
WWWWWW||WWWWWW
|
342
|
+
W W W||W W W
|
343
|
+
||
|
344
|
+
( OO )__________
|
345
|
+
/ | \
|
346
|
+
/o o| MIT \
|
347
|
+
\___/||_||__||_|| *
|
348
|
+
|| || || ||
|
349
|
+
_||_|| _||_||
|
350
|
+
(__|__|(__|__|
|
351
|
+
```
|
352
|
+
|
353
|
+
(The MIT License)
|
354
|
+
|
355
|
+
Copyright (c) 2012 Segment.io Inc. <friends@segment.io>
|
356
|
+
|
357
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
358
|
+
|
359
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
360
|
+
|
361
|
+
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.
|
data/analytics.gemspec
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
$:.unshift(File.join(File.dirname(__FILE__), 'lib'))
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "analytics-ruby"
|
5
|
+
spec.version = "0.0.1"
|
6
|
+
spec.files = `git ls-files`.split("\n")
|
7
|
+
spec.require_paths = ['lib']
|
8
|
+
spec.summary = "Segment.io analytics library"
|
9
|
+
spec.authors = ["friends@segment.io"]
|
10
|
+
end
|
data/lib/analytics.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
|
2
|
+
require 'time'
|
3
|
+
require 'thread'
|
4
|
+
require 'analytics/defaults'
|
5
|
+
require 'analytics/consumer'
|
6
|
+
require 'analytics/request'
|
7
|
+
|
8
|
+
module Analytics
|
9
|
+
|
10
|
+
class Client
|
11
|
+
|
12
|
+
# Public: Creates a new client
|
13
|
+
#
|
14
|
+
# options - Hash
|
15
|
+
# :secret - String of your project's secret
|
16
|
+
# :max_queue_size - Fixnum of the max calls to remain queued (optional)
|
17
|
+
def initialize (options = {})
|
18
|
+
|
19
|
+
@queue = Queue.new
|
20
|
+
@secret = options[:secret]
|
21
|
+
@max_queue_size = options[:max_queue_size] || Analytics::Defaults::Queue::MAX_SIZE
|
22
|
+
|
23
|
+
check_secret
|
24
|
+
|
25
|
+
@consumer = Analytics::Consumer.new(@queue, @secret, options)
|
26
|
+
Thread.new { @consumer.run }
|
27
|
+
end
|
28
|
+
|
29
|
+
# Public: Tracks an event
|
30
|
+
#
|
31
|
+
# options - Hash
|
32
|
+
# :event - String of event name.
|
33
|
+
# :sessionId - String of the user session. (optional with userId)
|
34
|
+
# :userId - String of the user id. (optional with sessionId)
|
35
|
+
# :context - Hash of context. (optional)
|
36
|
+
# :properties - Hash of event properties. (optional)
|
37
|
+
# :timestamp - Time of when the event occurred. (optional)
|
38
|
+
def track(options)
|
39
|
+
|
40
|
+
check_secret
|
41
|
+
|
42
|
+
event = options[:event]
|
43
|
+
session_id = options[:session_id]
|
44
|
+
user_id = options[:user_id]
|
45
|
+
context = options[:context] || {}
|
46
|
+
properties = options[:properties] || {}
|
47
|
+
timestamp = options[:timestamp] || Time.new
|
48
|
+
|
49
|
+
ensure_user(session_id, user_id)
|
50
|
+
check_timestamp(timestamp)
|
51
|
+
|
52
|
+
if event.nil? || event.empty?
|
53
|
+
fail ArgumentError, "Must supply event as a non-empty string"
|
54
|
+
end
|
55
|
+
|
56
|
+
add_context(context)
|
57
|
+
|
58
|
+
enqueue({ event: event,
|
59
|
+
sessionId: session_id,
|
60
|
+
userId: user_id,
|
61
|
+
context: context,
|
62
|
+
properties: properties,
|
63
|
+
timestamp: timestamp.iso8601,
|
64
|
+
action: "track" })
|
65
|
+
end
|
66
|
+
|
67
|
+
# Public: Identifies a user
|
68
|
+
#
|
69
|
+
# options - Hash
|
70
|
+
# :sessionId - String of the user session. (optional with userId)
|
71
|
+
# :userId - String of the user id. (optional with sessionId)
|
72
|
+
# :context - Hash of context. (optional)
|
73
|
+
# :traits - Hash of user traits. (optional)
|
74
|
+
# :timestamp - Time of when the event occurred. (optional)
|
75
|
+
def identify(options)
|
76
|
+
|
77
|
+
check_secret
|
78
|
+
|
79
|
+
session_id = options[:session_id]
|
80
|
+
user_id = options[:user_id]
|
81
|
+
context = options[:context] || {}
|
82
|
+
traits = options[:traits] || {}
|
83
|
+
timestamp = options[:timestamp] || Time.new
|
84
|
+
|
85
|
+
ensure_user(session_id, user_id)
|
86
|
+
check_timestamp(timestamp)
|
87
|
+
|
88
|
+
fail ArgumentError, "Must supply traits as a hash" unless traits.is_a? Hash
|
89
|
+
|
90
|
+
add_context(context)
|
91
|
+
|
92
|
+
enqueue({ sessionId: session_id,
|
93
|
+
userId: user_id,
|
94
|
+
context: context,
|
95
|
+
traits: traits,
|
96
|
+
timestamp: timestamp.iso8601,
|
97
|
+
action: "identify" })
|
98
|
+
end
|
99
|
+
|
100
|
+
|
101
|
+
private
|
102
|
+
|
103
|
+
# Private: Enqueues the action.
|
104
|
+
#
|
105
|
+
# returns Boolean of whether the item was added to the queue.
|
106
|
+
def enqueue(action)
|
107
|
+
queue_full = @queue.length >= @max_queue_size
|
108
|
+
@queue << action unless queue_full
|
109
|
+
|
110
|
+
!queue_full
|
111
|
+
end
|
112
|
+
|
113
|
+
# Private: Ensures that a user id was passed in.
|
114
|
+
#
|
115
|
+
# session_id - String of the session
|
116
|
+
# user_id - String of the user id
|
117
|
+
#
|
118
|
+
def ensure_user(session_id, user_id)
|
119
|
+
message = "Must supply either a non-empty session_id or user_id (or both)"
|
120
|
+
|
121
|
+
valid = user_id.is_a?(String) && !user_id.empty?
|
122
|
+
valid ||= session_id.is_a?(String) && !session_id.empty?
|
123
|
+
|
124
|
+
fail ArgumentError, message unless valid
|
125
|
+
end
|
126
|
+
|
127
|
+
# Private: Adds contextual information to the call
|
128
|
+
#
|
129
|
+
# context - Hash of call context
|
130
|
+
def add_context(context)
|
131
|
+
context[:library] = "analytics-ruby"
|
132
|
+
end
|
133
|
+
|
134
|
+
# Private: Checks that the secret is properly initialized
|
135
|
+
def check_secret
|
136
|
+
fail "Secret must be initialized" if @secret.nil?
|
137
|
+
end
|
138
|
+
|
139
|
+
# Private: Checks the timstamp option to make sure it is a Time.
|
140
|
+
def check_timestamp(timestamp)
|
141
|
+
fail ArgumentError, "Timestamp must be a Time" unless timestamp.is_a? Time
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
|
2
|
+
require 'analytics/defaults'
|
3
|
+
require 'analytics/request'
|
4
|
+
|
5
|
+
module Analytics
|
6
|
+
|
7
|
+
class Consumer
|
8
|
+
|
9
|
+
# public: Creates a new consumer
|
10
|
+
#
|
11
|
+
# The consumer continuously takes messages off the queue
|
12
|
+
# and makes requests to the segment.io api
|
13
|
+
#
|
14
|
+
def initialize(queue, secret, options = {})
|
15
|
+
@current_batch = []
|
16
|
+
@queue = queue
|
17
|
+
@batch_size = options[:batch_size] || Analytics::Defaults::Queue::BATCH_SIZE
|
18
|
+
@secret = secret
|
19
|
+
end
|
20
|
+
|
21
|
+
# public: Continuously runs the loop to check for new events
|
22
|
+
#
|
23
|
+
def run
|
24
|
+
while true
|
25
|
+
flush
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# private: Flush some events from our queue
|
32
|
+
#
|
33
|
+
def flush
|
34
|
+
|
35
|
+
# Block until we have something to send
|
36
|
+
@current_batch << @queue.pop()
|
37
|
+
|
38
|
+
until @current_batch.length >= @batch_size || @queue.empty?
|
39
|
+
@current_batch << @queue.pop()
|
40
|
+
end
|
41
|
+
|
42
|
+
req = Analytics::Request.new
|
43
|
+
res = req.post(@secret, @current_batch)
|
44
|
+
|
45
|
+
onError(res) unless res.status == 200
|
46
|
+
|
47
|
+
@current_batch = []
|
48
|
+
end
|
49
|
+
|
50
|
+
# private: Error handler whenever the api does not
|
51
|
+
# return a valid response
|
52
|
+
def onError(res)
|
53
|
+
puts res.status
|
54
|
+
puts res.body
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,18 @@
|
|
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
|
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
require 'analytics/defaults'
|
3
|
+
require 'multi_json'
|
4
|
+
require 'faraday'
|
5
|
+
require 'faraday_middleware'
|
6
|
+
require 'typhoeus'
|
7
|
+
require 'typhoeus/adapters/faraday'
|
8
|
+
|
9
|
+
module Analytics
|
10
|
+
|
11
|
+
class Request
|
12
|
+
|
13
|
+
# Creates a new request object
|
14
|
+
#
|
15
|
+
def initialize(options = {})
|
16
|
+
|
17
|
+
options[:url] ||= Analytics::Defaults::Request::BASE_URL
|
18
|
+
options[:ssl] ||= Analytics::Defaults::Request::SSL
|
19
|
+
options[:headers] ||= Analytics::Defaults::Request::HEADERS
|
20
|
+
@path = options[:path] || Analytics::Defaults::Request::PATH
|
21
|
+
|
22
|
+
@conn = Faraday.new(options) do |faraday|
|
23
|
+
faraday.request :json
|
24
|
+
faraday.response :json, :content_type => /\bjson$/
|
25
|
+
faraday.adapter :typhoeus
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
|
30
|
+
def post(secret, batch)
|
31
|
+
@conn.post do |req|
|
32
|
+
req.url(@path)
|
33
|
+
req.body = MultiJson.dump(secret: secret, batch: batch)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/spec/client.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require_relative '../lib/analytics'
|
2
|
+
|
3
|
+
|
4
|
+
describe Analytics::Client, "#track" do
|
5
|
+
|
6
|
+
before(:all) { @client = Analytics::Client.new(secret: "testsecret") }
|
7
|
+
|
8
|
+
it "should error without an event" do
|
9
|
+
expect { @client.track(user_id: "user") }.to raise_error(ArgumentError)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should error without a user or session" do
|
13
|
+
expect { @client.track(event: "Event") }.to raise_error(ArgumentError)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should not error with the required options" do
|
17
|
+
@client.track(user_id: "user",
|
18
|
+
event: "Event")
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/module.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative '../lib/analytics'
|
2
|
+
|
3
|
+
|
4
|
+
describe Analytics, "#init" do
|
5
|
+
|
6
|
+
it "should successfully init" do
|
7
|
+
Analytics.init(secret: "testsecret")
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
describe Analytics, "#track" do
|
15
|
+
|
16
|
+
it "should error without an event" do
|
17
|
+
expect { Analytics.track(user_id: "user") }.to raise_error(ArgumentError)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should error without a user or session" do
|
21
|
+
expect { Analytics.track(event: "Event") }.to raise_error(ArgumentError)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should not error with the required options" do
|
25
|
+
Analytics.track(user_id: "user",
|
26
|
+
event: "Event")
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: analytics-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- friends@segment.io
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-16 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description:
|
15
|
+
email:
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- .rspec
|
21
|
+
- README.md
|
22
|
+
- analytics.gemspec
|
23
|
+
- lib/analytics.rb
|
24
|
+
- lib/analytics/client.rb
|
25
|
+
- lib/analytics/consumer.rb
|
26
|
+
- lib/analytics/defaults.rb
|
27
|
+
- lib/analytics/request.rb
|
28
|
+
- spec/client.rb
|
29
|
+
- spec/module.rb
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
require_paths:
|
35
|
+
- lib
|
36
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
requirements: []
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.8.11
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: Segment.io analytics library
|
54
|
+
test_files: []
|